下拉列表始终返回0

时间:2013-06-17 11:17:12

标签: c# asp.net dropdownbox

我的代码出现问题。

我已经看到了一些答案,但我无法让它在我的代码中工作,希望有人可以请求帮助我一直试图解决一段时间。以下是我的代码。

我尝试了回发语句但仍然无效,droptopico总是返回0;我需要这个值,所以我可以从dropsub选择一些东西。

基本上这段代码是为了实现一个用户选择主题的论坛,它将从该主题生成子主题,并显示用户是否有权限,有人可以帮助我......提前感谢。

我在mysql管理上使用Storedprocedure - 它工作得很好所以问题可能就在我认为的绑定中,只是不知道如何解决它...

  using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;

    public partial class About : System.Web.UI.Page
    {  
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());
        public void Page_Load(object sender, EventArgs e)
        {       
            if (Page.IsPostBack == false) 
            {        login();            
                  topico1();
                  subtopico1();
                BindRepeaterData();
                BindRepeaterDataPost();    

            }
         }

            public void login()
    {string loginid = "";
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());          //definição do comando sql
            conn.Open();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = "SP_veruseraposlogin";
            comm.Parameters.AddWithValue("@nome", Page.User.Identity.Name);



            SqlDataReader rdr = comm.ExecuteReader();

            if (rdr.HasRows)
            {

                while (rdr.Read())
                {
                    loginid = rdr["Id"].ToString();


                }

                Lblidlog.Text = loginid;

                conn.Close();

            }
        }
            protected void subtopico1()
            {

                SqlConnection conn3 = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());          //definição do comando sql
                conn3.Open();
                SqlCommand comm3 = conn3.CreateCommand();
                comm3.CommandType = CommandType.StoredProcedure;
                comm3.CommandText = "SP_verutilizadorsubtopicoforum";            
                comm3.Parameters.AddWithValue("@id_user", Lblidlog.Text);
                comm3.Parameters.AddWithValue("@id_top", droptopico.SelectedValue.ToString());

                    SqlDataReader rdrsub = comm3.ExecuteReader();

                dropsub.DataSource = rdrsub;          
                dropsub.DataValueField = "Id_subtopico";
                dropsub.DataTextField = "nome";
                dropsub.DataBind();

                rdrsub.Close();
                conn3.Close();

            }


            protected void topico1()
            {

                SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());          //definição do comando sql
                conn2.Open();
                SqlCommand comm2 = conn2.CreateCommand();
                comm2.CommandType = CommandType.StoredProcedure;
                comm2.CommandText = "SP_verutilizadorTopico";
                comm2.Parameters.AddWithValue("@id_user", Lblidlog.Text);

                SqlDataReader rdrtop = comm2.ExecuteReader();

                    droptopico.DataSource = rdrtop;
                    droptopico.DataValueField = "Id_Topico";
                    droptopico.DataTextField = "nome";
                    droptopico.DataBind();         

                rdrtop.Close();
                conn2.Close();
            }


        protected void BindRepeaterData()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from Mensagem where id_subtopico = @id_sub Order By data desc", conn);
            DataSet ds = new DataSet();

            if (dropsub.SelectedValue != "")
            {
                cmd.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
            }
            else
            {
                cmd.Parameters.AddWithValue("@id_sub", 1);
            }

            SqlDataAdapter dab = new SqlDataAdapter(cmd);

            dab.Fill(ds);
            RepDetails.DataSource = ds;
            RepDetails.DataBind();
            conn.Close();
        }  
        protected void DropsubSelectedIndexChanged(object sender, EventArgs e)
        {
            login();
            topico1();
            BindRepeaterData();
            BindRepeaterDataPost();
        }
        protected void BindRepeaterDataPost()
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT Post.*, Ficheiro.Nome FROM Ficheiro INNER JOIN Post ON Ficheiro.IdFicheiro = Post.IdFicheiro  where Post.id_subtopico = @Id_sub Order By data desc", conn);
            DataSet das = new DataSet();
            if (dropsub.SelectedValue != "")
            {
                cmd.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
            }
            else
            {
                cmd.Parameters.AddWithValue("@id_sub", 1);
            }

            SqlDataAdapter daa = new SqlDataAdapter(cmd);

            daa.Fill(das);
            RepDetailsPost.DataSource = das;
            RepDetailsPost.DataBind();
            conn.Close();
        }

        //Bind Data to Repeater Control

        protected void Button1_Click(object sender, EventArgs e)
        {
            //definição da string de conexão
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());          //definição do comando sql

            conn.Open();
            //Cria uma objeto do tipo comando passando como parametro a string sql e a string de conexão

            //Adicionando o valor das textBox nos parametros do comando
            SqlCommand sqlCommand = conn.CreateCommand();
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlCommand.CommandText = "usp_inserirmsg";

            sqlCommand.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@nome_user", Page.User.Identity.Name);
            sqlCommand.Parameters.AddWithValue("@msg", this.txtmsg.Text);

            //executa o comando com os parametros que foram adicionados acima
            sqlCommand.ExecuteNonQuery();
            this.lblstatusmsg.ForeColor = Color.FromArgb(255, 6, 255, 196);
            lblstatusmsg.Text = "Mensagem enviada";
            conn.Close();
        }
        protected void novocomentario_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            Panel1.Visible = true;
        }

        protected void droptopico_SelectedIndexChanged(object sender, EventArgs e)
        {
            login();
            topico1();
            subtopico1();
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            login();
            topico1();
        }
    }

1 个答案:

答案 0 :(得分:1)

您已在此代码中调用了topico1()函数,此函数再次绑定了下拉列表。下拉值再次刷新。请删除对topico1()函数的调用。

protected void droptopico_SelectedIndexChanged(object sender, EventArgs e)
{
    login();
    //topico1();
    subtopico1();
}