gpt4 book ai didi

javascript - 如何控制 ASP.NET Validator Controls 客户端验证

转载 作者:行者123 更新时间:2023-11-29 10:50:50 25 4
gpt4 key购买 nike

下面是运行以下代码后我的页面的输出:

我在我的情况下使用 ValidatorEnable 客户端验证(如果您知道更好的方法,请告诉我。)

我的问题是:

我有一个带有复选框的 GridView 和一个下拉列表,如屏幕截图所示。

因此,如果用户选中复选框,它会启用下拉列表以供选择。如果用户点击提交按钮,它会验证并要求用户从下拉列表中选择任何选项。

问题:

但问题是:它验证所有行而不是我单击的行。

我如何强制验证用户在复选框上选中的特定行?

enter image description here

    <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));
}

最佳答案

让它为你工作...... GridView :

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"  OnRowDataBound="gv_RowDataBound">
<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="drpPaymentMethod" 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="rfv" InitialValue="-1" ControlToValidate="drpPaymentMethod" Enabled="false" Display="Static" runat="server" ErrorMessage="RequiredFieldValidator"></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>

CS:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox1 = e.Row.FindControl("checkbox1") as CheckBox;
RequiredFieldValidator rfv = e.Row.FindControl("rfv") as RequiredFieldValidator;
DropDownList drpPaymentMethod = (DropDownList)e.Row.FindControl("drpPaymentMethod");
// you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element
checkbox1.Attributes.Add("onclick", "UpdateValidator('" + checkbox1.ClientID + "','" + drpPaymentMethod.ClientID + "','" + rfv.ClientID + "');");
if (!checkbox1.Checked)
drpPaymentMethod.Attributes.Add("disabled", "disabled");
}
}

JavaScript:

function UpdateValidator(chkID, drpID, validatorid) {
//enabling the validator only if the checkbox is checked
var enableValidator = $("#" + chkID).is(":checked");

if (enableValidator)
$('#' + drpID).removeAttr('disabled');
else
$('#' + drpID).attr('disabled', 'disabled');

var vv = $('#' + validatorid).val();

ValidatorEnable(document.getElementById(validatorid), enableValidator);
}

关于javascript - 如何控制 ASP.NET Validator Controls 客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10566599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com