网格视图验证
Posted
技术标签:
【中文标题】网格视图验证【英文标题】:Gridview Validation 【发布时间】:2011-08-26 04:01:09 【问题描述】:我正在使用 Gridview 并使用项目模板文本框。有 5 个文本框,我在单击添加行 btn 时动态添加新行。在该行中,我添加了所有空的文本框。现在我想验证 btn 上的文本框单击下一步。
现在开始做吗?
我第一次使用必填字段验证来显示文本框,但是当我添加新的行文本框时它不会导致验证,我认为还有其他方法可以对动态添加的文本框进行验证。
如何验证我所有动态添加的文本框
我正在使用的网格视图..
<Columns >
<asp:TemplateField>
<ItemTemplate>
<div class="otherOthersTd">
<asp:Label ID="lblcnt" ForeColor="#136A96" Text='<%#Eval("Count") %>' runat="server" ></asp:Label>
<asp:Label ID="lblId" runat="server" text='<%#Eval("Id") %>' Visible="false"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Name">
<ItemTemplate>
<asp:TextBox ID="txtMName" runat="server" Text='<%# Eval("MName") %>'
Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLName" runat="server" Text='<%# Eval("LName") %>'
Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Degree">
<ItemTemplate>
<asp:TextBox ID="txtDegree" runat="Server" Text='<%# Eval("Degree") %>'
Width="50px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:TextBox ID="txtTitle" runat="Server" Text='<%# Eval("Title") %>' Width="88px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="Server"
Text='<%# Eval("Email") %>' Width="88px" CausesValidation="True" ValidationGroup="a"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpr" runat="server" ErrorMessage="Invalid email id" ControlToValidate="txtEmail" ValidationGroup="a" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:Label ID="revexp" runat="server" > </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Institution">
<ItemTemplate>
<asp:TextBox ID="txtInstitution" runat="server" Text='<%#Eval("Institution") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>
<asp:TemplateField> <ItemTemplate>
<asp:Label ID="lblAuthor" runat="server" Text="Authorerror" Visible="false" ForeColor="Red" Font-Bold="True" Font-Size="X-Large">*</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="false" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle Font-Bold="false" ForeColor="#136A96" />
</asp:GridView>
我的代码...
protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
if (e.CommandName.Equals(""))
lstAuthors = (List<OtherAuthors>)Session["lstAuthors"];
if (lstAuthors.Count == 0)
OtherAuthors obj0 = new OtherAuthors();
obj0.Count = "Author 1:";
lstAuthors.Add(obj0);
int index=GridView1.Rows.Count-1;
for (int i = 0; i < GridView1.Rows.Count; i++)
TextBox Name = GridView1.Rows[i].FindControl("txtName") as TextBox;
TextBox MName = GridView1.Rows[i].FindControl("txtMName") as TextBox;
TextBox LName = GridView1.Rows[i].FindControl("txtLName") as TextBox;
TextBox Degree = GridView1.Rows[i].FindControl("txtDegree") as TextBox;
TextBox Title = GridView1.Rows[i].FindControl("txtTitle") as TextBox;
TextBox Email = GridView1.Rows[i].FindControl("txtEmail") as TextBox;
TextBox Institution = GridView1.Rows[i].FindControl("txtInstitution") as TextBox;
if(Name!=null)
lstAuthors[i].Name = Name.Text;
lstAuthors[i].MName = MName.Text;
lstAuthors[i].LName = LName.Text;
lstAuthors[i].Degree = Degree.Text;
lstAuthors[i].Title = Title.Text;
lstAuthors[i].Email = Email.Text;
lstAuthors[i].Institution = Institution.Text;
OtherAuthors obj1 = new OtherAuthors();
obj1.Count = "Author "+(lstAuthors.Count+1).ToString()+":";
obj1.Name="";
lstAuthors.Add(obj1);
GridView1.DataSource = lstAuthors;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
if (GridView1.Rows.Count - 1 == i)
GridView1.Rows[i].Cells[8].Visible = true;
else
GridView1.Rows[i].Cells[8].Visible = false;
Session["lstAuthors"] = lstAuthors;
MultipleModalitySelect1.hideChosebutton = true;
私有布尔 IsValied() 布尔错误 = 假;
TextBox Name = GridView1.Rows[0].FindControl("txtName") as TextBox;
TextBox MName = GridView1.Rows[0].FindControl("txtMName") as TextBox;
TextBox LName = GridView1.Rows[0].FindControl("txtLName") as TextBox;
TextBox Degree = GridView1.Rows[0].FindControl("txtDegree") as TextBox;
TextBox Title = GridView1.Rows[0].FindControl("txtTitle") as TextBox;
TextBox Email = GridView1.Rows[0].FindControl("txtEmail") as TextBox;
TextBox Institution = GridView1.Rows[0].FindControl("txtInstitution") as TextBox;
Label lblAuthor = GridView1.Rows[0].FindControl("lblAuthor") as Label;
if (Name.Text.Length == 0 || LName.Text.Length == 0 || Degree.Text.Length == 0 ||Title.Text.Length == 0 || Email.Text.Length == 0 || Institution.Text.Length == 0)
lblAuthor.Visible = true;
error = true;
else
lblAuthor.Visible = false;
【问题讨论】:
@Ansari;验证问题是当您在运行时添加控件时?否则它的工作完美?请发布您的完整代码,以便我们解决您的问题。 【参考方案1】:您在“下一步”按钮上使用了验证组的 ValidationGroup="a"
,但尚未在必填字段验证器上使用它。但是您已经在电子邮件验证器中使用了它。
您必须与 Validation 控件保持一致,以及是否对所有控件和按钮控件进行验证以使其正常工作。
<asp:Button ID="btnNext" runat="server" ValidationGroup="a" CausesValidation="true" Text="Next" class="next btn" onclick="btnNext_Click"/>
【讨论】:
【参考方案2】:我认为您应该开始一个新页面并使用一个小得多的案例场景进行测试,然后首先让它发挥作用。然后添加更多的盒子。
如果您仍然需要有关该较小页面的帮助,请发布以寻求帮助 - 我们会更容易理解它。
【讨论】:
你应该在评论中写这个而不是回答。以上是关于网格视图验证的主要内容,如果未能解决你的问题,请参考以下文章