C# 从ASP.Net代码中获取/设置文本中的选择框

C# 从ASP.Net代码中获取/设置文本中的选择框,c#,asp.net,forms,pass-by-value,C#,Asp.net,Forms,Pass By Value,下面的代码被添加到我表单中的文字中。如何在代码隐藏中从select=name=“populationSelect”获取/设置数据 protectedvoid PopulatePopulation() { StringBuilder sb=新的StringBuilder(); StringBuilder sql=新的StringBuilder(); //定义sql 追加(“选择pid,填充”); 追加(“来自人口”); 追加(“按pid ASC排序”); 使用(IDataReader=SqlHel

下面的代码被添加到我表单中的文字中。如何在代码隐藏中从select=name=“populationSelect”获取/设置数据

protectedvoid PopulatePopulation()
{
StringBuilder sb=新的StringBuilder();
StringBuilder sql=新的StringBuilder();
//定义sql
追加(“选择pid,填充”);
追加(“来自人口”);
追加(“按pid ASC排序”);
使用(IDataReader=SqlHelper.GetDataReader(sql.ToString()))
{
sb.附录(“家禽种群”);
while(reader.Read())
{
int pid=reader.IsDBNull(0)?-1:reader.GetInt32(0);
字符串填充=reader.IsDBNull(1)?string.Empty:reader.GetString(1);
population=population.Trim();
sb.AppendLine(string.Format(“{1}”,pid,population));
}
}
某人加上一行(“”);
ltrlExplorePopulation.Text=sb.ToString();
}

不容易。由于您使用的是文字而不是asp.net控件(如下拉列表),因此asp.net不会创建控件供您在代码隐藏中使用

也就是说,您应该能够通过请求参数访问该值

var value = Request["populationSelect"];
更好的解决方案是在页面上创建dropdownlist控件并将其数据绑定到该控件

if (!IsPostBack)
{
    List<ListItem> data = new List<ListItem>();
    using (IDataReader reader = SqlHelper.GetDataReader(sql.ToString()))
    {
        //sb.AppendLine("<div class=\"narrowRes\">Poulation</div><select name=\"populationSelect\" class=\"narrowResSelect\"><option value=\"0\">All populations</option>");

        while (reader.Read())
        {
            int pid = reader.IsDBNull(0) ? -1 : reader.GetInt32(0);
            string population = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);

            population = population.Trim();
            data.Add(new ListItem(population, pid.ToString()));
            //sb.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", pid, population));
        }
    }
    DropDownList1.DataSource = data;
    DropDownList1.DataBind();
}
if(!IsPostBack)
{
列表数据=新列表();
使用(IDataReader=SqlHelper.GetDataReader(sql.ToString()))
{
//sb.附录(“家禽种群”);
while(reader.Read())
{
int pid=reader.IsDBNull(0)?-1:reader.GetInt32(0);
字符串填充=reader.IsDBNull(1)?string.Empty:reader.GetString(1);
population=population.Trim();
Add(新列表项(population,pid.ToString());
//sb.AppendLine(string.Format(“{1}”,pid,population));
}
}
DropDownList1.DataSource=数据;
DropDownList1.DataBind();
}

是否确定需要动态创建这些控件?有什么原因不能使用“创建asp.net DropDownList”并从代码隐藏中添加该控件?我想我应该切换到该选项,是否无法获取select name=“population”?我会将其从文字控件切换到文字控件,以使其更简单
if (!IsPostBack)
{
    List<ListItem> data = new List<ListItem>();
    using (IDataReader reader = SqlHelper.GetDataReader(sql.ToString()))
    {
        //sb.AppendLine("<div class=\"narrowRes\">Poulation</div><select name=\"populationSelect\" class=\"narrowResSelect\"><option value=\"0\">All populations</option>");

        while (reader.Read())
        {
            int pid = reader.IsDBNull(0) ? -1 : reader.GetInt32(0);
            string population = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);

            population = population.Trim();
            data.Add(new ListItem(population, pid.ToString()));
            //sb.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", pid, population));
        }
    }
    DropDownList1.DataSource = data;
    DropDownList1.DataBind();
}