none
vs 2013 webserver 控件状态回送错误 RRS feed

  • 问题

  • 在vs2013生一页面,页面上有两个控件:Repeater 列表控件,button控件,在reapeater有一列含有checkbox控件,作用是用来表示在repeater中选择的行。相应的控件都设置好。在前端选择行后,单击button,在后端代码中根据Repeater中的checkBox列识别哪些行被选择,但在实践中发现,checkbox状态没有回送到服务器端,事实上这个功能在vs2010 vs2008上都没有问题,这是什么原因?
    2016年1月21日 8:44

答案

  • 你好,

    你需要修改你的Page Load代码为如下,我已在自己电脑测试并能成功获取Checkbox的值。

    因为,在你页面回传的时候,首先加载的是Page Load 方法,然后将重新执行“FillList()”方法,从而将之前各个Checkbox的值初始化。

            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    FillList();
                }
            }
    

    Best Regards, <o:p></o:p>

    Albert Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 19711024 2016年1月24日 23:46
    2016年1月24日 8:59

全部回复

  • 你好,

    你可以提供些前台页面代码和后台获取CheckBox的代码吗?这样,我就可以把你代码在我电脑测试下,从而能更好的帮你找到问题点。

    根据上面的问题描述,你是想获取RepeaterCheckBox的值。如下的链接你可以参考下,其中提供了关于获取Checkbox的代码, 示例中首先遍历获取每个RepeaterItem,然后使用FindControl()得到每一个Checkbox.

    http://stackoverflow.com/questions/16697093/get-values-of-all-checkboxes-in-repeater

    至于为什么你的在VS2013上不能成功,建议你首先看下Checkbox是否是Server控件,然后F11一步步debug你的代码,找到为什么没有获取Checkbox的原因。

    Best Regards,

    Albert Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年1月21日 12:04
  • <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server" method="post">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" ClientIDMode="Static" OnCheckedChanged="CheckBox1_CheckedChanged"  />
                          <asp:Repeater runat="server" ID="WishListRpt"  EnableTheming="True" ViewStateMode="Enabled" ClientIDMode="Static" ValidateRequestMode="Enabled" OnItemDataBound="WishListRpt_ItemDataBound" >
                            <HeaderTemplate >
                                <table class="Grid_txt" cellspacing="0" cellpadding="0" border="0" style="width:100%;border-collapse:collapse;">
                    <tbody>
                                 <tr class="GV-title-bottom" align="center" style="height:26px;">
                     <th class="Gridtitle" scope="col" onclick="CheckAll(this);">Select All</th>
                                    <th class="Gridtitle" scope="col" align="left">ItemName</th>
                                    <th class="Gridtitle" scope="col">Collect Times</th>
                                    <th class="Gridtitle" scope="col">Collect Customer</th>
                     </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                 <tr class="Grid_Row" align="center">
                     <td style="width:70px;"><%#Eval("RowNo")%> <asp:CheckBox ID="chkId" CssClass="NullClass" runat="server" style="vertical-align:middle;" ClientIDMode="Static" ViewStateMode="Enabled" Font-Strikeout="False" /><asp:HiddenField ID="hidId" Value='<%#Eval("ID")%>' runat="server" ClientIDMode="Static"/></td></td>
                                    <td class="Grid_txt" align="left" >
                                        <a href="#" target="_blank"><img src="enter_view.gif" alt="go to DS check it!" border="0"></a><%#Eval("ItemNames")%></td>
                                    <td align="center" style="width:100px;"><%#Eval("TotalCount") %></td>
                                    <td align="center" style="width:140px;"><input id="but1" type="button" value="View Details" onclick="EV_modeAlert(this, 'popDiv')"/></td>
                    </tr>
                              
                            </ItemTemplate>
                              <AlternatingItemTemplate>
                                   <tr class="Grid_Row" style="background-color: #7f9db9;" align="center">
                     <td style="width:70px;"><%#Eval("RowNo")%> <asp:CheckBox ID="chkId" CssClass="NullClass" runat="server" style="vertical-align:middle;" ClientIDMode="Static" ViewStateMode="Enabled" EnableViewState="True" /><asp:HiddenField ID="hidId" Value='<%#Eval("ID")%>' runat="server" /></td></td>
                                    <td class="Grid_txt" align="left" >
                                        <a href="#" target="_blank"><img src="enter_view.gif" alt="go to DS check it!" border="0"></a><%#Eval("ItemNames")%></td>
                                    <td align="center" style="width:100px;"><%#Eval("TotalCount") %></td>
                                    <td align="center" style="width:140px;"><input id="but1" type="button" value="View Details" onclick="EV_modeAlert(this, 'popDiv')"/></td>
                    </tr>
                              </AlternatingItemTemplate>
                            <FooterTemplate>
                                <%#WishListRpt.Items.Count == 0 ? "<tr><td align=\"center\" colspan=\"7\">*No data available</td></tr>" : ""%>
                        </tbody>
                               </table>
                            </FooterTemplate>
                          </asp:Repeater>
       
        </div>
        </form>
    </body>
    </html>

     

    

    

    2016年1月21日 23:49
  • using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    namespace WebApplication2
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                FillList();
              
            }
            private void FillList()
            {

                DataTable dt = new DataTable();
                DataColumn col1 = new DataColumn("RowNo");
                DataColumn col2 = new DataColumn("ItemNames");
                DataColumn col3 = new DataColumn("totalCount");
                DataColumn col4 = new DataColumn("ID");
                dt.Columns.Add(col1);
                dt.Columns.Add(col2);
                dt.Columns.Add(col3);
                dt.Columns.Add(col4);
                DataRow dr;
                for (int i = 0; i < 4; i++)
                {
                    dr = dt.NewRow();
                    dr["RowNo"] = (i + 1).ToString();
                    dr["ItemNames"] = "ItemName" + (i + 1).ToString();
                    dr["totalCount"] = (i + 4).ToString();
                    dr["id"] = (i + 1).ToString();
                    dt.Rows.Add(dr);
                }
                WishListRpt.DataSource = dt;
                WishListRpt.DataBind();


            }

            protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                RepeaterItemCollection Ric = WishListRpt.Items;
                foreach (RepeaterItem ri in Ric)
                {
                    if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox cb = ri.FindControl("chkId") as CheckBox;
                        if (cb.Checked)
                        {

                            break;
                        }

                    }
                }

            }


            protected void LinkButton1_Click(object sender, EventArgs e)
            {
                RepeaterItemCollection Ric = WishListRpt.Items;
                foreach (RepeaterItem ri in Ric)
                {
                    if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
                    {
                        CheckBox cb = ri.FindControl("chkId") as CheckBox;//请看些处的checked属性的值能否是true
                        if (cb.Checked)
                        {

                            break;
                        }

                    }
                }

            }

            protected void WishListRpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {

            }
        }
    }

    2016年1月21日 23:49
  • 以上是前端与后端代码

    2016年1月21日 23:50
  • 你好,

    你需要修改你的Page Load代码为如下,我已在自己电脑测试并能成功获取Checkbox的值。

    因为,在你页面回传的时候,首先加载的是Page Load 方法,然后将重新执行“FillList()”方法,从而将之前各个Checkbox的值初始化。

            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    FillList();
                }
            }
    

    Best Regards, <o:p></o:p>

    Albert Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 19711024 2016年1月24日 23:46
    2016年1月24日 8:59
  • 非常感谢!

    2016年1月24日 23:44