Solved

DropDownList selectedindex = -1 after postback

Posted on 2008-06-19
3
3,888 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
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now