Solved

DropDownList selectedindex = -1 after postback

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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