C#从下拉选项填充网格视图
本文关键字:填充 网格 视图 选项 | 更新日期: 2023-09-27 17:58:30
我尝试了所有方法,但不知道如何用下拉选择填充网格视图。我已经检查了下拉列表的值,它是正确的,网格视图将显示所有项目,但不显示我想要的项目。我正在使用一个web表单。下面是我的代码。
protected void Page_Load(object sender, EventArgs e)
{
Populate();
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection.Open();
try
{
SqlDataReader reader = null;
string serverIP = drpChoose.SelectedItem.Value.ToString();
SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = " + serverIP, myConnection);
reader = cmd.ExecuteReader();
while (reader.Read())
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
myConnection.Close();
}
public void Populate()
{
SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection1.Open();
SqlCommand cmd1 = new SqlCommand("SELECT ServerIP FROM Servers", myConnection1);
SqlDataReader dropReader;
dropReader = cmd1.ExecuteReader();
drpChoose.DataSource = dropReader;
drpChoose.DataTextField = "ServerIP";
drpChoose.DataValueField = "ServerIP";
drpChoose.DataBind();
}
}
如有任何帮助,我们将不胜感激。
您还应该应用sql参数化。它将有助于防止SQL注入攻击。您的代码当前易受攻击。
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx
您不需要在每次发布后重新绑定下拉列表,否则您将丢失所选项目。尝试以下操作:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
Populate();
}
}
protected void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection.Open();
try
{
SqlDataReader reader = null;
string serverIP = drpChoose.SelectedItem.Value.ToString();
SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = " + serverIP, myConnection);
reader = cmd.ExecuteReader();
while (reader.Read())
{
GridView1.DataSource = reader;
GridView1.DataBind();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
myConnection.Close();
}
public void Populate()
{
SqlConnection myConnection1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
myConnection1.Open();
SqlCommand cmd1 = new SqlCommand("SELECT ServerIP FROM Servers", myConnection1);
SqlDataReader dropReader;
dropReader = cmd1.ExecuteReader();
drpChoose.DataSource = dropReader;
drpChoose.DataTextField = "ServerIP";
drpChoose.DataValueField = "ServerIP";
drpChoose.DataBind();
}
}
只是一个指南:为什么不使用Ajaxtoolkit UpdatePanel控件。将Gridview放在UpdatePanel中,然后将下拉选择设置为Gridview的Updatetrigger。这样您就不必担心回发和值了。
将Page_Load方法更改为:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack()) {
Populate();
}
}
每次加载页面时,都会重新绑定下拉选择,以便始终设置默认的"all"值。通过使用Page.IsPostBack属性,您告诉页面只在第一次加载时绑定,而不在随后的回发时绑定。通过使用此属性,在回发时下拉列表中选定的项目将不会更改。
MSDN:Page.IsPostBack
更新
关于您对SQL语句中"Where"子句的评论,serverIP是否以文本形式存储在数据库中?尝试用撇号包装serverIP,如:
SqlCommand cmd = new SqlCommand("SELECT * from ScheduledTasks WHERE ServerIP = '" + serverIP + "'', myConnection);