【发布时间】:2013-06-13 13:05:36
【问题描述】:
我在使用 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() 是绑定到下拉列表的位置。
【问题讨论】:
-
我删除了我的答案,因为我显然错了 - 即使我有赞成票.. 哪个下拉列表给出了问题。
droptopico或dropsub? -
droptopico,谢谢你的帮助,你应该留下你的答案它可以帮助别人它仍然是一个很好的答案。 dropsub 仅在我选择 droptopico 时显示。
标签: c# asp.net dropdownbox