Adding EventHandler to DropDownList programically

Discussion in 'ASP .Net' started by dtarczynski, Sep 7, 2006.

  1. dtarczynski

    dtarczynski Guest

    Hello.
    Im trying to add new EventHandler to DropDownList programically. Im
    doing something like this:

    DropDownList ddlCate = new DropDownList();
    ddlCate.ID = String.Format("ddlCategory{0}", i++);
    ddlCate.DataSource = tblSub1;
    ddlCate.DataTextField =
    tblSub1.Columns["categories_name"].ColumnName;
    ddlCate.DataValueField =
    tblSub1.Columns["categories_id"].ColumnName;
    Panel1.Controls.Add(ddlCate);
    ddlCate.DataBind();
    ddlCate.AutoPostBack = true;
    ddlCate.SelectedIndexChanged += new
    System.EventHandler(BindCategories);

    This is BindCategories function:

    void BindCategories(object sender, EventArgs e)
    {
    Response.Write("Event raised!");

    }

    but when Im changing value in ddlCate BindCategories are not raised.
    Any help?

    thanks in advandce
    Darek
     
    dtarczynski, Sep 7, 2006
    #1
    1. Advertisements

  2. My guess s that you have your code in a if (!Page.IsPostBack){}

    since events happen on postback, they need to be setup during postback.
    Normally you'd just place the hook during init...

    onInit(object source, EventArgs e)
    {
    ddlCate.SelectedIndexChanged +=....
    }

    since it's dynamic, simply put it out side the !Page.IsPostback

    Karl
     
    Karl Seguin [MVP], Sep 7, 2006
    #2
    1. Advertisements

  3. dtarczynski

    dtarczynski Guest

    Thanks for respose, but mayby you can answer me in more detail. Look
    Karl. I have one DropDownList ddlCategories (I create it with
    designer), then I have added code for SelectedItemIndexChanged. I have
    to create another DropDownList when ddlCategories
    SelectedItemIndexChanged is raised. All my code looks like this:

    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void ddlCategories_SelectedIndexChanged(object sender,
    EventArgs e)
    {

    if (...)
    {
    int i = 0;
    DropDownList ddlCate = new DropDownList();
    ddlCate.ID = String.Format("ddlCategory{0}", i++);
    ddlCate.DataSource = tblSub1;
    ddlCate.DataTextField =
    tblSub1.Columns["categories_name"].ColumnName;
    ddlCate.DataValueField =
    tblSub1.Columns["categories_id"].ColumnName;
    pnlCat.Controls.Add(ddlCate);
    ddlCate.DataBind();
    ddlCate.AutoPostBack = true;
    ddlCate.SelectedIndexChanged += new
    System.EventHandler(BindCategories);
    }


    }

    void BindCategories(object sender, EventArgs e)
    {
    Response.Write("Event raised!");

    }

    Can you answer me, how to properly add this event handler please?

    Karl Seguin [MVP] napisal(a):
     
    dtarczynski, Sep 7, 2006
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.