Hi Everyone I've been looking for a solution to this problema for a while, ive searched the net but still can't get this too work. if i put my code in the default asp template( http://i1.asp.net/asp.net/images/whitepapers/aspnet40/ASPNET4B1-image13.png?cdn_id=2013-05-08-001 ) it will work, but when i use my template i always get the listbox Selecetedindex as -1.
here is a example of what happens when i try to inspect the value using chrome. http://s21.postimg.org/3jsi8351z/weird.png
Someone please help me.
here is my code.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;

public partial class permissaotop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }  
    public void Droptopicoperfil_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=Server;Initial Catalog=Forum;uid=user;pwd=password;Connect Timeout=10;TrustServerCertificate=True ");
        conn.Open();


        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "SP_vertopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);

        SqlDataReader rdr = comm.ExecuteReader();

        lbpermitidotp.DataSource = rdr;
        lbpermitidotp.DataTextField = "Nome";
        this.lbpermitidotp.DataValueField = "Id";


        lbpermitidotp.DataBind();

        rdr.Close();
        conn.Close();

        conn.Open();

        SqlCommand comm1 = conn.CreateCommand();
        comm1.CommandType = CommandType.StoredProcedure;
        comm1.CommandText = "[SP_naotopicoperfil]";

        comm1.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);
        SqlDataReader rdr1 = comm1.ExecuteReader();


        lbproibidotp.DataSource = rdr1;
        lbproibidotp.DataTextField = "Nome";
        this.lbproibidotp.DataValueField = "Id";

        lbproibidotp.DataBind();

        rdr1.Close();
        conn.Close();

    }
    protected void BtnnaopermitirClick(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP\\NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True ");

        conn.Open();
        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "usp_inserirtopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);             
        comm.Parameters.AddWithValue("@id_perfil", this.lbproibidotp.SelectedValue.ToString());


        SqlDataReader rdr = comm.ExecuteReader();

        if (this.lbproibidotp.SelectedIndex >= 0)
        {
            this.lbproibidotp.Items.RemoveAt(this.lbproibidotp.SelectedIndex);
        }

        lbpermitidotp.DataBind();
        lbproibidotp.DataBind();
        Droptopicoperfil.DataBind();

        rdr.Close();
        conn.Close();

        Droptopicoperfil_SelectedIndexChanged(this, null);
    }
    protected void BtnpermitirClick(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection("Data Source=Microsoft SQL Server (SqlClient);Server=NEVETS-LAPTOP\\NEVETS;Initial Catalog=Forum;uid=sa;pwd=sql;Connect Timeout=10;TrustServerCertificate=True ");
        conn.Open();

        SqlCommand comm = conn.CreateCommand();
        comm.CommandType = CommandType.StoredProcedure;
        comm.CommandText = "SP_apagartopicoperfil";

        comm.Parameters.AddWithValue("@id_topico", this.Droptopicoperfil.SelectedValue);

        comm.Parameters.AddWithValue("@id_perfil", lbpermitidotp.SelectedValue.ToString());


        SqlDataReader rdr = comm.ExecuteReader();

        lbpermitidotp.DataSource = rdr;

        if (this.lbpermitidotp.SelectedIndex >= 0)
        {
            this.lbpermitidotp.Items.RemoveAt(this.lbpermitidotp.SelectedIndex);
        }

        lbpermitidotp.DataBind();
        lbproibidotp.DataBind();
        Droptopicoperfil.DataBind();

        rdr.Close();
        conn.Close();

        Droptopicoperfil_SelectedIndexChanged(this, null);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    //testing listbox
        Label3.Text =lbproibidotp.SelectedIndex.ToString();
        //Always gets the result -1

    }
}

As you can see in the image link i posted i cant get the values from the listbox, its like it doesn't select nothing when i highlight a item and click the button its always -1 .... i heard it could be the binding i have, but i couldnt fix this yet... anyone know how ?

Your control has the wrong ID.

Your dropdown in code is called: lbproibidotp but on the page it's called Content_lbproibidotp. They have to match.

@ketsuekiame, thanks for your answer, i've seen that, but when i try to change the id i get a error(Error 5 The name 'Content_lbproibidotp' does not exist in the current context), i think it says content_lbproibidotp because its in a panel. anything else i can do ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.