在按钮单击时迭代gridview行

时间:2014-01-14 18:33:21

标签: c# asp.net gridview

我有一个gridview,用户可以选中带有复选框的项目。在页面的底部,我有一个保存按钮,我想遍历gridview行并传递选定的行LinkID。当我去迭代时,我的gridview.rows返回0但是那里有行......我做错了什么?感谢

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

            }
}

2 个答案:

答案 0 :(得分:2)

您可以使用以下内容迭代这些行,以查找已勾选的CheckBox

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

在你的保存按钮触发之前,你确定你没有在回发时将网格重新绑定为空DataSource吗?查看您的PageLoad活动。

我看到你有多个UpdatePanel控件。尝试在包含Trigger的{​​{1}}中设置UpdatePanel,并使用保存按钮链接到GridView的{​​{1}}。服务器端不再知道绑定到Click的数据或UpdatePanel数据未被发回服务器的情况正在发生。

答案 1 :(得分:0)

谢谢,不,我不会在回发上重新绑定。我注意到,如果我从保存按钮中清除所有内容,则网格为空白。我正在同一屏幕上的搜索面板中的go按钮绑定网格。

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

        }