Connecting Tech Pros Worldwide Help | Site Map

Finding Value of Dynamic Control in MasterPage/ContentPage System

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 15th, 2006, 04:25 PM
dawg1998
Guest
 
Posts: n/a
Default Finding Value of Dynamic Control in MasterPage/ContentPage System

I have a page that creates dynamic textboxes based on the number of
fields a user chooses to fill out.

This process worked great when the page was standalone. However, when I
move to a Content/MasterPage setup, the MasterPage Form seems to be
interfering with the ability of my code to retrieve the value in the
dynamic control.

Are there any ideas on why this is happening or how to work-around the
problem? What is the syntax to find a dynamic control within a form
established on a Master Page? The two methods I have tried so far
always gets me the "Object reference not set to an instance of an
object" error.

A quick example is below. The name of the form established in the
MasterPage is "frmForm". I must place the form tags within the
MasterPage because of the presence of a SiteMap menu control that
resides in the MasterPage.

<%@ Page Language="VB" MasterPageFile="~/Sys_Masters/MasterPage.master"
title="Dynamic Control in Master Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphContent"
Runat="Server">

<script runat="server">

Sub Page_Load()

Dim txtbxTextBox As New TextBox()
txtbxTextBox.ID = "txtbxTextBox"
phPlaceHolder.Controls.Add(txtbxTextBox)

End Sub

Sub btnSubmit_OnClick(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim txtbxTextBox As New TextBox()

'// Neither of the two methods below finds the control
'txtbxTextBox =
Master.FindControl("frmForm").FindControl("phPlace Holder").FindControl("txtbxTextBox")
'txtbxTextBox = CType(Master.FindControl("frmForm").FindControl
"phPlaceHolder").FindControl("txtbxExample"), TextBox)

lblValue.Text = txtbxTextBox.Text


End Sub


</script>

<asp:Label ID="lblTitle" runat="server" Text="Dynamic Control in Master
Page Test: "/>
<p />
<asp:PlaceHolder ID="phPlaceHolder" runat="server"/>
<p />
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_OnClick"
Text="Submit" />
<p />
<asp:Label ID="lblValue" runat="server" />

</asp:Content>


  #2  
Old March 15th, 2006, 09:05 PM
Simon
Guest
 
Posts: n/a
Default Re: Finding Value of Dynamic Control in MasterPage/ContentPage System

Using a masterpage prepends a string onto the front of your control
names, usually something like "ctl00_<contentplaceholdername>_".
(replacing <contentplaceholdername> with the name of the
ContentPlaceHolder the control was created in.
You would then use FindControl("ctl00_whatever_yourcontrolname").
I have no idea why this is, it drove me nuts for a while.
Hope this helps.

Simon

  #3  
Old March 16th, 2006, 04:25 PM
Simon
Guest
 
Posts: n/a
Default Re: Finding Value of Dynamic Control in MasterPage/ContentPage System

Oops, I realized this morning that the page I was having that problem
in had never been run so I didn't realize that i was talking out of my
ass since that solution doesn't work.
What ended up working was first getting the ContentPlaceHolder control
and then using findcontrol from there:
ContentPlaceHolder mainContent;
mainContent = (ContentPlaceHolder)Master.FindControl("MainFormHo lder");

DropDownList ddl =
(DropDownList)mainContent.FindControl("controlname ");

That one ought to work for you, sorry about the idiocy :)
Simon

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.