marios
marios

Reputation: 261

CheckBox is not working when I am pressing the button

I am working with Asp.Net C# and want to take the id of the row with a checkbox from the GridView. With the code above if is checked the only think i am getting with debug is:

chk: {Text = "" Checked = false}

What i am doing wrong and how i can fix this?

<!-- 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>

Upvotes: 0

Views: 142

Answers (2)

Dusan
Dusan

Reputation: 5144

This is caused by the fact that you always bind your GridView1 with data, even on post-back. This is causing that all check box-es remain unchecked.

Remove grid loading if you are in post-back, and it should work fine.

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

If you, however, need to load grid even in post-back, I will tell you a fix for that.

Upvotes: 1

renakre
renakre

Reputation: 8291

Whenever you bind a data to a data control in Page_Load event, please consider using Page.IsPostBack

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

This is because, when you click a button, your page will be still refreshed, which means you will bind the same data to gridview again, resulting in unchecked checkboxes. You can check whether the page is loaded by a button click (Page.IsPostBack) or the page is loaded the first time (!Page.IsPostBack), and do your data binding accordingly.

Hope this makes sense!

Upvotes: 1

Related Questions