Click here to Skip to main content
16,022,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am working on the code below but the value is blank always. Please help
public partial class ThePage : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
           FillDdlOptions();
     }

     private void FillDdlOptions()
     {
           ddlOptions.Items.Clear();
           ddlOptions.Items.Add(new ListItem("Pick one...", string.Empty)); 
           ddlOptions.Items.Add(new ListItem("Option A", "a"));
           ddlOptions.Items.Add(new ListItem("Option B", "b")); 
           ddlOptions.Items.Add(new ListItem("Option C", "c"));
     }

     protected void btnSave_Click(object sender, EventArgs e)
     {
           Response.Write(ddlOptions.SelectedValue);
     }
}
Posted
Updated 27-Dec-13 11:42am
v2

1 solution

It is blank because you are reloading it. When a page posts back it will run the Page_load event before your btnSave_Click event.

In your Page_Load do this instead:

C#
if (!IsPostBack)
{
   FillDdlOptions();
}


That way it will only fill your options when the page loads the first time and not during post backs.
 
Share this answer
 
Comments
Member 10491035 28-Dec-13 0:26am    
Is Postback is normally used on page _load event to detect if the page is getting generated due to postback requested by a control on the page or if the page is getting loaded for the first time. When page is requested again without any event(directly write URL in browser) then again IsPostBack is false. PostBack occurs when event occurs (button click, dropdown change).
when u reload the page then isPostBack becomes false.

Thanks a lot it worked.
ZurdoDev 28-Dec-13 10:06am    
Glad to hear.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900