从 ASP.Net 代码隐藏中获取/设置文字中的选择框
我将以下代码添加到我的表单中的文字中。如何在后面的代码中获取/设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不容易。由于您使用的是文字而不是 asp.net 控件(如下拉列表),因此 asp.net 不会创建控件供您在后面的代码中使用。
话虽这么说,您应该能够通过请求参数访问该值。
更好的解决方案是在页面上创建一个下拉列表控件并对其进行数据绑定。
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.
A better solution would be to create a dropdownlist control on the page and databind to it.