I'm trying to read a value from a dropdown list after a partial page post back. For some reasons, it's always null.
ASP:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlW1SundayProject1" runat="server"
DataSourceID="dataProjectList"
开发者_Python百科 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>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (ScriptManager.IsInAsyncPostBack)
{
lblW1SundayProject1.Text = "User selected: " + Request.Form["ddlW1SundayProject1"]; // this is always null
}
}
Out put is: "User selected: "
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 do Request.Form[ddlW1SundayProject1.ClientID]
.
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 at AutoPostBack="True" ">
where you have an extra "
精彩评论