Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I am using Product Table. I choise Category DropDownList after page is postback and I choise checkbox and I choise another DropdownList after page is postback and I lost value Checkbox Checked. How can I do example.

What I have tried:

ASP.NET
<asp:GridView ID="example" CssClass="table table-striped table-bordered table-hover" AutoGenerateColumns="false" runat="server" ClientIDMode="Static" OnPreRender="example_PreRender" OnRowEditing="example_RowEditing" OnRowCancelingEdit="example_RowCancelingEdit" OnRowDeleting="example_RowDeleting" OnRowDataBound="example_RowDataBound">

<columns>

    <asp:TemplateField HeaderText="Ürün Seç" ItemStyle-Width="50">
        <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />
        </HeaderTemplate>
        <itemtemplate>
            <asp:CheckBox ID="chcsec" CssClass="form-control" runat="server" onclick="Check_Click(this);" />



    <asp:TemplateField HeaderText="Ürün Ad" ItemStyle-Width="100">
        <itemtemplate>
            <asp:Label ID="lblUrun" runat="server" Text='<%# Eval("UrunAd") %>'>

        <edititemtemplate>
            <asp:TextBox ID="txturunad" runat="server" Text='<%# Eval("UrunAd") %>'>




    <asp:TemplateField HeaderText="Stok" ItemStyle-Width="50">
        <itemtemplate>
            <asp:Label ID="lblStok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'>

        <edititemtemplate>
            <asp:TextBox ID="txtstok" Style="padding: 3px 5px;" runat="server" Text='<%# Eval("Stok") %>'>



    <asp:TemplateField HeaderText="Fiyat" ItemStyle-Width="50">
        <itemtemplate>
            <asp:Label ID="txtfiyat" Style="padding: 3px 5px;" CssClass="form-control" runat="server">

        <edititemtemplate>
            <asp:TextBox ID="txtfiyat" Style="padding: 3px 5px;" runat="server" CssClass="form-control">



    <asp:TemplateField HeaderText="Adet" ItemStyle-Width="50">
        <itemtemplate>
            <asp:Label ID="txtAdet" Style="padding: 3px 5px;" CssClass="form-control" runat="server">

        <edititemtemplate>
            <asp:TextBox ID="txtAdet" runat="server" Style="padding: 3px 5px;" CssClass="form-control">





    <asp:TemplateField HeaderText="Eleman Seç" ItemStyle-Width="100">

        <itemtemplate>
            <asp:DropDownList ID="drpEleman" CssClass="form-control" runat="server">





    <asp:TemplateField HeaderText="Eleman Adet" ItemStyle-Width="50">
        <itemtemplate>
            <asp:Label ID="txtElemanAdet" Style="padding: 3px 5px;" CssClass="form-control" runat="server">

        <edititemtemplate>
            <asp:TextBox ID="txtElemanAdet" runat="server" Style="padding: 3px 5px;" CssClass="form-control">



    <asp:TemplateField HeaderText="Eleman Fiyat" ItemStyle-Width="50">
        <itemtemplate>
            <asp:Label ID="txtElemanFiyat" Style="padding: 3px 5px;" CssClass="form-control" runat="server">

        <edititemtemplate>
            <asp:TextBox ID="txtElemanFiyat" runat="server" Style="padding: 3px 5px;" CssClass="form-control">




    <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
Posted
Updated 21-Nov-16 3:02am
v3
Comments
Suvendu Shekhar Giri 19-Nov-16 21:58pm    
Well, so what is your question?
Member 12859772 20-Nov-16 2:57am    
You dont understand me? I am using Gridview and in checkbox. When I checked Checkbox after postback lost Checkbox Value. I want to do if checkbox checked keep value.

You have to store the checkbox indexes or something to recognize them somewhere may be in a Session or ViewState property. While loading the grid, inside RowDatabound, you can check that checkbox if it is present in the session, simple.
 
Share this answer
 
Comments
I agree with the member above, that's the nature of the web. Web Apps are stateless and you have to do something to persist the state of the CheckBox that resides in a normal grid or paged grid. You'll have to track the selected page and the CheckBox that was selected and store it somewhere like ViewState/Session so that you can reference them back when it postbacks. Here's one example that you can refer: Preserving state of Checkboxes while paging in ASP.Net GridView Control[^]
 
Share this answer
 
