C# 带有验证器的动态WebUserControl UpdatePanel[ASP.NET]
C# 带有验证器的动态WebUserControl UpdatePanel[ASP.NET],c#,asp.net,ajax,updatepanel,C#,Asp.net,Ajax,Updatepanel,我有这个问题: Add-in UpdatePanel带有RequiredFieldValidator的文本框。 然后在回发时将值添加到DB并重定向到其他页面。 我的问题是,为什么当我添加一个MyControl并单击GO按钮时,ValidationSummary不会停止回发,为什么在第二次单击ValidationSummary火? 第二个问题是为什么VisualStudio找不到MyControl类(用红色下划线),但在编译结果时没有错误 代码如下: Default.aspx 测试验证器
我有这个问题:
Add-in UpdatePanel带有RequiredFieldValidator的文本框。
然后在回发时将值添加到DB并重定向到其他页面。
我的问题是,为什么当我添加一个MyControl并单击GO按钮时,ValidationSummary不会停止回发,为什么在第二次单击ValidationSummary火? 第二个问题是为什么VisualStudio找不到MyControl类(用红色下划线),但在编译结果时没有错误 代码如下: Default.aspx
测试验证器
Default.aspx.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的覆盖无效LoadViewState(对象保存状态)
{
base.LoadViewState(savedState);
列表m_oItems=新列表();
if(ViewState[“ItemsAdded”]!=null)
{
m_oItems=(列表)视图状态[“项目添加”];
}
ViewState[“ItemsAdded”]=m_oItems;
foreach(以m_oItems为单位的int m_oItem)
{
MyControl m_MyControl=(MyControl)LoadControl(“MyControl.ascx”);
phValidators.Controls.Add(m_-mycontrol);
}
}
受保护的void cmdAdd_单击(对象发送方,事件参数e)
{
列表m_oItems=新列表();
if(ViewState[“ItemsAdded”]!=null)
{
m_oItems=(列表)视图状态[“项目添加”];
}
m_oItems.Add(m_oItems.Count);
ViewState[“ItemsAdded”]=m_oItems;
MyControl m_MyControl=(MyControl)LoadControl(“MyControl.ascx”);
phValidators.Controls.Add(m_-mycontrol);
}
受保护的void cmdGo\u单击(对象发送方,事件参数e)
{
如果(!Page.IsValid)
回来
//添加到数据库
响应。重定向(“http://www.google.it/");
}
}
MyControl.ascx
多谢各位
达米亚诺
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Validators</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upAdd" runat="server">
<ContentTemplate>
<asp:Button ID="cmdAdd" runat="server" Text="Add" onclick="cmdAdd_Click" CausesValidation="false" />
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:UpdatePanel ID="upValidators" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="phValidators" runat="server"></asp:PlaceHolder>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmdAdd" />
</Triggers>
</asp:UpdatePanel>
<hr />
<asp:Button ID="cmdGo" runat="server" Text="Go" onclick="cmdGo_Click" />
</div>
<asp:ValidationSummary ID="valSum" HeaderText="Errors:" ShowSummary="False" ShowMessageBox="True" EnableClientScript="True" DisplayMode="BulletList" runat="server"></asp:ValidationSummary>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
ViewState["ItemsAdded"] = m_oItems;
foreach (int m_oItem in m_oItems)
{
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
}
protected void cmdAdd_Click(object sender, EventArgs e)
{
List<int> m_oItems = new List<int>();
if (ViewState["ItemsAdded"] != null)
{
m_oItems = (List<int>)ViewState["ItemsAdded"];
}
m_oItems.Add(m_oItems.Count);
ViewState["ItemsAdded"] = m_oItems;
MyControl m_oMyControl = (MyControl)LoadControl("MyControl.ascx");
phValidators.Controls.Add(m_oMyControl);
}
protected void cmdGo_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
//Add To DB
Response.Redirect("http://www.google.it/");
}
}
<asp:TextBox ID="txtLanguage" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rqrLanguage" runat="server" ErrorMessage="Language not set" ControlToValidate="txtLanguage"></asp:RequiredFieldValidator>
<br />