Link to home
Start Free TrialLog in
Avatar of dotnet22
dotnet22

asked on

selectedindex change not working in datagrid

If I dynamically populate my dropdownlist in ItemDataBound of a datagrid, the selectedindex change even does not work.. However, If I populate the dropdown through the properties and not dynamically, it works? Does anyone have any ideas? In both cases it postback but the event is not triggered for the dynamic case...?
Avatar of thrill_house
thrill_house
Flag of United States of America image

Can you post the selectedindex change event?
I see no reason this shouldn't work if you dynamically populate from ItemDataBound, can you post your code?
Avatar of dotnet22
dotnet22

ASKER

this is the item databound

private void grdOrderSheets_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                  if ( (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
                  {



                        DropDownList pageNums = (DropDownList)e.Item.FindControl("ddlPages");
                        
                        pageNums.DataTextField = "documentSheetOrder";
                        pageNums.DataValueField = "documentInputID";
                        pageNums.DataSource = pages;
                        pageNums.DataBind();
                        
                        


                  }
            }


the selectedindex change

protected void grdOrderSheets_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  // suppose to come int here but it dosn't
                  
            }

the control
<asp:dropdownlist id="ddlPages" runat="server" onselectedindexchanged="grdOrderSheets_SelectedIndexChanged" autopostback="True">
protected void grdOrderSheets_SelectedIndexChanged(object sender, System.EventArgs e)
          {
               // suppose to come int here but it dosn't
               
          }


This is the code we need.  The logic in there is probably not correct.
That's the problem... On a postback grdOrderSheets_SelectedIndexChanged is never triggered???
I believe it is being fired, but due to some logic errors, it doesn't appear to be firing.  But if you are so sure that it's not a logic error and it is indeed never firing then I'm sorry for wasting your time.
the only way I can see why it is not firing is if you are rebinding the dropdownlist on a postback (erasing any event handler information).  It doesn't appear you are doing this, and I don't know why you would.  Can you paste full code or the entire page process?
I know it's proably stemming from somewhere else...  
private void Page_Load(object sender, System.EventArgs e)
            {
            
                  if ( !Page.IsPostBack)
                  {

                  
                        if (User.IsInRole(clPermissions.AccessRights.Admin.ToString())||(User.IsInRole(clPermissions.AccessRights.Library.ToString())))

                        {
                        
                  
                        
                              if (Request.QueryString["id"] != null)
                              {

                                    try
                                    {
                                          int docID = Convert.ToInt32(Request.QueryString["id"]);
                                          pages = clDocument.GetDocumentInputSheets(docID);
                                          grdOrderSheets.DataSource = clDocument.GetDocumentSheetOrderDisplay(docID);
                                          grdOrderSheets.DataBind();
                                          

                                    }

                                    catch(Exception ee)
                                    {
                                          lblMessage.Text = ee.Message;
                                    }
                              }

                        }
                        else
                        {
                              Response.Redirect("../login.aspx");
                        }
                  }//if ( !Page.IsPostBack)
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.grdOrderSheets.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdOrderSheets_ItemDataBound);
                  this.grdOrderSheets.SelectedIndexChanged += new System.EventHandler(this.grdOrderSheets_SelectedIndexChanged);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            

            

            private void grdOrderSheets_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                  if ( (e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
                  {



                        DropDownList pageNums = (DropDownList)e.Item.FindControl("ddlPages");
                        
                        pageNums.DataTextField = "documentSheetOrder";
                        pageNums.DataValueField = "documentInputID";
                        pageNums.DataSource = pages;
                        pageNums.DataBind();
                        
                        


                  }
            }

            protected void grdOrderSheets_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  // suppose to come int here but it doewn't
                  
            }
Everything looks ok to me, this is very weird.

Try what I do when I can't figure out something like this, create a new page that does absolutely nothing but what your question is about.  Get it working, it should work in the most simplest form.  Then start tacking on the extra code from your original page and see when it stops working, then you will have your problem.
That's exactly what I did and when I comment out the dynamic population in item databound, it works... go figure... Note: I tried this on a brand new page...
If that was your new page, then you are still doing "extra" things that don't have to be done, for testing purposes anyway.  Everything should be done bare bones, and I mean it, no role checks in page_load, no setting the dropdownlist DataSource=pages.  nothing extra, only what you need!
Sorry. I meant I did what you suggested in a test page. The code I have posted is from the original. So there was no role checks etc in the new test page... I've done this plenty of time before... I've never come across this...
ok, this is weird.  I'm not a C# programmer, but just for kicks I created a test page to do exactly what you are after.  Instead of assigning the eventhandler in the asp control on the aspx page, I did it in code, in ItemDataBound.  I was experiencing the exact same problem you were, the eventhandler wasn't fired on a postback.  I read through some examples, and someone has suggested to put it in ItemCreated...bam!  worked like a champ.  This is a very weird behavior to me, so if a C# developer would like to explain it, I'd even appreciate it!

here's what it looked like..

            private void dgTest_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                  if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
                  {
                        string[] blah = new string[3];
                        blah[0]="blahblah";
                        blah[1]="test";
                        blah[2]="yo";

                        DropDownList ddlTest = (DropDownList)e.Item.FindControl("ddlTest");
                        ddlTest.DataSource = blah;
                        ddlTest.AutoPostBack = true;
                        ddlTest.DataBind();

                        ddlTest.SelectedIndexChanged += new System.EventHandler(this.ddlTest_SelectedIndexChanged);
                  }
            }

            private void ddlTest_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  // suppose to come int here but it doewn't
                  Response.Write("fired event");
            }
ok .. y do u say the selectedindexchanged is not firing??
have a look at this link exapling everything about adding a dropdown to a datagrid ... uses the same approach as have here
http://odetocode.com/Articles/231.aspx

is it because u r searching for adropdown named ddlPages in itemdatabound
DropDownList pageNums = (DropDownList)e.Item.FindControl("ddlPages");

and u do not have a event handler for the dropdown .. do u have it in ur aspx page .. something like
            <ItemTemplate>
                <asp:DropDownList
                    ID="ddlPages" Runat="server"
                    AutoPostBack="True"
                    OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" />
            </ItemTemplate>
within ur datagrid ....??
also confirm that the autopostback is set to true for the dropdown .. ddlPages
The problem was due to the fact that values were all the same in the dropdown... Meaning it looked as follows:

Text  Value
1         66
2         66
3         66

Once the Values were different, it solved the problem... Go figure...
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial