Solved

DropDownList selectedindex = -1 after postback

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 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