Solved

FindControl and CheckBox...

Posted on 2010-11-11
14
1,508 Views
Last Modified: 2012-05-10
I have a situation where I cannot find a checkbox on a page and the page has a masterform. The code for this table is generated in the code-behind. The end result when I view the source is something like this (note: this is just a portion):

<input name="ctl00$Main$chkAllItems" type="checkbox" id="ctl00_Main_chkAllItems" title="Submit All" onclick="javascript:Select(true)" />
   
     <table border="1" align="center">
     <tr><td valign='top' align='center'><input id='chkAllItems0' name='chkAllItems0' type='checkbox' runat='server' title='Flag'/><a href="./PictList/Vacation/Vac2007/IMG_0184.jpg" target="_blank"><img src=./PictList/Vacation/Vav2007/IMG_0184.jpg width= 95 height=95 border="0"></a></td><td valign='top' align='center'><input id='chkAllItems1' name='chkAllItems1' type='checkbox' runat='server' title='Flag'/><a href="./PictList/Vacation/Vac2007/IMG_0185.jpg" target="_blank"><img src=./PictList/Vacation/Vac2007/IMG_0185.jpg width= 95 height=95 border="0"></a></td><td valign='top' align='center'><input id='chkAllItems2' name='chkAllItems2' type='checkbox' runat='server' title='Flag'/><a href="./PictList/Vacation/Vac2007/IMG_0186.jpg" target="_blank"><img src=./PictList/Vacation/Vac2007/IMG_0186.jpg width= 95 height=95 border="0"></a></td></tr></table>

When I try to perform a FindControl in the code-behind and look for like chkAllItems2 it returns a nothing.

Some of the oode in the code-behind is:

For i = i To Files.Length - 1
            'If Not mpContentPlaceHolder Is Nothing Then
            submitCtrl = CType(Page.FindControl("chkAllItems" & i), HtmlInputCheckBox)
            If Not submitCtrl Is Nothing Then
                submitCtrl.Disabled = True 'Just a sample check for DEBUG mode
            End If
            'End If

        Next i

Any ideas on how this could be found?

Any assistance on this would be greatly appreciated.

Thanks
0
Comment
Question by:davism
  • 7
  • 7
14 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 34112403
The checkboxes are not on the page, but in the table.  Normally you would use control.findcontrol(), but the table is not a server control.

Mabe this?

For i = i To Files.Length - 1
    Select Case i
        Case 1
            submitCtrl = chkAllItems1
        Case 2
0
 
LVL 1

Author Comment

by:davism
ID: 34112502
So, does that mean I have to do a Case statement for all possible chkAllItems boxes of I want to interate through all of them?

If I gave the table a name and then did a runat='server' would that work on that loop with the findcontrol?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34112785
You can try making the table a server control, but not sure what the parent of the checkboxes would be -- the table, the row, the cell?

Alternatively you could use the following function to search for the control in all of the page controls.  Not sure if it will work in a table, but it's worth a shot.


submitCtrl = CType(FindControlRecursive(Me,"chkAllItems" & i), HtmlInputCheckBox)

	Public Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control

		If (root.ID = ID) Then
			Return root
		End If

		For Each c As Control In root.Controls

			Dim t As Control = FindControlRecursive(c, id)
			If (t IsNot Nothing) Then
				Return t
			End If

		Next

		Return Nothing

	End Function

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:davism
ID: 34112942
I did try the recursive but that didn't work.

The checkboxes and table are on the page as indicated by the view-source so why would it not be there to find? (just curious)

I will try to name and runat='server' on the table and see.

0
 
LVL 32

Expert Comment

by:Erick37
ID: 34113281
I just tested the code using your table and the checkbox was found.


	Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
		Dim c As Control = FindControlRecursive(Me, "chkAllItems2")
		If c IsNot Nothing Then
			Debug.WriteLine(c.ID)
			Debug.WriteLine(CType(c, HtmlInputCheckBox).Checked)
		Else
			Debug.WriteLine("Nothing")
		End If

	End Sub

Open in new window

0
 
LVL 1

Author Comment

