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
}
}