在回发c#asp后,下拉列表显示为空

时间:2013-06-13 13:05:37

标签: c# asp.net dropdownbox

我在使用ASP和C#时遇到问题我有一个带有2个下拉列表的表单,一个显示主题,另一个显示子主题,现在我添加了一个StoredProcedure,用于检查用户是否有权查看该主题,如果是这样的话添加到下拉列表中,它工作正常,但当我选择一个主题时,它只返回第一个选项,任何人都知道我能做什么?我读过关于Postback因为绑定把我似乎不能让它在这里工作是我的代码...我也尝试过使用sqladapter同样的问题......

存储过程

USE [Forum]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_verutilizadorTopico]

    (

    @id_user int
    )

AS
    SELECT UtilizadorTopico.IdTopico, Topico.Id_topico, Topico.Nome FROM UtilizadorTopico INNER JOIN Topico ON UtilizadorTopico.IdTopico = Topico.Id_topico WHERE (UtilizadorTopico.IdUtilizador = @id_user )
    RETURN

和我的C#代码

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
{
    string loginid = "";
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());
    public void Page_Load(object sender, EventArgs e)
    {       
        if (!Page.IsPostBack) 
        {           
            topico1();
            BindRepeaterData();
            BindRepeaterDataPost();
        }

        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 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();
        }
    }

    public void topico1()
    {
        SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());          //definição do comando sql
        conn2.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 comm2 = conn2.CreateCommand();
        comm2.CommandType = CommandType.StoredProcedure;
        comm2.CommandText = "SP_verutilizadorTopico";
        comm2.Parameters.AddWithValue("@id_user", loginid);
        SqlDataReader rdrtop = comm2.ExecuteReader();

        droptopico.DataSource = rdrtop;
        droptopico.DataTextField = "Nome";
        droptopico.DataValueField = "Id_topico";
        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 da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        RepDetails.DataSource = ds;
        RepDetails.DataBind();
        conn.Close();
    }

    protected void DropsubSelectedIndexChanged(object sender, EventArgs e)
    {
        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", loginid);
        comm2.Parameters.AddWithValue("@id_topico", droptopico.SelectedValue);
        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 ds = new DataSet();
        if (dropsub.SelectedValue != "")
        {
            cmd.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
        }
        else
        {
            cmd.Parameters.AddWithValue("@id_sub", 1);
        }

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        da.Fill(ds);
        RepDetailsPost.DataSource = ds;
        RepDetailsPost.DataBind();
        conn.Close();
    }

    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();
        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);
        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;
    }




}

Topico1()绑定到下拉列表。

1 个答案:

答案 0 :(得分:0)

将以下代码放入函数

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 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();
    }

并在里面调用此函数     如果(!Page.IsPostBack)