Rectangle 27 0

ASP.NET Control dropdownlist postback programmatically?


if (ddl1.SelectedIndex==2 && ddl2.SelectedIndex>2)
   {
        if (!txt1.Visible)
        {
           txt1.Visible=true;// And then I want to call postback
           //dowork
        }

   }
   else
   {
        if (txt1.Visible)
        {
            txt1.Visible=false;// And then I want to call postback
            //do work
        }
   }

If AutoPostBack = True, which it would have to be for your events to be firing just call a funciton when your condition is met. ASP.NET is always posting back, you just need to handle the condition, otherwise you have to handle the validation with JavaScript and manually post the page:

Now I realize that SelectedIndexChanged event is a server side event. So the postback is required. I probably need JavaScript to do this but do not know how.

Note
Rectangle 27 0

ASP.NET Control dropdownlist postback programmatically?


function Onchange() {
        var ddl1 = document.getElementById('<%= ddl1.ClientID %>');
        var ddl2 = document.getElementById('<%= ddl2.ClientID %>');
        var txtbox = document.getElementById('<%= txtbox.ClientID %>');
        if (ddl1.selectedIndex == 2 && ddl2.selectedIndex > 2) {
            txtbox.style.display = "inline";
            __doPostBack(ddl1, '');
        }
        else {
            txtbox.style.display = "none";
            return false;
        }
    }
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddl1" onchange="return Onchange();"
            OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
            <asp:ListItem Text="text1" />
            <asp:ListItem Text="text2" />
            <asp:ListItem Text="text3" />
            <asp:ListItem Text="text4" />
        </asp:DropDownList>
        <asp:DropDownList runat="server" AutoPostBack="true" ID="ddl2" onchange="return Onchange();"
            OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
            <asp:ListItem Text="text1" />
            <asp:ListItem Text="text2" />
            <asp:ListItem Text="text3" />
            <asp:ListItem Text="text4" />
        </asp:DropDownList>
        <asp:TextBox runat="server" ID="txtbox" />

I am not sure if i understand your problem but you want to achieve postback only if certain condition is met. you can hook up a javascript function on both dropdown onchange="return onchange();" Set Autopostback = true;

Note