我希望能够update
Gridview
行row
中的textbox
数据,并确保label message
字段中的数据是有效输入。但是我的代码是检查整个页面是否有有效输入,并且即使我尝试更新的行中的数据有效,也阻止我更新更新。在error message
的情况下,我的invalid data
也未显示row
。如何检查update
我尝试error message
是否有效输入,如果没有输出<br />
<asp:Label ID="MessageLbl" runat="server"></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text="Editing Table"></asp:Label>
<br />
<asp:GridView ID="GridView4" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView4_RowCommand" OnSelectedIndexChanged="GridView4_SelectedIndexChanged" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton OnClick="UpdateRow_Click" ID="LinkButton1" runat="server" CausesValidation="false" CommandName="UpdateGCommand" Text="Update">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="textBox1" runat="server" Text='<%#Eval("Name")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="UpdatingGrid" ID="rfvName" runat="server" ErrorMessage="Name is a required field" ControlToValidate="textBox1" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="textBox2" runat="server" Text='<%#Eval("Email")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="UpdatingGrid" ID="rfvEmail" runat="server" ErrorMessage="Email is a required field" ControlToValidate="textBox2" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ValidationGroup="UpdatingGrid" ID="RegularExpressionValidatorEmail" runat="server" ErrorMessage="*Invalid Email" ForeColor="Red" ControlToValidate="textBox2" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:TextBox ID="textBox3" runat="server" Text='<%#Eval("Mobile")%>'>
</asp:TextBox>
<asp:RequiredFieldValidator ValidationGroup="UpdatingGrid" ID="rfvMobile" runat="server" ErrorMessage="Mobile is a required field" ControlToValidate="textBox3" Text="*" ForeColor="Red">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
?
这是我的HTML代码:
protected void GridView4_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "UpdateGCommand") {
if (IsPostBack) {
Page.Validate("UpdatingGrid");
while (!Page.IsValid) {
if (Page.IsValid) {
DataSet EditT = new DataSet();
DataSet ValidT = new DataSet();
DataRow row;
if (Session["Edit"] != null) {
EditT = (DataSet) Session["Edit"];
}
if (Session["Valid"] != null) {
ValidT = (DataSet) Session["Valid"];
}
DataTable dtEdit = EditT.Tables[0];
DataTable dtValid = ValidT.Tables[0];
GridViewRow gvr = (GridViewRow)(((LinkButton) e.CommandSource).NamingContainer);
int RowIndex = gvr.RowIndex;
row = dtEdit.Rows[RowIndex];
string txtName = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox1")).Text;
string txtEmail = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox2")).Text;
string txtMobile = ((TextBox) GridView4.Rows[RowIndex].FindControl("textBox3")).Text;
if (txtName != null) {
EditT.Tables[0].Rows[RowIndex]["Name"] = txtName;
}
if (txtEmail != null) {
EditT.Tables[0].Rows[RowIndex]["Email"] = txtEmail;
}
if (txtMobile != null) {
EditT.Tables[0].Rows[RowIndex]["Mobile"] = txtMobile;
}
dtValid.Rows.Add(row.ItemArray);
dtEdit.Rows[RowIndex].Delete();
GridView4.DataSource = EditT;
GridView5.DataSource = ValidT;
GridView4.DataBind();
GridView5.DataBind();
} else {
MessageLbl.Text = "Invalid Input";
}
}
}
}
}
&#13;
这是我的c#代码:
drawRec
&#13;
答案 0 :(得分:0)
您必须添加
的ValidationGroup
也属于你的链接按钮。
<asp:LinkButton ID="yourId" CausesValidation="true" ValidationGroup="UpdatingGrid" runat="server">LinkButton</asp:LinkButton>
答案 1 :(得分:0)
即使将ValidationGroup
属性放在按钮上也不确定,因为所有行都有相同的控件验证组。您可以尝试使验证组对每一行都是唯一的,例如将其放入:
<asp:RequiredFieldValidator
ValidationGroup="<%# String.Format("UpdatingGrid{0}", Container.DataItemIndex)%>" ...>
</asp:RequiredFieldValidator>
以及按钮:
<asp:LinkButton ValidationGroup="<%# String.Format("UpdatingGrid{0}", Container.DataItemIndex)%>"
如果这不起作用,那么您可以点击旧的RowCreated
事件(或RowDataBound
):
GridView1_RowCreated(object sender, GridViewRowEventArgs e){
if(e.Row.RowType != DataControlRowType.DataRow)
return;
var rfem = e.Row.FindControl("rfvEmail") as RequiredFieldValidator;
if(rfem!=null){
rfem.ValidationGroup = "UpdatingGrid" + e.Row.RowIndex;
}
//do same for other validators and button as well.
}