locked
Dropdownlist and Response.Redirect not working. Please suggest if any. RRS feed

  • Question

  • User496086301 posted

    I amn't sure what's wrong in here. I tried to search the blogs but I cant find the exact scenario.

    Here is what I am trying to do. select a value from dropdown and move to that page and postback.

    1.aspx page

     <asp:DropDownList  ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" EnableViewState='True'>

                   <asp:ListItem>a</asp:ListItem>

                        <asp:ListItem>b</asp:ListItem>

                        <asp:ListItem>c</asp:ListItem>

                        <asp:ListItem>d</asp:ListItem>

           </asp:DropDownList>

    Csfile:

    protected void Page_Load(object sender, EventArgs e)

        {     

            if (!IsPostBack)

            {

                DropDownList1.Enabled = true;           

            }

            else

            {

              DropDownList1.Enabled = false;

               DropDownList1.Attributes.Add("disable", "disable");

                if (this.Session["Template"] == "a ")

                {

                    this.DropDownList1.Attributes.Add("disable", "disable");

                }

            }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "a")
            {
                Response.Redirect("~/a.aspx");
               DropDownList1.Attributes.Add("disable", "disable");
            }

     

    On selecting option ‘a’ redirecting to page a.aspx, after filling the details moves back to page 1.aspx

    Here I want to disable the dropdown so user cant reselect from that particular dropdown. I tried all the above options but nothing worked. Please suggest.

    Thursday, April 25, 2013 1:49 PM

All replies

  • User551462331 posted

    On selecting option ‘a’ redirecting to page a.aspx, after filling the details moves back to page 1.aspx

    how u r moving back to 1.aspx? by using back button of browser?

    if yes, when u click on back button, it does not get the page again from server (no page_load) instead, it gets data from browser cache which had dropdownlist enabled...

    to avoid this, disable back button and then redirect the page.. which can be done via javascript redirection... like below

    protected void Page_Load(object sender, EventArgs e)
        {     
            if (!IsPostBack)
            {
                DropDownList1.Enabled = true;           
            }
            else
            {
              DropDownList1.Enabled = false;
               DropDownList1.Attributes.Add("disable", "disable");
                if (this.Session["Template"] == "a ")
                {
                    this.DropDownList1.Attributes.Add("disable", "disable");
                }
            }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "a")
            {
               DropDownList1.Enabled = false;
               string strScript = "window.location ='a.aspx';"
               ClientScript.RegisterStartupScript(strScript.GetType(),"loadScript", strScript, true)
            }
        }

    let me know if it works?

    hope this helps...

    Thursday, April 25, 2013 2:06 PM
  • User496086301 posted

    Thank you Kedarkulkarni,

    btw that dint help me.

    Yes I am going back using back button.

    a.aspx cscode:

    protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void bnSubmit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/1.aspx");
            
        }

    Now with your suggestion. Its still showing the dropdown to select when coming from a->1.aspx and if I select anything other than 'a' its getting disabled.

    if "a" it goes to a.aspx again.

    Thursday, April 25, 2013 2:40 PM