• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

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...?
0
dotnet22
Asked:
dotnet22
  • 7
  • 5
  • 3
  • +1
1 Solution
 
thrill_houseCommented:
Can you post the selectedindex change event?
0
 
raterusCommented:
I see no reason this shouldn't work if you dynamically populate from ItemDataBound, can you post your code?
0
 
dotnet22Author Commented:
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">
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thrill_houseCommented:
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.
0
 
dotnet22Author Commented:
That's the problem... On a postback grdOrderSheets_SelectedIndexChanged is never triggered???
0
 
thrill_houseCommented:
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.
0
 
raterusCommented:
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?
0
 
dotnet22Author Commented:
I know it's proably stemming from somewhere else...  
0
 
dotnet22Author Commented:
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
                  
            }
0
 
raterusCommented:
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.
0
 
dotnet22Author Commented:
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...
0
 
raterusCommented:
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!
0
 
dotnet22Author Commented:
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...
0
 
raterusCommented:
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");
            }
0
 
RejojohnyCommented:
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 ....??
0
 
RejojohnyCommented:
also confirm that the autopostback is set to true for the dropdown .. ddlPages
0
 
dotnet22Author Commented:
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...
0
 
RejojohnyCommented:
as i said the code should have worked the way u had written .. as u have mentioned now, it was indeed true .. as for selectedindexchanged not firing when values are same, it is behaving as expected ..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now