Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET - How do I reference a dynamic control from code behind

Posted on 2008-06-20
4
Medium Priority
?
248 Views
Last Modified: 2012-08-14
I have a button (1 of 2) inside a ListViewItem. The definition is:
<ItemTemplate>
<tr class="ItemStyle" style=" background-color: White ">
<td  ondatabinding="ManageButtonText" ID="UserCell" runat="server" align=center Width=180 style="border-bottom: solid 1px #BDF">
<%#Eval("username")%></td>
<td style="padding-left: 6px; padding-right: 3px; border-bottom: solid 1px #BDF">
<asp:Button ID="DetailButton"  OnClick="ButtonClick" CausesValidation=false
runat="server" Width=140 Height="22px" Font-Size=8  Text="View/Edit detailed ratings" />&nbsp;
<asp:Button ID="SummaryButton"  OnClick="ButtonClick" CausesValidation=false
runat="server" Width=140 Height="22px" Font-Size=8 Text="View/Edit summary ratings" />
</td>
</tr>
</ItemTemplate>

The extract in the attached code snippet, from my IE browser's "View Source", shows the ID of my button to be "EmpListView_ctrl0_DetailButton".
But when I try to access the button from code-behind using Page.FindControl("EmpListView_ctrl0_DetailButton"), it returns Nothing.
What am I doing wrong, please?

<table id="EmpListView_Table1" style="margin: 15px; ">
<tr>
<td>
<table id="EmpListView_itemPlaceholderContainer" border="1" cellpadding="4" class="MediumArial " style=" border-collapse: collapse;border-color: #555;
border-style:solid ; border-width:1px; ">
<tr id="EmpListView_Tr2" height="20px" style="font-weight:bold;  background-color: #ddd;">
<th id="EmpListView_Th1" style="border-bottom: solid 1px #333; padding: 5px">Employee Name</th>
<th id="EmpListView_Th2" colspan="2" style="border-bottom: solid 1px #333;">Action</th>
</tr>
<tr class="ItemStyle" style=" background-color: White ">
<td id="EmpListView_ctrl0_UserCell" align="center" width="180" style="border-bottom: solid 1px #BDF">Conradie Eddie</td>
<td style="padding-left: 6px; padding-right: 3px; border-bottom: solid 1px #BDF">
<input type="submit" name="EmpListView$ctrl0$DetailButton" value="View/Edit detailed ratings" id="EmpListView_ctrl0_DetailButton" style="font-size:8pt;height:22px;width:140px;" />&nbsp; 
<input type="submit" name="EmpListView$ctrl0$SummaryButton" value="View/Edit summary ratings" id="EmpListView_ctrl0_SummaryButton" style="font-size:8pt;height:22px;width:140px;" />
</td> 
</tr>
</table>

Open in new window

0
Comment
Question by:eddiecon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 21

Expert Comment

by:naspinski
ID: 21830055
instead of Page.FindControl("EmpListView_ctrl0_DetailButton") try:
EmpListView.FindControl("DetailButton");

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21830059
more completely
Button DetailButton = (Button)EmpListView.FindControl("DetailButton");

Open in new window

0
 

Author Comment

by:eddiecon
ID: 21830176
Thanks for responding
The button(s) I want to access are inside each Listview item. So I have
EmpListView_ctrl0_Detail
EmpListView_ctrl1_Detail
etc
I have the "row" number 0/1/2 etc, now I just need the right syntax to refernce the button
Could you help, please?
0
 
LVL 21

Accepted Solution

by:
naspinski earned 200 total points
ID: 21830279
if you have the row number, thats easy.  Remember that asp.net doesn't reference anything by the whole html id that you are using up there, just control IDs.  Call this in your EmpListView_DataBound Event.
Button DetailButton = (Button)EmpListView.Items[ROW_NUMBER].FindControl("DetailButton");

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

670 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