简体   繁体   中英

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.

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. I does happen that even when i select index 0 , the validation message appears briefly and then partial postback takes place . 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.

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

EDIT: Added in TextBox and Button with validation groups; Only ddl1 is validated on SelectedIndexChanged but both ddl1 and txt1 are validated OnClick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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