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

Discussion in 'ASP .Net' started by Nathan Baulch, Jun 25, 2003.

  1. 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
    Nathan Baulch, Jun 25, 2003
    #1
    1. Advertising

  2. > 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));
    }
    }
    Nathan Baulch, Jun 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joey
    Replies:
    2
    Views:
    3,914
  2. John J Lee
    Replies:
    3
    Views:
    455
    bruno at modulix
    Dec 1, 2005
  3. Edward Loper
    Replies:
    0
    Views:
    444
    Edward Loper
    Aug 7, 2007
  4. John J Lee
    Replies:
    0
    Views:
    502
    John J Lee
    Aug 7, 2007
  5. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    415
    Edward Loper
    Aug 9, 2007
Loading...

Share This Page