【发布时间】:2016-07-02 07:26:43
【问题描述】:
我希望能够updateGridview 行中的数据row 并确保textbox 字段中的数据是有效输入。但是,使用我的代码,它会检查整个页面是否具有有效输入,并且即使我尝试更新的行中的数据有效,也会阻止我进行更新更改。我的label message 也没有显示error message,以防invalid data。我怎样才能检查我尝试update 的row 是否具有有效输入,如果没有输出error message?
这是我的 HTML 代码:
<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>
这是我的 C# 代码:
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";
}
}
}
}
}
【问题讨论】:
标签: c# asp.net validation gridview updates