Home All Groups Group Topic Archive Search About
Author
16 Mar 2006 12:46 PM
cyrus
Hi, I have been working with my project for 2 weeks but i have failed
everytime, The problem is i want to catch the selecteditem in my dropdownlist
but it is just the first item everytime,
here is my code:
protected override void CreateChildControls()
    {
        DDL = new DropDownList();
        DDL.Load += new EventHandler(DDLLoad);
        DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);
        Controls.Add(DDL);
    }
    protected void DDLLoad(object sender, EventArgs e)
    {
        DS = GetChannels();
        if (DS.Tables.Count > 0 )
        {
            DDL.DataSource = DS;
            DDL.DataTextField = "title";
            DDL.DataValueField = "title";
            DDL.DataBind();

        }
        else
        {
            MessageLBL.Text = "Unable to connect to the database.";
        }
    }
    protected void DDLItemChanged(object sender, EventArgs e)
    {
        string mytitle = DDL.SelectedValue;

        MessageLBL.Text = mytitle; //here it is always the first item
    }

Author
16 Mar 2006 3:21 PM
Phillip Williams
You have to call the the CreateChildControls during the page initialization
phase in order for the ViewState to be loaded upon postback and the correct
events get triggered.  But if you call the CreateChildControls during the
page.load or any later stage then the event will never fire upon postback.

For an overview of the page's life cycle:
http://msdn2.microsoft.com/en-us/library/ms178472(VS.80).aspx

Show quoteHide quote
"cyrus" wrote:

> Hi, I have been working with my project for 2 weeks but i have failed
> everytime, The problem is i want to catch the selecteditem in my dropdownlist
> but it is just the first item everytime,
> here is my code:
>  protected override void CreateChildControls()
>     {
>         DDL = new DropDownList();
>         DDL.Load += new EventHandler(DDLLoad);
>         DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);
>         Controls.Add(DDL);
>     }
>     protected void DDLLoad(object sender, EventArgs e)
>     {
>         DS = GetChannels();
>         if (DS.Tables.Count > 0 )
>         {
>             DDL.DataSource = DS;
>             DDL.DataTextField = "title";
>             DDL.DataValueField = "title";
>             DDL.DataBind();
>            
>         }
>         else
>         {
>             MessageLBL.Text = "Unable to connect to the database.";
>         }
>     }
>     protected void DDLItemChanged(object sender, EventArgs e)
>     {
>         string mytitle = DDL.SelectedValue;
>       
>         MessageLBL.Text = mytitle; //here it is always the first item
>     }
Author
16 Mar 2006 3:30 PM
Todd Burry
First try modifying this in CreateChildControls:

if(!Page.IsPostback)
  DDL.Load += new EventHandler(DDLLoad);

That should work if you have view state enabled on your control and the page
because the items will be fetched from the view state every time.

If the above method does not work then try modifying DDLoad, inside the
first part of the if statement.

string selected_value = DDL.SelectedValue;
DDL.DataSource = DS;
DDL.DataTextField = "title";
DDL.DataValueField = "title";
DDL.DataBind();
if(DDL.Items.FindByValue(selected_value) != null)
  DDL.SelectedValue = selected_value;



Show quoteHide quote
"cyrus" wrote:

> Hi, I have been working with my project for 2 weeks but i have failed
> everytime, The problem is i want to catch the selecteditem in my dropdownlist
> but it is just the first item everytime,
> here is my code:
>  protected override void CreateChildControls()
>     {
>         DDL = new DropDownList();
>         DDL.Load += new EventHandler(DDLLoad);
>         DDL.SelectedIndexChanged += new EventHandler(DDLItemChanged);
>         Controls.Add(DDL);
>     }
>     protected void DDLLoad(object sender, EventArgs e)
>     {
>         DS = GetChannels();
>         if (DS.Tables.Count > 0 )
>         {
>             DDL.DataSource = DS;
>             DDL.DataTextField = "title";
>             DDL.DataValueField = "title";
>             DDL.DataBind();
>            
>         }
>         else
>         {
>             MessageLBL.Text = "Unable to connect to the database.";
>         }
>     }
>     protected void DDLItemChanged(object sender, EventArgs e)
>     {
>         string mytitle = DDL.SelectedValue;
>       
>         MessageLBL.Text = mytitle; //here it is always the first item
>     }