丢失值后复选框中的Gridview [英] Gridview in checkbox after lost value

查看:61
本文介绍了丢失值后复选框中的Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用产品表。我在页面回发后选择了类别DropDownList,我选择了复选框,在页面回发后我选择了另一个DropdownList,我丢失了值Checkbox Checked。我该怎么做。



我尝试过的事情:



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

< >

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



< asp:TemplateField HeaderText = ÜrünAd ItemStyle-Width = 100 < span class =code-keyword>>
< itemtemplate >
< asp:Label ID = lblUrun runat = 服务器 正文 =' <% #Eval( UrunAd)%>' >

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




< < span class =code-leadattribute> asp:TemplateField 标题文字 = Stok ItemStyle-Width = 50 >
< itemtemplate >
< asp:Label ID = < span class =code-keyword> lblStok 样式 = padding:3px 5px; runat = server 文字 =' <% #Eval( Stok)%>' >

< edititemtemplate >
< asp:TextBox ID = txtstok 样式 = 填充:3px 5px; runat = server 正文 =' <% #Eval( Stok)%>' >



< asp: TemplateField HeaderText = Fiyat ItemStyle-Width = 50 >
< span class =code-keyword>< itemtemplate >
< asp:标签 ID = txtfiyat 样式 = 填充:3px 5px; CssClass = form-control runat = 服务器 >

< edititemtemplate > ;
< asp:TextBox ID = txtfiyat 样式 = 填充:3px 5px; runat = server < span class =code-attribute> CssClass = 表格控制 >



< asp:TemplateField HeaderText = Adet ItemStyle-Width = 50 >
< itemtemplate >
< asp:标签 ID = txtAdet 样式 = 填充:3px 5px; CssClass = form-control runat
= 服务器 >

< edititemtemplate > ;
< asp:TextBox ID = txtAdet runat = server 样式 = 填充:3px 5px; CssClass = form-c ontrol >





< asp:TemplateField HeaderText = ElemanSeç ItemStyle-Width = 100 >

< itemtemplate >
< asp:DropDownList ID = drpEleman CssClass = form-control runat = 服务器 >





< asp:TemplateField < span class =code-attribute> HeaderText
= Eleman Adet ItemStyle-Width = 50 >
< itemtemplate >
< asp:标签 ID = txtElemanAdet 样式 = 填充:3px 5px; CssClass = form-control runat = server >

< edititemtemplate >
< asp:Tex tBox ID = txtElemanAdet runat = server 样式 = 填充:3px 5px; CssClass = 表格控制 >



< asp:TemplateField HeaderText < span class =code-keyword> = Eleman Fiyat ItemStyle-Width = 50 >
< itemtemplate >
< asp:标签 ID = txtElemanFiyat 风格 = 填充:3px 5px; CssClass = form-control < span class =code-attribute> runat = 服务器 >

< edititemtemplate >
< asp:TextBox ID = txtElemanFiyat runat = server 样式 = 填充:3px 5px; CssClass = 表格 - control >




< asp:CommandField ButtonType = 链接 ShowEditButton = true ShowDeleteButton = true ItemStyle-Width = 150 / >

解决方案

您必须存储复选框索引或某些内容以识别它们可能位于Session或ViewState属性中。在RowDatabound内加载网格时,你可以检查该会话中是否存在该复选框,简单。


我同意上面的成员,这就是网络的本质。 Web应用程序是无状态的,您必须做一些事情来保持驻留在普通网格或分页网格中的 CheckBox 的状态。您必须跟踪所选页面和选中的 CheckBox 并将其存储在某个地方,例如 ViewState / 会话以便您在回发时可以引用它们。以下是您可以参考的一个示例:保留州在ASP.Net GridView控件中进行分页时的复选框 [ ^ ]


< asp:gridview id =example cssclass =table table-striped table-bordered table-hoverautogeneratecolumns =falsedatakeynames =IDrunat =serverclientidmode =Staticonprerender =example_PreRenderonrowediting =example_RowEditingonrowcancelingedit =example_RowCancelingEdit onrowdeleting =example_RowDeletingonrowdatabound =example_RowDataBoundondatabound =example_DataBound> 

< columns>

< asp:templatefield headertext =ÜrünSeçitemstyle-width =50>

< itemtemplate>

< asp:checkbox id =chcseccssclass =form-controlrunat =serverautopostback =true>



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

< asp:templatefield headertext =ÜrünAditemstyle-width =100>
< itemtemplate>
< asp:label id =lblUrunrunat =servertext =<%#Eval(UrunAd)%>>< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txturunadrunat =servertext =<%#Eval(UrunAd)%>>< / asp:textbox>
< / edititemtemplate>
< / asp:templatefield>


< asp:templatefield headertext =Stokitemstyle-width =50>
< itemtemplate>
< asp:label id =lblStokstyle =padding:3px 5px; runat =servertext =<%#Eval(Stok)%>>< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txtstokstyle =padding:3px 5px; runat =servertext =<%#Eval(Stok)%>>< / asp:textbox>
< / edititemtemplate>
< / asp:templatefield>

< asp:templatefield headertext =Fiyatitemstyle-width =50>
< itemtemplate>
< asp:label id =txtfiyatstyle =padding:3px 5px; cssclass =form-controlrunat =server> 500< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txtfiyatstyle =padding:3px 5px; runat =servercssclass =form-control>< / asp:textbox>
< / edititemtemplate>
< / asp:templatefield>

< asp:templatefield headertext =Adetitemstyle-width =50>
< itemtemplate>
< asp:label id =txtAdetstyle =padding:3px 5px; cssclass =form-controlrunat =server>< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txtAdetrunat =serverstyle =padding:3px 5px;的CssClass = 形式控制 >< / ASP:文本框>
< / edititemtemplate>
< / asp:templatefield>



< asp:templatefield headertext =ElemanSeçitemstyle-width =100>

< itemtemplate>
< asp:dropdownlist id =drpElemancssclass =form-controlrunat =server>< / asp:dropdownlist>
< / itemtemplate>


< / asp:templatefield>

< asp:templatefield headertext =Eleman Adetitemstyle-width =50>
< itemtemplate>
< asp:label id =txtElemanAdetstyle =padding:3px 5px; cssclass =form-controlrunat =server>< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txtElemanAdetrunat =serverstyle =padding:3px 5px;的CssClass = 形式控制 >< / ASP:文本框>
< / edititemtemplate>
< / asp:templatefield>

< asp:templatefield headertext =Eleman Fiyatitemstyle-width =50>
< itemtemplate>
< asp:label id =txtElemanFiyatstyle =padding:3px 5px; cssclass =form-controlrunat =server>< / asp:label>
< / itemtemplate>
< edititemtemplate>
< asp:textbox id =txtElemanFiyatrunat =serverstyle =padding:3px 5px;的CssClass = 形式控制 >< / ASP:文本框>
< / edititemtemplate>
< / asp:templatefield>



< asp:commandfield buttontype =Linkshowsitbutton =trueshowdeletebutton =trueitemstyle-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行。行)
if(((CheckBox)row.FindControl(chcsec))。已检查)
{
datakeys。添加(example.DataKeys [row.RowIndex]。价值);
}
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();

}
}


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: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" />

解决方案

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.


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[^]


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

           }
       }


这篇关于丢失值后复选框中的Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