Dropdownlist 返回 null 并回发部分页面
我试图在部分页面回发后从下拉列表中读取值。由于某些原因,它始终为空。
ASP:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlW1SundayProject1" runat="server"
DataSourceID="dataProjectList"
DataTextField="ProjectName" DataValueField="Project_Id"
AppendDataBoundItems="true"
onBlur="validateProjectTask('W1', 'Sunday', 1);"
AutoPostBack="True" ">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblW1SundayProject1" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.IsInAsyncPostBack)
{
lblW1SundayProject1.Text = "User selected: " + Request.Form["ddlW1SundayProject1"]; // this is always null
}
}
输出为:“用户选择:”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不起作用,因为 ASP.NET 服务器控件(如 DropDownList)会破坏表单字段的名称/ID。因此,使用您为控件 ID (ddlW1SundayProject1) 键入的名称在 Form 集合中查找将找不到任何内容(因为 html 中的实际名称类似于 ctl00_UpdatePanel1_ddlW1SundayProject1)。
要获取回发的值,您可以使用 ddlW1SundayProject1.SelectedValue,或者如果您出于某种奇怪的原因需要查看 Form 集合,则可以执行 Request.Form[ddlW1SundayProject1.ClientID ]。
It won't work because ASP.NET server controls (like the DropDownList) mangle the names/ids of the form fields. So using the name you typed for the control ID (ddlW1SundayProject1) to look in the Form collection will not find anything (because the actual name in the html would be something like ctl00_UpdatePanel1_ddlW1SundayProject1).
To get the posted back value, you can either use
ddlW1SundayProject1.SelectedValue
, or if you need to look in the Form collection for some odd reason, you can doRequest.Form[ddlW1SundayProject1.ClientID]
.如果您在 Visual Studio 中没有收到任何错误,它似乎工作正常,因为我在您的代码中看到的唯一不正确的地方是您的 标记格式不正确在
AutoPostBack="True" ">
处,您有一个额外的 "It seems to be working fine if you are not getting any error in Visual Studio, because the only thing i can see with your code which is not correct is your
<asp:DropDownList
tag is not well formed atAutoPostBack="True" ">
where you have an extra "