locked
Set dropdown selectedIndex RRS feed

  • Question

  • User-909867351 posted

    I need to download one word document and after that I need to put the selectedIndex from my dropdown to 0

    The problem is that if I download the file the code after is not executed therefore I can't put the selectedIndex to 0 

    here is my code in agenda.aspx:

    Response.Redirect("https://multiagenda.programamos.pt/documentos_criados/doc" + id + ".docx");
            duty.SelectedIndex = 0;       
            Response.Redirect("agenda.aspx");

    The name of dropdownlist is duty and I need to Response.Redirect to refresh the page to put duty.SelectecIndex to 0 

    Monday, September 2, 2019 11:38 AM

Answers

  • User-719153870 posted

    Hi mariolopes,

    the code after is not executed

    Response.Redirect("url") will discard all output of the current buffer and redirect directly to the specified page.

    Here are two ways i can find for you to solve this problem.

    One is that you can open a new window in your back code to do the download job and the code behind will not need to be changed and they will execute like you wish.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True" SelectCommand="select * from Options"></asp:SqlDataSource>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataValueField="QID" DataTextField="Answer" DataSourceID="SqlDataSource1"></asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

    .CS:( in this part of code, you can refer to Open New Window from Server Side (Code Behind) in ASP.Net using C# and VB.Net)

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string url = @"files/Asp.Net MVC.docx";
                string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
                ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
                DropDownList1.SelectedIndex = 0;;
            }

    The result of this demo:

    Notice: the weakness of this way is that as you can see, a new window will be opened, thus you might need to code to close it.

    Another way is like mgebhard said, you can do this job in your JS code.

    Please refer to below code:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function Reset() {
                window.open("files/Asp.Net MVC.docx");
                document.getElementById('DropDownList1').selectedIndex=0; 
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True" SelectCommand="select * from Options"></asp:SqlDataSource>
                <asp:DropDownList onchange="JavaScript:Reset()" ID="DropDownList1" runat="server" DataValueField="QID" DataTextField="Answer" DataSourceID="SqlDataSource1"></asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

    The result of this demo:

    Notice: This way is much better than the before one, however it will limit your code for your other movements.

    Please choose whichever suitable for you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2019 3:01 AM
  • User475983607 posted

    A user control makes no difference whatsoever. 

    Your new code suffers from an issue similar to your first.  You are trying to return an HTML response and redirect. You can't do both.

    string url = "https://multiagenda.programamos.pt/documentos_criados/doc" + id + ".docx";
    string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";        
    Page.ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);       
    duty.SelectedIndex = 0;       
    Response.Redirect("agenda.aspx");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 4, 2019 8:32 PM

All replies

  • User475983607 posted

    This is the expected behavior.  You can either download HTML to the browser or download a file.  You cannot do both.  

    You need to write JavaScript to set the select then redirect.

    Monday, September 2, 2019 12:31 PM
  • User-719153870 posted

    Hi mariolopes,

    the code after is not executed

    Response.Redirect("url") will discard all output of the current buffer and redirect directly to the specified page.

    Here are two ways i can find for you to solve this problem.

    One is that you can open a new window in your back code to do the download job and the code behind will not need to be changed and they will execute like you wish.

    Please refer to below demo:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True" SelectCommand="select * from Options"></asp:SqlDataSource>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataValueField="QID" DataTextField="Answer" DataSourceID="SqlDataSource1"></asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

    .CS:( in this part of code, you can refer to Open New Window from Server Side (Code Behind) in ASP.Net using C# and VB.Net)

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string url = @"files/Asp.Net MVC.docx";
                string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
                ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
                DropDownList1.SelectedIndex = 0;;
            }

    The result of this demo:

    Notice: the weakness of this way is that as you can see, a new window will be opened, thus you might need to code to close it.

    Another way is like mgebhard said, you can do this job in your JS code.

    Please refer to below code:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function Reset() {
                window.open("files/Asp.Net MVC.docx");
                document.getElementById('DropDownList1').selectedIndex=0; 
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True" SelectCommand="select * from Options"></asp:SqlDataSource>
                <asp:DropDownList onchange="JavaScript:Reset()" ID="DropDownList1" runat="server" DataValueField="QID" DataTextField="Answer" DataSourceID="SqlDataSource1"></asp:DropDownList>
            </div>
        </form>
    </body>
    </html>

    The result of this demo:

    Notice: This way is much better than the before one, however it will limit your code for your other movements.

    Please choose whichever suitable for you.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 3, 2019 3:01 AM
  • User-909867351 posted

    Thank you Yang

    works fine

     string url = "https://aaaa.bbbb.pt/documentos_criados/doc" + id + ".docx";
    string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes')";
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "script", s, true); duty.SelectedIndex = 0;

    thank you

    Wednesday, September 4, 2019 7:33 PM
  • User475983607 posted

    A user control makes no difference whatsoever. 

    Your new code suffers from an issue similar to your first.  You are trying to return an HTML response and redirect. You can't do both.

    string url = "https://multiagenda.programamos.pt/documentos_criados/doc" + id + ".docx";
    string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";        
    Page.ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);       
    duty.SelectedIndex = 0;       
    Response.Redirect("agenda.aspx");

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 4, 2019 8:32 PM