C#从下拉选项中填充Gridview

时间:2011-06-03 16:38:13

标签: c# asp.net gridview webforms drop-down-menu

我已尝试过所有内容,但无法弄清楚如何使用我的下拉选项填充gridview。我已经检查了下拉列表的值,它是正确的,gridview将显示所有项目,但不显示我想要的项目。我正在使用网络表单。以下是我的代码。

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

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

您还应该应用sql参数化。它将有助于防止SQL注入攻击。您的代码目前容易受到哪些攻击。

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

答案 1 :(得分:0)

您不需要在每个帖子后重新绑定下拉列表,否则您将丢失所选项目。试试以下内容:

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

答案 2 :(得分:0)

将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);

答案 3 :(得分:0)

只是一个指南:为什么不使用Ajaxtoolkit UpdatePanel控件。 将Gridview放在UpdatePanel中,然后将下拉选项设置为Gridview的Updatetrigger。然后您不必担心回发和值。