简体   繁体   English

在updatepanel中将AutoPostBack设置为true的DropdownList验证

[英]DropdownList validation with AutoPostBack set to true inside updatepanel

there are many posts that deal with validation controls inside update panel and partial page rendering. 有很多帖子涉及更新面板和部分页面渲染中的验证控件。 But i got a different problem here, i did try updating to sp1 .NET framework 2.0 and again .NET Framework 4.0 but nothing happens. 但是我在这里遇到了一个不同的问题,我确实尝试更新到sp1 .NET Framework 2.0,然后又更新到.NET Framework 4.0,但是什么也没有发生。

Basically i got a dropdownlist inside update panel whose autopostback is set to true and an empty item -- Select -- is added as index 0 for validation ( Required Field Validator ) purpose. 基本上我在更新面板中有一个dropdownlist,其autopostback设置为true,并且一个空项目-- Select --被添加为index 0以进行验证( Required Field Validator )。 I does happen that even when i select index 0 , the validation message appears briefly and then partial postback takes place . 我确实发生了即使我选择索引0时, 验证消息也会短暂出现然后发生部分回发的情况 Does anyone have any reasons for the same or alternate ways to do this. 是否有人有相同或替代方式的原因。

note: 注意:

I am populating other controls (dropdownlist) during the selected index changed event. 在选定的索引更改事件期间,我正在填充其他控件(下拉列表)。 I could use cascading dropdownlist from AjaxControlToolkit but then i lose event validation functionality that other controls need. 我可以使用AjaxControlToolkit级联下拉列表,但随后我失去了其他控件所需的事件验证功能。

why not validating client choise in code behind ? 为什么不验证后面代码中的客户选择?

for ex' : 对于前':

if (ddlName.selectedValue == "-1")
{
    lblErr.text = "You have to select...";
    lblErr.visible = true;
}

As a quick test I've come up with this, which works (for me): 作为一个快速测试,我想出了这个方法(对我而言):

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="ddl1" InitialValue="0" ValidationGroup="DDLOnly">*</asp:RequiredFieldValidator>
        <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="test" CausesValidation="true" ValidationGroup="DDLOnly">
            <asp:ListItem Value="0">---Select---</asp:ListItem>
            <asp:ListItem Value="1">Option1</asp:ListItem>
            <asp:ListItem Value="2">Option2</asp:ListItem>
            <asp:ListItem Value="3">Option3</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddl2" runat="server">
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="rfvTxt" runat="server" ControlToValidate="txt1" ValidationGroup="WholePage">*</asp:RequiredFieldValidator>
        <asp:TextBox ID="txt1" runat="server" ValidationGroup="WholePage"></asp:TextBox>
        <asp:Button ID="btn1" runat="server" Text="Button" OnClientClick="return Page_ClientValidate();" OnClick="btn" />
    </ContentTemplate>
</asp:UpdatePanel>

And in the code behind: 并在后面的代码中:

protected void test(object sender, EventArgs e)
{
    ddl2.Items.Clear();
    for (int i = 0; i < 4; i++)
        ddl2.Items.Add(new ListItem("Test" + ddl1.SelectedIndex));
}

Populates the second DDL when any option is chosen, but not for the initial item of 0 选择任何选项时填充第二个DDL,但不填充初始项0

EDIT: Added in TextBox and Button with validation groups; 编辑:TextBox和带有验证组的Button添加; Only ddl1 is validated on SelectedIndexChanged but both ddl1 and txt1 are validated OnClick ddl1SelectedIndexChanged上经过验证,但ddl1txt1均在OnClick上经过验证

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

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