1

I am trying to populate a datalist control onchange of a dropdown list. I am unable to fire Itemcommand event of datalist. When I populate datalist on page_load event it works fine. but I don't know what is happening with the dropdown.

code behind:

 protected void dlSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        string CategoryID = Request.QueryString["ID"].ToString();

        using (TestEntities db = new TestEntities())
        {
            IEnumerable<Test.Product> Test= //linq expression here
            dlProducts.DataSource = Test;
            dlProducts.DataBind();
        }
    }
Damon
  • 3,004
  • 7
  • 24
  • 28
Deepak D
  • 103
  • 1
  • 12
  • if you want to bind a datalist with dropdown selection then must try for dropdown `selectedIndexChanged` event,why you want to use `Item Command` – Rahul Sep 09 '13 at 09:05
  • check this link as well http://stackoverflow.com/questions/8716578/itemcommand-event-not-firing-for-datalist – Rahul Sep 09 '13 at 09:06
  • datalist is binding on change of dropdown but after binding i cannot fire itemcommand event of datalist. – Deepak D Sep 09 '13 at 09:11
  • are u using `Page.IsPostBack` property on `Page_Load`,if not then use it and check my answer as well.. – Rahul Sep 09 '13 at 09:24

3 Answers3

0

Try to use

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) //this IF statement is what prevents re-binding on PostBack 
    { 
        // Code to bind datalist..
    } 

}

Note :- You must not databind on post back. Otherwise any pending event handler requests are cancelled.

Rahul
  • 5,603
  • 6
  • 34
  • 57
0

Try it using event handler.

dlProducts.ItemCommand += new DataListCommandEventHandler(dlProducts_ItemCommand);
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
0

You are fire Itemcommand of datalist by dropdownlist. You see in my video is uploaded on youtube link given below : https://www.youtube.com/watch?v=hnZp6y2n_h8

In this video I am using "ID" variable. This variable just like a ItemIndex. This Id provide dropdownlist changed Datalistitem index.

It is like a (e.ItemIndex=Id) you are use ID on e.ItemIndex place .

Community
  • 1
  • 1