当我按下按钮时,CheckBox不起作用

时间:2015-04-09 12:10:36

标签: c# asp.net gridview checkbox

我正在使用Asp.Net C#,并希望使用GridView中的复选框获取行的ID。 如果检查上面的代码,我认为调试的唯一想法是:

chk: {Text = "" Checked = false}

我做错了什么以及如何解决这个问题?

<!-- language: lang-css -->
  protected void Page_Load(object sender, EventArgs e)
        {
            string query = "SELECT * FROM routedoc WHERE Recipient=@user ";

            string user = Session["user"].ToString();
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["imagingConnectionString"].ConnectionString);
            MySqlCommand cmd = new MySqlCommand(query, con);
            cmd.Parameters.AddWithValue("@user", user);
            con.Open();
            DataTable dataTable = new DataTable();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);

            da.Fill(dataTable);


            GridView1.DataSource = dataTable;
            GridView1.DataBind();

            if(!IsPostBack)
            {

                BindDropDown();

            }
        }
private void BindDropDown()
        {
            string user = Session["user"].ToString();
            using (MySqlConnection con2 = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["imagingConnectionString"].ConnectionString))
            {


                MySqlDataAdapter adp = new MySqlDataAdapter("SELECT RouteNo,sender FROM routedoc WHERE Recipient = @user  ", con2);
                adp.SelectCommand.Parameters.AddWithValue("?user", user);
                DataTable dt = new DataTable();
                adp.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    DropDownList1.DataSource = dt;
                    DropDownList1.DataTextField = "sender";
                    DropDownList1.DataValueField = "RouteNo";
                    DropDownList1.DataBind();
                }

            }

        }
 protected void Button1_Click(object sender, EventArgs e)
   {

       // Response.Write(GridView1.SelectedRow.Cells[1].Text);

        foreach(GridViewRow row in GridView1.Rows)
        {
            var chk = row.FindControl("box") as CheckBox ;
            if (chk.Checked) 
            {
                var id = row.FindControl("RouteNo") as Label;
                Response.Write(id.Text);
            }

         }
    }

routedoc.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="routedoc.aspx.cs" Inherits="WebApplication2.WebForm2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>page 2<asp:DropDownList ID="DropDownList1"  runat="server" Height="20px" AutoPostBack="True"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="136px" >
        </asp:DropDownList>
        </h1>

    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Recipient,DocHandle,Sender"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
            <Columns>
                <asp:BoundField DataField="RouteNo" HeaderText="RouteNo" InsertVisible="False" SortExpression="RouteNo" />
                <asp:BoundField DataField="Recipient" HeaderText="Recipient" ReadOnly="True" SortExpression="Recipient" />
                <asp:BoundField DataField="DocHandle" HeaderText="DocHandle" ReadOnly="True" SortExpression="DocHandle" />
                <asp:BoundField DataField="Sender" HeaderText="Sender" ReadOnly="True" SortExpression="Sender" />
                <asp:BoundField DataField="TermNo" HeaderText="TermNo" SortExpression="TermNo" />
                <asp:BoundField DataField="SentDate" HeaderText="SentDate" SortExpression="SentDate" />
                <asp:BoundField DataField="DueDate" HeaderText="DueDate" SortExpression="DueDate" />
                <asp:BoundField DataField="SentTime" HeaderText="SentTime" SortExpression="SentTime" />
                <asp:BoundField DataField="DueTime" HeaderText="DueTime" SortExpression="DueTime" />
                <asp:BoundField DataField="Action" HeaderText="Action" SortExpression="Action" />
                <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
                <asp:BoundField DataField="BUDate" HeaderText="BUDate" SortExpression="BUDate" />
 <asp:TemplateField HeaderText="Select">
     <ItemTemplate><asp:CheckBox ID="Box" runat="server"/>
     </ItemTemplate></asp:TemplateField>
           </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:imagingConnectionString %>" ProviderName="<%$ ConnectionStrings:imagingConnectionString.ProviderName %>" SelectCommand="SELECT * FROM routedoc "></asp:SqlDataSource>
        <asp:Button ID="Button1" runat="server" Height="43px" OnClick="Button1_Click" Text="DELETE" Width="109px" />
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

这是因为您始终将GridView1与数据绑定, 甚至在回发后。这导致所有复选框保持未选中状态。

如果你在回发后删除网格加载,它应该可以正常工作。

protected void Page_Load(object sender, EventArgs e)
{                        
    if (!Page.IsPostBack)
    {
        string query = "SELECT * FROM routedoc WHERE Recipient=@user ";

        string user = Session["user"].ToString();
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["imagingConnectionString"].ConnectionString);
        MySqlCommand cmd = new MySqlCommand(query, con);
        cmd.Parameters.AddWithValue("@user", user);
        con.Open();
        DataTable dataTable = new DataTable();
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        da.Fill(dataTable);

        GridView1.DataSource = dataTable;
        GridView1.DataBind();
        BindDropDown();
    }
 }

但是,如果您需要在回发后加载网格,我会告诉您修复此问题。

答案 1 :(得分:1)

每当您将数据绑定到Page_Load事件中的数据控件时,请考虑使用Page.IsPostBack

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

这是因为,当您单击某个按钮时,您的页面仍会刷新,这意味着您将再次将相同的数据绑定到gridview,从而导致出现未选中的复选框。您可以通过单击按钮(Page.IsPostBack)来检查页面是否已加载,或者是第一次加载页面(!Page.IsPostBack),并相应地进行数据绑定。

希望这是有道理的!