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

  • Thread starter Thread starter Nathan Baulch
  • Start date Start date
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
 
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));
}
}
 
Back
Top