Solved

Registering to 'OnSelectedIndexChanged' of a DRopDownList within a GridView

Posted on 2009-06-29
13
672 Views
Last Modified: 2012-05-07
Hello,

I am writing a c# asp.net application in which I am using a gridView.
The gridView has a DropDownList in one of the EditItemTemplates (Below is the code).

I would like to register to the 'OnSelectedIndexChanged' of the dropdownlist the following way:

((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddl")).SelectedIndexChanged += new EventHandler(CompanyMain_SelectedIndexChanged);

But that doesn't seem to work. When debugging, the debugger does not enter 'CompanyMain_SelectedIndexChanged' function.

Any help would be appreciated,

Thank you



<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"  
OnRowDataBound="gv_RowDataBound" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowEditing="gv_RowEditing">
  <Columns>
    <asp:TemplateField HeaderText="test">
	<ItemTemplate>
	  <asp:Label ID="lbl" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.name")%>'></asp:Label>
	</ItemTemplate>
	<EditItemTemplate>
	  <asp:DropDownList ID="ddl" runat=server AutoPostBack="true">
	    <asp:ListItem Text="aa" Value="aa"></asp:ListItem>
	    <asp:ListItem Text="bb" Value="bb"></asp:ListItem>
	  </asp:DropDownList>
	</EditItemTemplate>
   </asp:TemplateField>
  <asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>

Open in new window

0
Comment
Question by:Relegence
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 5

Expert Comment

by:Krummelz
ID: 24742713
It would be much simpler to just add a OnSelectedIndexChanged="CompanyMain_SelectedIndexChanged" property to your drop down list.

Is there a specific reason why you would want to bind the event at runtime?
0
 

Author Comment

by:Relegence
ID: 24742740
Actually, yes.

My original question is more comlpicated then that, I tried to make it clearer.

I actually created a user-control which contains a dropDown (among other controls).
I added this user-control to my gridView and what I was trying to do was to use the selectedIndexChanged event, I added to my user control, within the gridView's dropdown.

I am trying to register to the event when 'gv_RowEditing' was fired, but that doesn't seem to work.

registering to the event:

ddlToText - the name of the ascx
ctlLevel1 - the name of the ddlToText I added to my gridView

((ddlToText)gv.Rows[e.NewEditIndex].FindControl("ctlLevel1")).OnSelectedIndexChanged += new ddlToText.OnSelectedIndexChangedDelegate(ctlLevel1_OnSelectedIndexChanged);

I hope I was clear enough :-)
0
 
LVL 3

Expert Comment

by:tpsl
ID: 24742754
I think u'll have to write that in the Gridview RowDataBound Event and not the Editing Event.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Relegence
ID: 24742771
When trying to do that:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  ((ddlToText)gv.Rows[e.Row.RowIndex].FindControl("ctlLevel1")).OnSelectedIndexChanged += new ddlToText.OnSelectedIndexChangedDelegate(ctlLevel1_OnSelectedIndexChanged);
}

I am getting the exception - 'Index was out of range. Must be non-negative and less than the size of the collection.'.
I don't understand why I am getting that exception.
0
 
LVL 3

Expert Comment

by:tpsl
ID: 24742798
OnSelectedIndexChanged is not an event of the user control.

U'll have to find the dropdownlist in the usercontrol and then add onSelectedIndexChanged event to it
0
 

Author Comment

by:Relegence
ID: 24743200
I tried that too, but still, the debugger doesn't stop at the 'DDL_SelectedIndexChanged' function

((ddlToText)gv.Rows[e.NewEditIndex].FindControl("ctlLevel1")).DDL.SelectedIndexChanged +=new EventHandler(DDL_SelectedIndexChanged);
0
 
LVL 3

Expert Comment

by:tpsl
ID: 24743601
Is the autopostback set to true of the dropdownlist?
0
 

Author Comment

by:Relegence
ID: 24744361
it is set to true
0
 
LVL 3

Expert Comment

by:tpsl
ID: 24751660
Does the page come to the page load of the page?
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24752347
I see no one has replied to this in awhile. I was hoping to gain some enlightenment... shucks.

I tried setting up this scenario for a test because it seemed do-able. What I found was myself stuck in the exact same boat you are in, except I was not using a custom dropdown control, just a standard one.

I did some digging around, there are a lot of people who complain of this problem, but not so many solutions :( darnit (I dont think that counts as swearing).

It seems to have to do with:
A) When you set this event inline in the aspx page on the control, it is normally set in the pre_init event. Not important in and of itself, but a signifigant fact, we can't do that for obvious reasons and we should be able to set events just the way you are trying to.

B) The issue seems to be not that we are not setting the event to the controls, but that this information is lost every time the page is posted back, so we don't get the functional out of it. We reset it in the row_databound, but alas when the autopostback occurrs it is erased.

Anyway, that's my theory and I'm pretty sure I'm right

Now you're using a custom control, and that just makes things more complex. Interestingly you didnt show a custom control in your sample code (not nice). I think your solution is to expose a property in your custom control that allows you to specify this event, and then set the property of your custom control up front (on the aspx page) like you would a normal dropdown.

If that doesn't make sense well, no harm, no foul...this thread's been dead for over 24hrs anyway.
0
 

Accepted Solution

by:
Relegence earned 0 total points
ID: 24752513
Thanks for your insights.
What I did eventually, was:

<controls:ddlTOText ID="ctlLevel1" Name="ctlLevel1" runat="server" OnSelectedIndexChanged="FillSubList"/>

I added the appropriate function on code behind and that worked well.
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24752650
Exactly what I was thinkin you should do.... great minds sometimes match up with mediocre one's..what's that all about! :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check number of row prior to displaying gridview 10 62
Save ms data to server side. 19 55
Need a simple RegEx to search for two words 27 44
Amazon S3 .Net error 5 17
Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question