我已将Grid View实现为以下代码行
<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>
现在我想要当用户检查特定行的方框时,应该在该特定行中启用所需的字段验证“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;
}
}
}
我已经尝试了上面的代码,但它无法正常工作!!!
请帮帮我!!!
答案 0 :(得分:0)
答案 1 :(得分:0)
使用当前代码的问题是,当按钮点击等任何事件被引发时,会引发gridView的RowCommand
事件,但由于您的Checkbox控件没有执行任何操作,因此该事件不会触发任何内容。 / p>
您可以使用CheckChanged
控件附加CheckBox
个事件并启用此类自动回复: -
<asp:CheckBox ID="chkbox" runat="server" OnCheckedChanged="chkbox_CheckedChanged"
AutoPostBack="true"/>
然后在后面的代码中,找到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;
}
}