Manually get the text that a certain control posted when in IsPostBack mode...


N

Nathan Baulch

I have a page with EnableViewState="false".
That page contains a DropDownList with dynamic items and a submit Button.

I need to get the SelectedIndex of the DropDownList on postback however all
I ever get is -1. This makes sense since on postback, the DropDownList is
now empty (ViewState data is responsible for maintaining the items).

Now the only way I can think of to get the SelectedIndex is to somehow get
the text that the DropDownList posted to the server and perform an IndexOf()
or FindByText().

My question is how do I manually get the text that a certain control posted
when in IsPostBack mode?
Is there a Collection somewhere of posted data?
Do controls expose raw posted data somewhere?


Cheers
Nathan
 
Ad

Advertisements

N

Nathan Baulch

My question is how do I manually get the text that a certain control
posted when in IsPostBack mode?
Is there a Collection somewhere of posted data?
Do controls expose raw posted data somewhere?

My solution was to subclass the DropDownList control and use the
LoadPostData method to save the postback data to a property. Here is the
code for anybody who is interested:

public class CustomDDL : DropDownList,IPostBackDataHandler
{
private string postedText;
private string text;

public string PostedText {
get {
return postedText;
}
}
public string Text {
set {
text = value;
}
}

public virtual bool LoadPostData(
string postDataKey,NameValueCollection values) {
postedText = values[postDataKey];
return false;
}
protected override void OnDataBinding(EventArgs e) {
base.OnDataBinding(e);
SelectedIndex = Items.IndexOf(Items.FindByText(text));
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top