asp.net – GridView不记得回发之间的状态
我有一个简单的ASP页面与数据绑定网格(绑定到对象源).网格位于向导的页面内,每行都有一个“选择”复选框.
在向导的一个阶段,我绑定GridView: protected void Wizard1_NextButtonClick(object sender,WizardNavigationEventArgs e) { ... // Bind and display matches GridViewMatches.EnableViewState = true; GridViewMatches.DataSource = getEmailRecipients(); GridViewMatches.DataBind(); 单击完成按钮后,我会遍历行并检查所选内容: protected void Wizard1_FinishButtonClick(object sender,WizardNavigationEventArgs e) { // Set the selected values,depending on the checkboxes on the grid. foreach (GridViewRow gr in GridViewMatches.Rows) { Int32 personID = Convert.ToInt32(gr.Cells[0].Text); CheckBox selected = (CheckBox) gr.Cells[1].FindControl("CheckBoxSelectedToSend"); 但是在这个阶段GridViewMatches.Rows.Count = 0!我不重新绑定网格,我不应该,对吧?我希望视图状态能够维持状态. (另外,如果我重新绑定网格,我的选择复选框将被清除) 注意:此页面还在OnInit方法中动态添加用户控件.我听说它可能会混淆视图状态,但据我所知,我正确地做了,并且那些添加控件的视图状态似乎有效(值在回发之间保持不变) 非常感谢您的任何帮助! 瑞安 更新:这可能与我以编程方式设置数据源的事实有关吗?我想知道asp引擎是否在页面生命周期中将网格数据绑定到尚未定义的数据源. (在测试页面中,GridView是’自动’数据绑定’.我不希望网格重新绑定我只想要来自上一篇文章的viewstate中的值! 另外,我在asp标题中有这个:ViewStateEncryptionMode =“Never” – 这是为了解决偶尔出现的“无效的Viewstate验证MAC”消息 作为参考,我的GridView定义如下: <asp:GridView ID="GridViewMatches" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" OnDataBinding="GridViewMatches_OnBinding"> <Columns> <asp:BoundField DataField="PersonID"><ItemStyle CssClass="hidden"/></asp:BoundField> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBoxSelectedToSend" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"SelectedToSend") %>'/> </ItemTemplate> ... 解决方法
迭代PreInit事件中的控件(以检测是否按下了“添加另一个控件”或“删除另一个控件”按钮)使视图状态无效!
这是从PreInit调用的方法 public Control GetPostBackControl(Page thePage) { //return null; Control myControl = null; string ctrlName = thePage.Request.Params.Get("__EVENTTARGET"); if (((ctrlName != null) & (ctrlName != string.Empty))) { myControl = thePage.Master.FindControl(ctrlName); } else { foreach (string Item in thePage.Request.Form) { Control c = thePage.Master.FindControl(Item); if (((c) is System.Web.UI.WebControls.Button)) { myControl = c; } } } return myControl; } (我不相信这个方法,我在网上找到它) 如果取消注释第一行,则维护视图状态. 可怕! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 让Sql Server数据工具与Visual Studio 2012 Exp
- 自签名ASP.NET Web API 2 REST服务的令牌身份验证和授权
- asp.net – Web.config身份验证错误
- asp.net-mvc – 在web api 2中使用autofac的无参数构造函数
- asp.net中的动态用户控件
- asp.net – ListView与DataPager不工作
- asp.net-mvc-3 – 在仅HTTPS站点上对HTTP HEAD请求的正确响
- ASP.NET MemoryCache跨应用程序共享
- asp.net-mvc – ConfigurationManager读取错误的文件 – We
- asp.net-mvc-4 – 在Visual Studio 2017 RC中无法识别ASP.N
- ASP.NET Core 2.0 机密配置项
- asp.net-mvc-4 – 多个RequiredFieldValidator崩
- asp.net-mvc – MVC 5中的IModelBinder签名是否已
- ASP.NET Core WebAPI 实现CRUD
- asp.net core webapi文件上传
- asp.net-mvc – MVC发布复杂对象的列表
- ASP.net没有为某些用户代理生成javascript
- asp.net – Xamarin.Forms应用程序SQL服务器数据
- asp.net-mvc – 使用ASP.NET身份重置密码时的令牌
- asp.net-mvc-3 – 在ASP.NET MVC3中混合基本身份