Comments
Member 12859772 21-Nov-16 6:13am    
is that true that code?
Vincent Maverick Durano 21-Nov-16 7:47am    
what do you mean?
Member 12859772 21-Nov-16 8:14am    
My code is true? I asked.
Vincent Maverick Durano 21-Nov-16 8:16am    
Which code? If you mean your solution, then yes. You need to use Session/ViewState to reference the selected CheckBox state across postbacks.
Member 12859772 21-Nov-16 8:34am    
But I have problem. I have two Category and according to Products. When I selected First category and selected two Product checkbox. After I selected second category. I selected one product. After I want to do selected checkboxes Total price. When I click Total Button Only total 2 checkbox. So I selected 3 checkbox. How can I do.
<asp:gridview id="example" cssclass="table table-striped table-bordered table-hover" autogeneratecolumns="false" datakeynames="ID" runat="server" clientidmode="Static" onprerender="example_PreRender" onrowediting="example_RowEditing" onrowcancelingedit="example_RowCancelingEdit" onrowdeleting="example_RowDeleting" onrowdatabound="example_RowDataBound" ondatabound="example_DataBound">

                                            <columns>

                                                <asp:templatefield headertext="Ürün Seç" itemstyle-width="50">

                                                    <itemtemplate>
                                                       
                                                            <asp:checkbox id="chcsec" cssclass="form-control" runat="server" autopostback="true">


                                                      
                                                    </asp:checkbox></itemtemplate>
                                                </asp:templatefield>

                                                <asp:templatefield headertext="Ürün Ad" itemstyle-width="100">
                                                    <itemtemplate>
                                                        <asp:label id="lblUrun" runat="server" text="<%# Eval("UrunAd") %>"></asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txturunad" runat="server" text="<%# Eval("UrunAd") %>"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>


                                                <asp:templatefield headertext="Stok" itemstyle-width="50">
                                                    <itemtemplate>
                                                        <asp:label id="lblStok" style="padding: 3px 5px;" runat="server" text="<%# Eval("Stok") %>"></asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txtstok" style="padding: 3px 5px;" runat="server" text="<%# Eval("Stok") %>"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>

                                                <asp:templatefield headertext="Fiyat" itemstyle-width="50">
                                                    <itemtemplate>
                                                        <asp:label id="txtfiyat" style="padding: 3px 5px;" cssclass="form-control" runat="server">500</asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txtfiyat" style="padding: 3px 5px;" runat="server" cssclass="form-control"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>

                                                <asp:templatefield headertext="Adet" itemstyle-width="50">
                                                   <itemtemplate>
                                                        <asp:label id="txtAdet" style="padding: 3px 5px;" cssclass="form-control" runat="server"></asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txtAdet" runat="server" style="padding: 3px 5px;" cssclass="form-control"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>



                                                <asp:templatefield headertext="Eleman Seç" itemstyle-width="100">

                                                    <itemtemplate>
                                                        <asp:dropdownlist id="drpEleman" cssclass="form-control" runat="server"></asp:dropdownlist>
                                                    </itemtemplate>


                                                </asp:templatefield>

                                                <asp:templatefield headertext="Eleman Adet" itemstyle-width="50">
                                                    <itemtemplate>
                                                        <asp:label id="txtElemanAdet" style="padding: 3px 5px;" cssclass="form-control" runat="server"></asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txtElemanAdet" runat="server" style="padding: 3px 5px;" cssclass="form-control"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>

                                                <asp:templatefield headertext="Eleman Fiyat" itemstyle-width="50">
                                                    <itemtemplate>
                                                        <asp:label id="txtElemanFiyat" style="padding: 3px 5px;" cssclass="form-control" runat="server"></asp:label>
                                                    </itemtemplate>
                                                    <edititemtemplate>
                                                        <asp:textbox id="txtElemanFiyat" runat="server" style="padding: 3px 5px;" cssclass="form-control"></asp:textbox>
                                                    </edititemtemplate>
                                                </asp:templatefield>

                                         

                                                <asp:commandfield buttontype="Link" showeditbutton="true" showdeletebutton="true" itemstyle-width="150">
                                            </asp:commandfield></columns>

                                        </asp:gridview>


------------------------------------------------------------------------------------

if (!Page.IsPostBack)
          {
              //Uruns();
              //Projes();
              //Siparislers();
              Elemans();
              Musteris();
              Kategoris();



          }
          else
          {
              List<object> datakeys = ViewState["datakeys"] != null ?
                          (List<object>)ViewState["datakeys"] : new List<object>();

              foreach (GridViewRow row in example.Rows)
                  if (((CheckBox)row.FindControl("chcsec")).Checked)
                  {
                      datakeys.Add(example.DataKeys[row.RowIndex].Value);
                  }
                  else
                  {
                      datakeys.Remove(example.DataKeys[row.RowIndex].Value);
                  }
              ViewState["datakeys"] = datakeys;
          }


-----------------------------------------------------------

protected void example_DataBound(object sender, EventArgs e)
       {
           //Session.Add("arrcon", arr);

           if (example.Rows.Count > 0 && ViewState["datakeys"] != null)
           {
               List<object> datakeys = (List<object>)ViewState["datakeys"];
               foreach (GridViewRow row in example.Rows)
                   ((CheckBox)row.FindControl("chcsec")).Checked = datakeys.Contains(example.DataKeys[row.RowIndex].Value);
           }
       }

       protected void btnTopla_Click(object sender, EventArgs e)
       {
           if (example.Rows.Count > 0 && ViewState["datakeys"] != null)
           {
               int sonuc = 0;
               List<object> datakeys = (List<object>)ViewState["datakeys"];
               int i = 0;
               foreach (GridViewRow row in example.Rows)
               {
                   ((CheckBox)row.FindControl("chcsec")).Checked = datakeys.Contains(example.DataKeys[row.RowIndex].Value);

                   var lblFiyat = row.FindControl("txtfiyat") as Label;


                   int sayi1 = Convert.ToInt32(lblFiyat.Text);

                   i++;

                   sonuc = sayi1 * i;

               }

               txtToplamfiyat.Text = sonuc.ToString();

           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900