下面是我的页面的输出运行下面的代码后:
我使用ValidatorEnable
客户端验证在我的情况(如果你知道更好的方式来做到这一点,让我知道。)
我的问题是:
IHAVE与复选框并彼此相邻一个下拉列表一个gridview如图所示的屏幕截图。
因此,如果用户在复选框选中它启用了下拉列表可供选择。 如果用户点击提交按钮它验证,并要求用户选择从下拉列表的任何选项。
问题:
但问题是:它验证了所有行,而不是一个我点击。
我怎么能强制执行,以验证特定行用户在选中复选框?
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gv_RowDataBound"
EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName"
SortExpression="LastName" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" />
<asp:DropDownList ID="ddl_PaymentMethod" runat="server">
<asp:ListItem Value="-1">----</asp:ListItem>
<asp:ListItem Value="0">Month</asp:ListItem>
<asp:ListItem Value="1">At End</asp:ListItem>
<asp:ListItem Value="2">At Travel</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server" ControlToValidate="ddl_PaymentMethod"
ErrorMessage="Please select" InitialValue="-1" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox1 = (CheckBox)e.Row.FindControl("checkbox1");
DropDownList ddl_PaymentMethod = (DropDownList)e.Row.FindControl("ddl_PaymentMethod");
ddl_PaymentMethod.Attributes.Add("disabled", "disabled");
checkbox1.Attributes.Add("onclick", "javascript:EnableCheckBox('" + ddl_PaymentMethod.ClientID + "','" + checkbox1.ClientID + "')");
}
}
function EnableCheckBox(ddl, chk) {
var ddl_PaymentMethod = document.getElementById(ddl);
var _chkbox = document.getElementById(chk);
if (_chkbox.checked) {
ddl_PaymentMethod.disabled = false;
validateCheckBox(ddl_PaymentMethod, true);
}
else {
ddl_PaymentMethod.disabled = true;
validateCheckBox(ddl_PaymentMethod, false);
}
}
function validateCheckBox(ddl, state) {
ValidatorEnable(document.getElementById(ddl.id, state));
}