GridView - 选择第一行不起作用

时间:2018-06-14 08:33:14

标签: c# asp.net gridview checkbox

我对gridview有疑问,选择一行复选框。

enter image description here

我在Label中获得了正确的文字,但如果我从第一行选择复选框:

enter image description here

我明白了:房间没有选择

BUTTON CLICK

protected void bookButton_Click(object sender,EventArgs e)         {

        foreach (GridViewRow row in GridView1.Rows)
        {


            var chk = (HtmlInputCheckBox) row.FindControl("checkboxID");
            int id_room = Convert.ToInt32(row.Cells[4].Text);
            if (chk.Checked)
            {
                Label1.Text = id_room.ToString();

                /* 
                String CS = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
                 using (SqlConnection con = new SqlConnection(CS))
                 {
                        //STORED PROCEDURE CALL
                 }

            }

            else
            {

                Label1.Text ="Room not pick";
            }
                */
        }
    }

AND GRIDVIEW

                            <asp:GridView ID="GridView1" runat="server" CssClass="table border-0 table-hover" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" GridLines="None" BorderWidth="0px">

                <Columns>
                    <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                    <asp:BoundField DataField="Picture" HeaderText="Picture" SortExpression="Picture" />
                    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                     <asp:BoundField DataField="ID"  HeaderText="IDP" SortExpression="ID" />
                    <asp:TemplateField>

                        <ItemTemplate>
                            <input type="checkbox" CssClass="custom-checkbox" ID="checkboxID" runat="server"  />
                        </ItemTemplate>

                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>

2 个答案:

答案 0 :(得分:0)

索引错误。它的4列。作为基于零的索引,您应该使用3

int id_room = Convert.ToInt32(row.Cells[3].Text);

更简洁的方法是使用ItemTemplate

<asp:TemplateField HeaderText="IDP" SortExpression="ID">
    <ItemTemplate>
        <asp:Label ID="roomID" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

并像

一样访问它
foreach(GridViewRow row in GridView1.Rows) {
    var chk = (HtmlInputCheckBox)row.FindControl("checkboxID");
    var selectedRoomID = (Label)row.FindControl("roomID");
    if (chk.Checked) {
        Label1.Text = selectedRoomID.Text;
    }
}

答案 1 :(得分:0)

好的,谢谢@naveen!我改变了这样的代码:

 foreach (GridViewRow row in GridView1.Rows)
        {
            var chk = (HtmlInputCheckBox)row.FindControl("checkboxID");
            var selectedRoomID = (Label)row.FindControl("Label2");

            if (chk.Checked && chk != null)
            {

                Label1.Text = selectedRoomID.Text;
            }
            else
               {
              Label1.Text = "error";
                }

并将ID_ROOM col更改为ItemTemplates。