从 ASP.Net 代码隐藏中获取/设置文字中的选择框

发布于 2024-11-29 17:13:20 字数 1113 浏览 1 评论 0原文

我将以下代码添加到我的表单中的文字中。如何在后面的代码中获取/设置 select = name="populationSelect".... 中的数据?

 protected void PopulatePopulation()
{
    StringBuilder sb = new StringBuilder();
    StringBuilder sql = new StringBuilder();

    // Define sql
    sql.Append("SELECT pid, population ");
    sql.Append("FROM populations ");
    sql.Append("ORDER BY pid ASC ");

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

            sb.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", pid, population));
        }
    }

    sb.AppendLine("</select>");

    ltrlExplorePopulation.Text = sb.ToString();
}

I have the below code that gets added to a literal in my form. How in the code behind to I grab get/set the data from the select = name="populationSelect"....?

 protected void PopulatePopulation()
{
    StringBuilder sb = new StringBuilder();
    StringBuilder sql = new StringBuilder();

    // Define sql
    sql.Append("SELECT pid, population ");
    sql.Append("FROM populations ");
    sql.Append("ORDER BY pid ASC ");

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

            sb.AppendLine(string.Format("<option value=\"{0}\">{1}</option>", pid, population));
        }
    }

    sb.AppendLine("</select>");

    ltrlExplorePopulation.Text = sb.ToString();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-12-06 17:13:20

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

话虽这么说,您应该能够通过请求参数访问该值。

var value = Request["populationSelect"];

更好的解决方案是在页面上创建一个下拉列表控件并对其进行数据绑定。

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

Not easily. Since you're using a literal instead of an asp.net control (like a drop down list), asp.net does not create a control for you to use in the code behind.

That being said you should be able to access the value through the Request parameters.

var value = Request["populationSelect"];

A better solution would be to create a dropdownlist control on the page and databind to it.

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文