繁体   English   中英

如何在Gridview页脚ButtonClick事件中查找标签控件值

[英]How to Find label control value in Gridview Footer ButtonClick Event

Gridview Aspx页面

<asp:GridView ID="GridView2" runat="server" ShowFooter="true" GridLines="None" AutoGenerateColumns="False" CssClass="table table-hover">
        <Columns>
            <asp:TemplateField HeaderText="S.no">
            <ItemTemplate><%#Container.DataItemIndex+1 %>
                <asp:Label ID="lblReqNo1" Visible="false" runat="server" Text='<%#Eval("ReqNo")%>' ></asp:Label>
                     <asp:Label ID="lblFranchise_id" Visible="false" runat="server" Text='<%#Eval("franchise_id")%>' ></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAccept" runat="server" CssClass="btn btn-danger" 
                    Text="Accept Request" onclick="btnAccept_Click" />

            </FooterTemplate>
            </asp:TemplateField>
          <asp:BoundField HeaderText="Product Name" DataField="p_name" />
          <asp:BoundField HeaderText="Quantity" DataField="Qty" />
          <asp:BoundField HeaderText="Dealer Price" DataField="DP" />
          <asp:BoundField HeaderText="Amount" DataField="amt" />

        </Columns>
    </asp:GridView>

Gridview页脚模板中的C#代码按钮单击事件

protected void btnAccept_Click(object sender, EventArgs e)
            {
              //  GridViewRow row = (GridViewRow)((sender as Button).NamingContainer);
                using (GridViewRow row = (GridViewRow)((Button)sender).NamingContainer)
                {
                    Label lblReqNo = (Label)row.FindControl("lblReqNo1");

                    Label lblFranchise_id = (Label)row.FindControl("lblFranchise_id");
                    objFund.Transfer_id = int.Parse(lblReqNo.Text);
                    objFund.Type = 9;
                    int a = objFund.Insert();

                    objFund.Transfer_id = int.Parse(lblFranchise_id.Text);//franchise Id
                    objFund.CreditAmt = float.Parse(lblReqNo.Text);
                    objFund.Type = 5;
                    int b = objFund.Insert();
                    if (b > 0)
                    {
                        msg.Alert("Request accepted and mount Debited ");
                    }
                }
            }

它引发下面的异常

Object reference not set to an instance of an object.

Label lblReqNo = (Label)row.FindControl("lblReqNo1");//

通过此行,我仅获得Null值。如何查找标签值?

谢谢

您可以检查行类型,无论是标题行还是数据行.....即,如果它是数据行,则仅将执行您的代码

if (row.RowType == DataControlRowType.DataRow)
{

                Label lblReqNo = (Label)row.FindControl("lblReqNo1");

                Label lblFranchise_id = (Label)row.FindControl("lblFranchise_id");
                objFund.Transfer_id = int.Parse(lblReqNo.Text);
                objFund.Type = 9;
                int a = objFund.Insert();

                objFund.Transfer_id = int.Parse(lblFranchise_id.Text);//franchise Id
                objFund.CreditAmt = float.Parse(lblReqNo.Text);
                objFund.Type = 5;
                int b = objFund.Insert();
                if (b > 0)
                {
                    msg.Alert("Request accepted and mount Debited ");
                }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM