You must Sign In to post a response.
  • Category: .NET

    How to redirect to another page after dropdownbox selected and click on a button

    actually in my project there is requirement like i have two textboxes and adropdownlist box and a button control
    so if enter textbox1 text as ravi and textbox2 text as 123456 and select dropdownlistbox as hr it has to redirect to hr page
    below is my code where i am wrong plz suggest me
    protected void Button1_Click(object sender, EventArgs e)
    {

    if ( DropDownList1.SelectedItem.Text =="HR" && TextBox1.Text =="ravi")
    {
    Server.Transfer("HR.aspx");

    }}
  • #765559
    Hi,

    Try below points :

    1) If you are using ajax call then No, you cannot call Server.Transfer on an ASP.NET AJAX enabled page.

    2) Put a break point at the beginning of your Button1_Click event and confirm it executes the line or not.

    3)Server.Transfer does not update the browser URL, but it redirects correctly.So u if u want to confirm it working or not..Just do the following.

    4)Add this in your hr.aspx page inside body tag pair..

    <body> <h2> HR Page</h2></body>

    4)Try using Response.Redirect() instead of Server.Transfer()
    protected void Button1_Click (object sender, EventArgs e)
    {
    if ( DropDownList1.SelectedItem.Text =="HR" && TextBox1.Text =="ravi")
    {
    Response.Redirect("HR.aspx");
    //or
    Server.Transfer("HR.aspx");
    }
    }

    5)Last but not least before comparing string Trim the values so that the code block executes without fail when string matches
    protected void Button1_Click (object sender, EventArgs e)
    {
    if ( DropDownList1.SelectedItem.Text.Trim() =="HR" && TextBox1.Text.Trim() =="ravi")
    {
    Server.Transfer("HR.aspx");
    }
    }

    Hope it helps!!!

  • #765563
    Hi,
    1. Request you to place debugger and cross check value of DropDownList1.SelectedItem.Text.
    2. Try .ToUpper():
    if (DropDownList1.SelectedItem.Text.Trim().ToUpper() == "HR" && TextBox1.Text.Trim().ToUpper() == "RAVI")

    Hope it will be helpful to you.

  • #765564
    Hi Velidiraviram,

    Have you debug the code? Whether breakpoint enter inside if condition or not?

    You have to remember onething while compare dropdown values don't compare with selectedItem, always try to compare it with appropriate values, it will give you exact value, and while compare the textbox value you need to convert it either lower or upper case and trim the values so that you can match exact result without any issues.

    Ex:

    Protected void Button1_Click(object sender, EventArgs e)
    {
    if ( DropDownList1.SelectedValue ==IndexofHR && TextBox1.Text.Trim().ToLower().ToString() =="ravi")
    {
    Server.Transfer("HR.aspx"); (or)
    Response.Redirect("HR.aspx");
    }
    }

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765747
    Please post the .aspx page code too so that we can check where is the issue. What exactly happens when you click the button? Does it fire the button click event handler?
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #765966
    Hello,

    As per my suggestion please use Response.Redirect() instead of Server.Transfer().
    Please try with the below code :-

    protected void Button1_Click(object sender, EventArgs e)
    {

    if ( DropDownList1.SelectedItem.Text.Trim().ToLower() =="hr" && TextBox1.Text.Trim().ToLower()=="ravi" && TextBox2.Text.Trim().ToLower() == "123456")
    {
    Response.Redirect("HR.aspx");

    }

    }

    Thanks


Sign In to post your comments