user1801525
user1801525

Reputation: 125

iterate through gridview rows on button click

I've got a gridview that the user can select items with a checkbox. At the bottom of the page I have a save button that I want to iterate through the gridview rows and pass the selected rows LinkID. When I go to iterate though, my gridview.rows is returning 0 but there are rows there...what am I doing wrong? Thanks

<asp:UpdatePanel ID="upPanResults" runat="server">
        <ContentTemplate>
            <div id="divResults" style="min-height: 350px;">
                <asp:GridView ID="grdInvoice" ClientIDMode="Static" runat="server" CssClass="DG"
                    Width="100%" DataKeyNames="LinkID" AutoGenerateColumns="False" GridLines="Vertical"
                    CellPadding="4" AllowPaging="True" AllowCustomPaging="True" PageSize="1000" PagerStyle-Visible="False"
                    ShowFooter="true" RowStyle-CssClass="Item" AlternatingRowStyle-CssClass="Alternating"
                    HeaderStyle-CssClass="HomeDGhead" SelectedRowStyle-CssClass="Selected"
                    OnRowDataBound="grdInvoice_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Address">
                            <ItemTemplate>
                                <asp:Label ID="Address" CssClass="norm" runat="server"><%# DataBinder.Eval(Container, "DataItem.Address") %></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Date">
                            <ItemTemplate>
                                <asp:Label ID="Date" CssClass="norm" runat="server"><%# DataBinder.Eval(Container, "DataItem.Date") %></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="LblAmount" CssClass="no_border" Text='<%# DataBinder.Eval(Container, "DataItem.Amount") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="ChkSelect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <table width="98%">
                <tr height="25">
                    <td align="left">
                        <asp:Label ID="Label1" runat="server" CssClass="head2" Text="Approval Number:" ClientIDMode="Static"></asp:Label>&nbsp;&nbsp;<asp:TextBox
                            ID="Txt" runat="server" CssClass="norm" ClientIDMode="Static"></asp:TextBox>
                    </td>
                    <td align="right">
                        <asp:Label ID="Label3" runat="server" CssClass="head2" Text="Selected Total:"></asp:Label>
                        &nbsp;&nbsp;
                        <asp:Label ID="LblTotal" runat="server" ClientIDMode="Static" CssClass="head2" Text="0"></asp:Label>
                    </td>
                </tr>
                <tr height="25">
                    <td align="center" colspan="2">
                        <asp:Button ID="BtnSave" runat="server" CssClass="btn" 
                            Text="Save" OnClick="BtnSave_Click" />
                        &nbsp;&nbsp;<asp:Button ID="btnCancel" runat="server" CssClass="btn" Text="Cancel"
                            OnClick="btnCancel_Click" />
                    </td>
                </tr>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>



protected void BtnSave_Click(object sender, EventArgs e)
        {
            upProgress.Visible = false;
            string sLinks = "";
            int iLinks = 0;
            CheckBox chk;
            foreach (GridViewRow row in grdInvoice.Rows)
            {
                chk = (CheckBox)row.Cells[3].Controls[1];
                if (chk.Checked == true)
                {
                    sLinks += grdInvoice.DataKeys[row.RowIndex].Value.ToString() + ",";
                    iLinks = iLinks + 1;
                }

            }
}

Upvotes: 2

Views: 5666

Answers (2)

Kelsey
Kelsey

Reputation: 47726

You can iterate through the rows using the following to find the CheckBox's that were ticked:

foreach (GridViewRow row in yourGrid.Rows)
{
    CheckBox chk = row.Cells[3].Controls[1] as CheckBox;
    if ((chk != null) && chk.Checked)
    {
        int yourID = Convert.ToInt32(yourGrid.DataKeys[row.RowIndex].Value);
        // Do something with each row here...
    }
}

Are you sure your not rebinding the grid on postback to an empty DataSource before your save button fires? Check your PageLoad event.

I see you have multiple UpdatePanel controls. Try setting up a Trigger in the UpdatePanel that contains the GridView to link to the Click of the UpdatePanel with the save button. Something is going on where the server side no longer knows about the data that was bound to the GridView or the GridView data is not being posted back to the server.

Upvotes: 3

user1801525
user1801525

Reputation: 125

Thanks, no I'm not rebinding on postback. I have noticed, that if I clear everything out of the save button the grid is blank. I'm binding the grid from a go button in a search panel on the same screen.

<asp:UpdatePanel ID="upInvoice" runat="server">
        <ContentTemplate>
            <div id="divSearch" style="background-color: #e6f3e7; border: 1px solid #333; min-height: 50px;">
                <asp:Panel ID="pnlSearch" runat="server" DefaultButton="BtnGo">
                    <br />
                    <asp:Label ID="lblClient" runat="server" Text="Client:" CssClass="head2" ClientIDMode="Static"></asp:Label>&nbsp;&nbsp;
                    <asp:DropDownList ID="ddlClient" runat="server" Width="200px" CssClass="norm" ClientIDMode="Static"
                        AutoPostBack="true" OnSelectedIndexChanged="ddlClient_SelectedIndexChanged">
                    </asp:DropDownList>
                    &nbsp;&nbsp;<asp:Label ID="Label4" runat="server" Text="Area:" ClientIDMode="Static"
                        CssClass="head2" Visible="false"></asp:Label>&nbsp;&nbsp;
                    <asp:DropDownList ID="ddlArea" runat="server" Width="200px" ClientIDMode="Static"
                        CssClass="norm" Visible="false">
                    </asp:DropDownList>
                   <asp:Button ID="btnGo" runat="server"
                                CssClass="btn" Text="Go" OnClick="btnGo_Click" />
                    </br> </br>
                </asp:Panel>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                BindInvoicingClients();

                ddlArea.Items.Add(new ListItem("[ALL]", "0"));
                ddlArea.SelectedValue = "0";

            }
        }
        protected void ddlClient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(ddlClient.SelectedValue) == 1)
            {
                PopulatePropertyAreaDropdown(int.Parse(ddlClient.SelectedValue));

            }
        }

 protected void btnGo_Click(object sender, EventArgs e)
        {

            grdInvoice.DataSource = null;
            grdInvoice.DataBind();

            BindGrid(int.Parse(ddlClient.SelectedValue));

        }

Upvotes: 0

Related Questions