验证GridView控件在编辑状态下输入的数据是否合法(不合法,不允许更新,同时弹出提示框)
ReadOnly="true",表示该列不能被编辑
页面设计:
<table style="border:solid 1px green;width:40%">
<tr>
<td style="background-color:Green"><strong><span style="font-size: 14pt; color: #ffffff">验证GridView控件中编辑列的值</span></strong>
</td>
</tr>
<tr>
<td>
<asp:GridView Width="100%" ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductName" ReadOnly="True" HeaderText="商品名称"/>
<asp:TemplateField>
<HeaderTemplate>
<table width="100%">
<tr>
<td align="left" width="60%" >数量</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td width="20%"><%# Eval("UnitsInStock")%></td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td width="20%"><asp:TextBox runat="Server" ID="t1" Text='<%# Eval("UnitsInStock")%>' Width="90%"></asp:TextBox></td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="操作" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
后台代码:
“编辑”事件处理代码:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1; // 获取编辑行的索引值
band(); // 会绑数据
}
当用户进入编辑状态,在文本框中输入内容后,“更新”,会触发 RowDataBound事件进行校验用户输入的数据,代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState & DataControlRowState.Edit)==DataControlRowState.Edit)
{
RequiredFieldValidator re = new RequiredFieldValidator(); // 创建一个非空验证控件
re.ControlToValidate = "t1";
re.ErrorMessage = "销售量不能为空";
re.Display = ValidatorDisplay.Dynamic;
re.Text = "*";
e.Row.Cells[1].Controls.Add(re); // 将该空间添加到GridView控件中
RangeValidator ra = new RangeValidator();
ra.ControlToValidate = "t1";
ra.Type = ValidationDataType.Integer;
ra.Display = ValidatorDisplay.Dynamic;
ra.MaximumValue = "200";
ra.MinimumValue = "10";
ra.Text = "*";
ra.ErrorMessage = "销售量的范围为10-200";
e.Row.Cells[1].Controls.Add(ra);
ValidationSummary va = new ValidationSummary(); // 创建一个范围验证控件
va.ShowSummary = false;
va.ShowMessageBox = true;
e.Row.Cells[1].Controls.Add(va); // 添加到GridView中
}
}
验证无误后,触发 RowUpdating 事件,代码如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["val"].ConnectionString);
con.Open();
string aa = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string bb = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("t1")).Text.ToString();
string cc = "update products set UnitsInStock='" + bb + "' where ProductID='" + aa +"'";
SqlCommand cmd = new SqlCommand(cc, con);
cmd.ExecuteNonQuery();
con.Close();
this.GridView1.EditIndex = -1;
band();
Response.Write("<script>alert('更新成功!')</script>");
}