如何在Gridview页脚按钮中查找标签控件值单击“事件”

时间:2015-02-07 10:07:55

标签: c# asp.net gridview

Gridview Aspx Page

<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>

C#代码     按钮Click事件在Gridview页脚模板

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");//

通过这一行,我只得到空值。我如何找到标签值?

由于

1 个答案:

答案 0 :(得分:0)

您可以检查行类型,无论是标题行还是数据行... ..即如果它是数据行,那么只有你的代码才会执行​​

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 ");
                }
}