0

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.

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

2 Answers2

0

why not validating client choise in code behind ?

for ex' :

if (ddlName.selectedValue == "-1")
{
    lblErr.text = "You have to select...";
    lblErr.visible = true;
}
barak ben horin
  • 494
  • 1
  • 4
  • 12
0

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

Iain
  • 13
  • 1
  • 2
  • 7
  • @lain it should work because `causes` validation is enabled. I can't do that because that would now validate other controls on the page which u haven't tried with. – Deeptechtons Jan 18 '12 at 12:45
  • @Deeptechtons - What about adding your DDL to it's own validation group? You can use JavaScript to trigger the whole page's validation group and the one for the DDL from your submit button. [link](http://stackoverflow.com/a/984289/1038027) – Iain Jan 18 '12 at 12:59
  • @lain that is kind of hack isn't it? so there isn't any straight solution – Deeptechtons Jan 18 '12 at 14:45
  • @Deeptechtons :-) I wouldn't call it a 'hack' personally, the `Page_ClientValidate()` is there to be used, and you can only specify one event handler on a button. You could write your own Validate functions by calling an individual `Validator` in JavaScript [See here](http://sandblogaspnet.blogspot.com/2009/04/calling-validator-controls-from.html) rather than validating the whole page. – Iain Jan 18 '12 at 14:55