验证器甚至不能与验证组一起工作,以确保自定义更新按钮的更新输入是有效的



我希望能够通过rowupdate Gridview行中的数据,并确保textbox字段中的数据是有效输入。然而,与我的代码,它检查是否整个页面有有效的输入,并阻止我进行更新更改,即使在行的数据,我试图更新是有效的。此外,我的label messageinvalid data的情况下不显示error message。我怎样才能检查row,我试图update有有效的输入,如果不输出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";
          }
        }
      }
    }
  }

你必须添加

ValidationGroup

属性到你的链接按钮

<asp:LinkButton ID="yourId" CausesValidation="true" ValidationGroup="UpdatingGrid" runat="server">LinkButton</asp:LinkButton>

不确定,即使把ValidationGroup属性在你的按钮将工作或不作为所有的行将有相同的验证组为他们的控件。您可以尝试使验证组对每行都是唯一的,如输入:

<asp:RequiredFieldValidator 
 ValidationGroup="<%# String.Format("UpdatingGrid{0}", Container.DataItemIndex)%>" ...>
</asp:RequiredFieldValidator>

和on button:

<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.
}

相关内容

  • 没有找到相关文章

最新更新