Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
I have a listbox which reterive values from database and when i select value from lisbox then i want that page redirect to other,but I'm unable to do this,I think my code is perfect but not working.Autopostback property is 'true' but it's not firing the event .

What I have tried:

Here is my aspx code :
ASP.NET
 <asp:ListBox ID="ListBox1" runat="server" Height="202px" Width="262px" 
        style="margin-left: 247px; margin-top: 68px" 
        onselectedindexchanged="ListBox2" AutoPostBack="true" EnableViewState="True">
</asp:ListBox>

and here is my aspx.cs code
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getdata();
        }
    }
    public void getdata()
    {
        string user = Session["name"].ToString();

        SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
        SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
        cnn.Open();
        string id = cmd2.ExecuteScalar().ToString();
        int ID = Int32.Parse(id);
        SqlCommand cmd = new SqlCommand("USE db_compiler SELECT Database_Name FROM  Create_db WHERE User_ID='" + ID + "'", cnn);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.ExecuteNonQuery();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                string value = row["Database_Name"] + "";

                ListBox1.Items.Add(value);
            }

        }
    }
    public void ListBox2(object sender, EventArgs e)
    {
        Session["value"] = ListBox1.SelectedValue;

        Response.Redirect("CreateTable.aspx");
    }
}

Values loaded from database into listbox well,but page not redirect to other.I use break point to confirm that is event firing or not,But event not firing.Thanks
Posted
Updated 9-May-16 20:46pm
Comments
Er. Puneet Goel 10-May-16 2:39am    
i used your code, it works for me. Issue can be related to page attributes. Can you mention complete page html with markups
Karthik_Mahalingam 10-May-16 12:17pm    
Are you using update panel?

1 solution

Please make sure you have following attribute in page markup set to true
ASP.NET
AutoEventWireup="true"
 
Share this answer
 
Comments
Member 12369816 10-May-16 4:59am    
Yes it is included... I check it on the top of the page..
Er. Puneet Goel 10-May-16 5:15am    
So is it throwing any warning ?
Member 12369816 10-May-16 5:15am    
No,when i select value from listbox then automatically page reload on same page,nothing warning
Er. Puneet Goel 10-May-16 5:30am    
you need to provide full page html and code then !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900