这是我的项目我有一个列表视图。显示文章的评论或手写。 我有两个sqldatasource,第2个(文章)和3个(手写)。 我想在页面加载时更改数据源ID并显示其他注释。 默认listview datasourceid是sqldatasource2并且它可以工作,但是当将sqldatasource2更改为sqldatasource3时它们不起作用。 这是sqldatasource2的选择命令
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:myconnection_sql %>" SelectCommand="SELECT [name], [date], [text]
FROM [comment] WHERE ([idarticle] = @idarticle)">
这是sqldatasource3的选择命令
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$
ConnectionStrings:myconnection_sql %>" SelectCommand="SELECT [name], [date], [text]
FROM [comment] WHERE ([idhandwritten] = @idarticle)">
这是page_load代码:
protected void Page_Load(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(Request.QueryString.Get("id"));
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings
["myconnection_sql"].ConnectionString);
SqlCommand cmd = new SqlCommand("select type from article_handwritten where
id=@id");
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
da.SelectCommand = cmd;
da.Fill(dt);
string type = dt.Rows[0]["type"].ToString();
if (type == "Article")
{
ListView1.DataSourceID = "SqlDataSource2";
ListView1.DataBind();
}
else if (type == "handwritten")
{
ListView1.DataSourceID = "SqlDataSource3";
ListView1.DataBind();
}
con.Close();
}
catch
{
}