简体   繁体   English

在特定行的gridview内启用必需的字段验证器?

[英]Enable required field validator inside gridview in a particular row?

I have implemented Grid View as below lines of code 我已经按照下面的代码行实现了网格视图

<div  class="overflowX">
    <asp:GridView ID="grdView" AutoGenerateColumns="false" BorderWidth="0" OnRowCommand="grdView_RowCommand" runat="server" CssClass="table">
        <Columns>
            <asp:TemplateField HeaderText="Save It">
                <ItemTemplate>
                    <asp:CheckBox ID="chkbox" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Expiration Date">
                <ItemTemplate>
                    <asp:TextBox ID="txtExpirationDate" style="padding:12px 5px;" placeholder="(mm/dd/yyyy)" CssClass="datepiker" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ValReqExpD" Enabled="false" Display="Dynamic" runat="server" ErrorMessage="Expiry Date cannot be Blank." ControlToValidate="txtExpirationDate"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator Display="Dynamic" ID="ValRegExpD" runat="server" ControlToValidate="txtExpirationDate" ErrorMessage="Enter a valid Expiry Date ." ValidationExpression="([1-9]|0[1-9]|1[012])([-/.])([1-9]|0[1-9]|[12][0-9]|3[01])([-/.])(19[5-9][0-9]|20[0-4][0-9])">
                        <b>Enter a valid Renewal Date</b>
                    </asp:RegularExpressionValidator><br />
                    <asp:CompareValidator ID="ValCmpSD" Display="Dynamic" runat="server" ControlToCompare="txtEffectiveDate" ControlToValidate="txtExpirationDate" ErrorMessage="Expiry Date should be greater than Effective date" Operator="GreaterThan" Type="Date"></asp:CompareValidator>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

Now I want that When user will check boxes of a particular row, then the required feild validation "ValReqExpD" should be enabled in that particular row... 现在我想要当用户将选中特定行的复选框时,则应在该特定行中启用所需的字段验证“ ValReqExpD” ...

    protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            CheckBox chk = (CheckBox)e.Row.FindControl("chkbox");

            RequiredFieldValidator rfv = (RequiredFieldValidator)e.Row.FindControl("ValReqED");

            if (chk.Checked == false)
            {
                  rfv.Enabled = false;
            }
        }

    }

I have tried the above code but it is not working !!! 我已经尝试了上面的代码,但是没有用!

Please help me !!! 请帮我 !!!

  1. First you need to add server event on CheckBox control of Change event fire. 首先,您需要 Change事件触发的CheckBox控件上添加服务器事件
  2. Then fire of checked event of Checkbox you need to enable/disable RequiredFieldValidator server control. 然后触发Checkbox的选中事件 ,您需要启用/禁用RequiredFieldValidator服务器控件。

The probelm with your current code is that gridView's RowCommand event is raised when any event like button click etc is raised but since your Checkbox control is not doing anything this event will not trigger anything. 当前代码的问题是,当引发诸如按钮单击等的任何事件时,都会引发gridView的RowCommand事件,但是由于您的Checkbox控件没有执行任何操作,因此该事件将不会触发任何操作。

You can attach a CheckChanged event with your CheckBox control and enable autopostback like this:- 您可以将CheckChanged事件与CheckBox控件一起附加,并启用自动回发,如下所示:-

<asp:CheckBox ID="chkbox" runat="server" OnCheckedChanged="chkbox_CheckedChanged" 
     AutoPostBack="true"/>

Then in the code behind, find the RequiredFieldValidator (present inside the row in which the checkbox is checked) and enable it like this:- 然后在后面的代码中,找到RequiredFieldValidator(在选中该复选框的行中显示)并启用它,如下所示:

protected void chkbox_CheckedChanged(object sender, EventArgs e)
{
   CheckBox chkbox= sender as CheckBox;
   GridViewRow currentRow = chkbox.NamingContainer as GridViewRow;
   RequiredFieldValidator rfv = grdCustomer.Rows[currentRow.RowIndex]
                                      .FindControl("ValReqED") as RequiredFieldValidator;
   if (chkCustomer.Checked)
   {
      rfv .Enabled = true;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM