【问题标题】:Validators are not working even with validation group to make sure that update input of custom update button is valid验证器即使与验证组一起工作也无法确保自定义更新按钮的更新输入有效
【发布时间】:2016-07-02 07:26:43
【问题描述】:

我希望能够updateGridview 行中的数据row 并确保textbox 字段中的数据是有效输入。但是,使用我的代码,它会检查整个页面是否具有有效输入,并且即使我尝试更新的行中的数据有效,也会阻止我进行更新更改。我的label message 也没有显示error message,以防invalid data。我怎样才能检查我尝试updaterow 是否具有有效输入,如果没有输出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


    【解决方案1】:

    你必须添加

    验证组

    也归因于您的链接按钮。

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

    【讨论】:

      【解决方案2】:

      不确定是否将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.
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-07
        • 2014-06-10
        • 2011-01-05
        • 2018-07-30
        • 1970-01-01
        • 2021-03-31
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多