C#中为什么两个Button_click事件只有一个有效
publicpartialclass_Default:System.Web.UI.Page{stringconnectionString="server=localhos...
public partial class _Default : System.Web.UI.Page
{
string connectionString = "server=localhost;database=nbems;User ID=sa;Password=112233";
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "...“;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "...”;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.DataList2.DataSource = ds;
this.DataList2.DataBind();
}
}
}
程序目的是想点击Button1在datalist1中显示数据库查询结果,点击Button2在datalist2中显示数据库另一个查询结果。
这样的格式有问题吗?为什么只有button1有效 button2点了什么反应都没用呢?也没有报错。为什么如果吧Button2_click事件下面的代码放在Button1_click下面,datalist1和datalist2都能显示正常了呢? 展开
{
string connectionString = "server=localhost;database=nbems;User ID=sa;Password=112233";
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "...“;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.DataList1.DataSource = ds;
this.DataList1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string sql = "...”;
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandTimeout = 180;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.DataList2.DataSource = ds;
this.DataList2.DataBind();
}
}
}
程序目的是想点击Button1在datalist1中显示数据库查询结果,点击Button2在datalist2中显示数据库另一个查询结果。
这样的格式有问题吗?为什么只有button1有效 button2点了什么反应都没用呢?也没有报错。为什么如果吧Button2_click事件下面的代码放在Button1_click下面,datalist1和datalist2都能显示正常了呢? 展开
6个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询