无法在转发器ASP.NET中获取复选框选中的值 [英] Can't get checkbox checked value in repeater ASP.NET

查看:67
本文介绍了无法在转发器ASP.NET中获取复选框选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Aspx

Aspx

<asp:Repeater ID="rptOzellik" runat="server">
     <ItemTemplate>

      <div class="col-md-5 pad10">
         <asp:CheckBox ID="check" runat="server" />
         <asp:HiddenField ID="OzellikID" Value='<%#Eval("OzellikId") %>' runat="server" />

       </div>


     </ItemTemplate>
     </asp:Repeater>





代码背后:



Code Behind:

int[] OzellikIdArray = new int[rptOzellik.Items.Count];
         int i = 0;
         foreach (RepeaterItem item in rptOzellik.Items)
         {
             HiddenField OzellikID = (HiddenField)item.FindControl("OzellikId");
             var checkBox = (CheckBox)item.FindControl("check");
             if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
             {

                 if (checkBox.Checked)
                 {

                     OzellikIdArray[i] = int.Parse(OzellikID.Value.ToString());
                 }

             }
             i++;
         }





我勾选了几个复选框,但checkBox.checked总是返回false。

什么我错过了吗?



我尝试了什么:



我尝试了很多解决方案,但我无法解决



I checked a few checkbox but checkBox.checked always returned false.
What i'm missing ?

What I have tried:

I tried a lot of way for solution but i couldn't solve

推荐答案

你应该做的是遍历控件,检查每个控件是否是一个复选框如果它是,并且具有正确的名称,请检查状态。
What you should do is to iterate through the controls, checking each to see if it is a checkbox.If it is, and has the right name, check the state.


我已经测试了您的代码,它的工作正常。

您必须移动内的数据绑定代码!Page.IsPostBack



I have tested your code, its working fine.
You will have to move the databinding code inside the !Page.IsPostBack

if (!Page.IsPostBack)
            {
                rptOzellik.DataSource = yourDataSource;
                rptOzellik.DataBind();
            }


这篇关于无法在转发器ASP.NET中获取复选框选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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