?
Solved

DropDownList selectedindex = -1 after postback

Posted on 2008-06-19
3
Medium Priority
?
3,904 Views
Last Modified: 2010-04-21
Hi Experts,

The dropdownlist (DDL1) on my aspx page gets populated on page load just fine. On page load I set the selectedindex to 1.

I have Autopostback set to false on the dropdownlist.

When I click on a button, and go to the function behind that button click Updatebtn_Click(), I want to retrieve the selected item in DDL1. However, at this point I get an object reference error and DDL1.selectedindex = -1.

Could you tell me what's going on and how I can get the selected value without a flash on the page?

Here's the aspx:
Dropdownlist:
<asp:DropDownList AutoPostBack="false" ID="DDL1" runat="server" Font-Size="X-Small" width="120"></asp:DropDownList>

Button:
<asp:Button ID="Updatebtn" runat="server" Text="Update" Font-Size="1.0em" OnClick="Updatebtn_Click" />

The page has enableviewstate set to false.


Here's the code behind:

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack && !Page.IsCallback)
        {

            InitAlertsPanel();

            DataTable dt = (DataTable)Session["dataTable"];
            grid1.DataSource = dt;
            grid1.KeyFieldName = "RowNumber";
            grid1.DataBind();


            // Register trigger for update panel
            ScriptManager1.RegisterAsyncPostBackControl(Button1);
            AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            trigger.EventName = "Click";

            if (Button1.UniqueID.EndsWith(trigger.ControlID))
            {
                trigger.ControlID = Button1.UniqueID.ToString();
            }

        }
        else
        {
            //Bind the grid on every request
            BuildGrid1();


        }

        if (!IsPostBack){
            GetUPendingStatus(); // This populates DDL1

        }

0
Comment
Question by:rss2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Expert Comment

by:burakiewicz
ID: 21823659
to get it form the codebehind you need it to postback, if you can use ajax then you could put the ddl in an update panel, and you wont see the screen flash
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 21824233
>>The page has enableviewstate set to false.

ViewState is what holds the values of controls.
Try either enabling viewstate on the entire page or just on the dropdownlist.
0
 

Author Closing Comment

by:rss2
ID: 31468860
Thank you SO MUCH!!! :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question