0

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" 
            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: "

dam
  • 171
  • 1
  • 6
  • 17
  • Did you try `lblW1SundayProject1.Text = "User selected: " + ddlW1SundayProject1.Text;` ? – Bala R Aug 24 '11 at 00:40
  • Any specific reason why you handle the event that way ? Why not simply use selectedindexchanged event ? – David Aug 24 '11 at 01:03
  • @Bala, I tried your suggestion. It didn't work. – dam Aug 24 '11 at 19:38
  • @David, I did try selectedindexchanged initially, but it never gets triggered. The only way I know the form got posted is checking it through page_load. What am i missing/doing wrong? – dam Aug 24 '11 at 19:40

2 Answers2

1

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].

patmortech
  • 10,139
  • 5
  • 38
  • 50
0

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 "

Waqas
  • 6,812
  • 2
  • 33
  • 50
  • The extra " is a mistake I made in copy/paste-ing. It compiles and runs fine in VS. – dam Aug 24 '11 at 19:38
  • No. I don't have a masterpage. The scriptmanager and updatepanel are all on the same page. Why do you ask? – dam Aug 25 '11 at 04:36