简体   繁体   中英

How to Find label control value in Gridview Footer ButtonClick Event

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# Code Button Click event which is in Gridview Footer Template

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

it throws exception which is below

Object reference not set to an instance of an object.

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

by this line I'm getting only Null Values.How do i find Label Value?

Thanks

You can check row type wether it is header row or data row... .. ie if it is data row then only your code will will execute

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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