HI Friends,

On Button click(postback), my dropdownlist of gridview is getting blank, so i m getting error of "Object Reference...." on the line "ddl.selecteditem.value"

Also, dropdownlist's selectedindexchanged event is fired on Button Click(Page Postback), which is making the dropdownlist to go blank. AutoPostBack of dropdownlist is set as False, still the event is fired

Pls help

Regards

Suresh G

Recommended Answers

All 7 Replies

When form is submitted, SelectedIndex property is lost, you must save it with query string, session or something, and then you can use it without error.

Response.Redirect("page.aspx?index=" + dropDownList1.SelectedIndex);

or

// Saving the selected index value
Session["index"] = dropDownList1.SelectedIndex;

// Getting selected index value
string index = Session["index"].ToString();

Thanks for the Reply,

In which event m i going to capture selectedindex

Also, this is not happening with my Other Forms, I m able to capture the selectedvalue for each row of the gridview in my other Forms, under similar conditions

Pls help

Regards
Suresh G

In this case, button is problem.

protected void button1_Click(object sender, EventArgs e)
{
    ...

    // Saving SelectedIndex property
    Session["index"] = dropDownList1.SelectedIndex;

    ...
}

Both SelectedIndex and SelectedItem is not working, since at Button Click, the dropdownlist Selected Index Changed event is fired and the count of dropdownlist is becoming Zero

Do you use the value that we saved or you didn't changed SelectedIndex in code to Session["index"]. Can you post your code?

Have you tried to debug application? Try solving with it. Watch for location where SelectedIndex property is restarted.

Do you have a method that removes all rows from DataGrid/DropDownList. In all that methods there must be a code that saves selected index in session.

I solved my problem with session setting in some methods, where I selected item from list and selected index was -1 (default) because of Page_Load event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.