by:davism
ID: 34113417
I just tried it as well and it did not find it. :-(

What the hell? Are you using it in a with masterpage and this is in the contentplaceholder?

This is even though that the names and id's are not having the masterpage and placeholder info (i.e. ctl00, etc)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34113627
Yep.  Here is the aspx.
<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Default.aspx.vb" Inherits="EricWeb._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.</p>
	<p>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </p>

	<table border="1" align="center">
     <tr><td valign='top' align='center'>
			<input id='chkAllItems0' name='chkAllItems0' type='checkbox' runat='server' title='Flag'/>
			<a href="./PictList/Vacation/Vac2007/IMG_0184.jpg" target="_blank">
			<img src=./PictList/Vacation/Vav2007/IMG_0184.jpg width= 95 height=95 border="0"></a>
		</td>
		<td valign='top' align='center'>
			<input id='chkAllItems1' name='chkAllItems1' type='checkbox' runat='server' title='Flag'/>
			<a href="./PictList/Vacation/Vac2007/IMG_0185.jpg" target="_blank">
			<img src=./PictList/Vacation/Vac2007/IMG_0185.jpg width= 95 height=95 border="0"></a>
		</td>
		<td valign='top' align='center'>
			<input id='chkAllItems2' name='chkAllItems2' type='checkbox' runat='server' title='Flag'/>
			<a href="./PictList/Vacation/Vac2007/IMG_0186.jpg" target="_blank">
			<img src=./PictList/Vacation/Vac2007/IMG_0186.jpg width= 95 height=95 border="0"></a>
		</td>
	 </tr>
	 </table>


</asp:Content>

Open in new window

0
 
LVL 1

Author Comment

by:davism
ID: 34113639
Let me a little further explain this.

I am using this in the code which is in the contentplaceholder:

table ID="Pictures" border="1" align="center">
     <asp:Literal ID="Literal1" Runat="server"/></table>

In the code-behind when I am displaying the pictures I do this:

Literal1.Text += "<td valign='top' align='center'>" & _
                "<input id='chkAllItems" & i & "' name='chkAllItems" & i & "' type='checkbox' runat='server' title='Flag'/>" & _
                "<a href=""" & MainPath & Files(i).Name & """ target=""_blank""><img src=" & _
                MainPath & Files(i).Name & swidth & sheight & " border=""0""></a></td>"

I do some other things but I finish it up with:

Literal1.Text += "</tr>"

Is all this why I am not able to find the control? If so, what can I do?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34113840
That is the problem exactly.  Those controls do not exist until they are rendered by the Literal control and so are not available to the code behind.

Is there a way you could place the table inside a Panel control and toggle the Visible property of the panel in the code behind?  This approach will make your controls available to the code.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34113876
Another option would be to use Javascript on the client side to find the controls.
0
 
LVL 1

Author Comment

by:davism
ID: 34113988
I was thinking about the javascript as well with the client side. But that's the thing it's client side. If you noticed I used the Files in the example. The Files are on the server side and not the client side.Could I mix by doing a script injection? Not sure but I may entertain that.

If I do the panel option the render of those checkboxes are not going to be readily available when it's rendered. Will that have an impact when I use the panel as the container when finding the control? The items or pictures that have the checkbox are dynamically generated. Hence, the literal. This is why I'm asking the question on the panel option.

Please let me know.

Thanks
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 125 total points
ID: 34114144
I am assuming you will have a variable number of images/checkboxes to display and that is reason for the dynamic code.  This makes the Panel idea not work so well.

Depending on what you need to do, you could keep the dynamic code as-is to generate the table.
Your javascript could loop through the checkboxes and do some action based on the checked state.  The script would need to know the number of checkboxes that were generated for loop control, ut that can be stored in a hiddenfield or the script could also be dynamically generated.
0
 
LVL 1

Author Comment

by:davism
ID: 34114164
Yep, that's where I'm going with the hiddenvalue with a runat='server'

This is going to be a little interesting. and hopefully it'll work out.

Let me give this a shot and I'll get back to you.
0
 
LVL 1

Author Closing Comment

by:davism
ID: 34228045
Sorry, I completely spaced this one out.

The solution with the Javascript has been working out great. I worked with a few things on a loop structure but it appears to be working as expected.

Thank you very much for the assistance.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
FInd Image Control Gridview 3 20
ASP.NET (VB) return a record 2 35
Please explain purpose of GZIP 4 34
asp.net open new page without popup blocker 8 18
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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