By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Using FindControl() in global code base

P: n/a
I have created a method that accepts a string value (representing the name of a textbox) and then returns the value. Because the name of the textbox can change, I first build a string with the textbox name and then pass it to the FindControl() method to return the value. This works great when I use the method in the code-behind for the exact page that I want to use it on. However, I'd like to use this same method on other pages throughout my site, but when I try to move the method into a global code base ( and then instantiate the class in the individual pages), I always receive null as the return value. I am assuming that the problem is that the method FindControl() is not in the same naming container as the page calling it. My question is....How can I make this work?

Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi D:

Perhaps you could show us some code?
It's not clear on what control you are invoking FindControl now that
you've moved it into a common class.

--s

On Fri, 7 May 2004 05:26:06 -0700, D Sheldon
<an*******@discussions.microsoft.com> wrote:
I have created a method that accepts a string value (representing the name of a textbox) and then returns the value. Because the name of the textbox can change, I first build a string with the textbox name and then pass it to the FindControl() method to return the value. This works great when I use the method in the code-behind for the exact page that I want to use it on. However, I'd like to use this same method on other pages throughout my site, but when I try to move the method into a global code base ( and then instantiate the class in the individual pages), I always receive null as the return value. I am assuming that the problem is that the method FindControl() is not in the same naming container as the page calling it. My question is....How can I make this work?


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #2

P: n/a
Basically, I have a page which collects contact info about different people (ie...Person #1, Person #2, etc...) that are stored in a database as individual records. The information gathered is the same for each person, so I numbered the ID's like (txtFirst_Name_P1, txtFirst_Name_P2, txtLast_Name_P1, txtLast_Name_P2). That way, I can simply loop through an array to collect the values of each form field.

This works great when the GetValue() method is in the code-behind page, but when I move it to a global(common) code base, it always return "null".

I did a little research and it sounds like because the GetValues() method is not in the same naming container, it doesn't see the fields

PLease let me know if you need any additional info

Thanks

My code sort of looks like this

public void PopulateDB(

int[] intContacts = {1,2,3}
foreach(int i in intContacts

string strFirst_Name = GetValue("txtFirst_Name" + i.ToString())
string strLast_Name = GetValue("txtLast_Name" + i.ToString())
// and so forth and so on....there are about 15 more elements (hence the reason for the loop

public string GetValue(string strFieldName

TextBox ctlControlFound = FindControl(strFieldName) as TextBox
if(ctlControlFound !=null

return ctlControlFound.Text

els

return null


Nov 18 '05 #3

P: n/a
H
You need to pass the page to the method

public void PopulateDB(

int[] intContacts = {1,2,3}
foreach(int i in intContacts

string strFirst_Name = GetValue("txtFirst_Name" + i.ToString(), Page)
string strLast_Name = GetValue("txtLast_Name" + i.ToString(), Page)
// and so forth and so on....there are about 15 more elements (hence the reason for the loop


public string GetValue(string strFieldName, page myPage

TextBox ctlControlFound = myPage.FindControl(strFieldName) as TextBox
if(ctlControlFound !=null

return ctlControlFound.Text

els

return null

Bin Song, MCP
Nov 18 '05 #4

P: n/a
I think Bin hit on the key point here, which is: what control are you
invoking FindControl on? If GetValue is just another member function
of your WebForm, then there should not be a problem. If not, you'll
need to pass a control reference to call FindControl, for example:

public void PopulateDB()
{
....
string s = GetValue(this, theName);
....
}

and in your common code area...

public string GetValue(Page page, string name)
{
....
TextBox b = page.FindControl(name) as TextBox;
....
}

HTH,
--
Scott
http://www.OdeToCode.com

On Fri, 7 May 2004 10:21:02 -0700, D Sheldon
<an*******@discussions.microsoft.com> wrote:
Basically, I have a page which collects contact info about different people (ie...Person #1, Person #2, etc...) that are stored in a database as individual records. The information gathered is the same for each person, so I numbered the ID's like (txtFirst_Name_P1, txtFirst_Name_P2, txtLast_Name_P1, txtLast_Name_P2). That way, I can simply loop through an array to collect the values of each form field.

This works great when the GetValue() method is in the code-behind page, but when I move it to a global(common) code base, it always return "null".

I did a little research and it sounds like because the GetValues() method is not in the same naming container, it doesn't see the fields.

PLease let me know if you need any additional info.

Thanks.
My code sort of looks like this:
public void PopulateDB()
{
int[] intContacts = {1,2,3};
foreach(int i in intContacts)
{
string strFirst_Name = GetValue("txtFirst_Name" + i.ToString());
string strLast_Name = GetValue("txtLast_Name" + i.ToString());
// and so forth and so on....there are about 15 more elements (hence the reason for the loop)
}
}
public string GetValue(string strFieldName)
{
TextBox ctlControlFound = FindControl(strFieldName) as TextBox;
if(ctlControlFound !=null)
{
return ctlControlFound.Text;
}
else
{
return null;
}
}


Nov 18 '05 #5

P: n/a
Well, there are a couple ways to approach this.

First, you don't nessecarily need to pass the type. In .NET we can
determine the type using code. The method GetType, which is present on
every object, returns a Type instance to represent the exact runtime
type of the object you are examining. Example:

string typeName = ctlControlFound.GetType().Name;

and you could switch on the name:

switch(typeName)
{
case "TextBox": ... break;
case "DropDownList": ... break;
}

The disadvantage to the above code is performance. It's expensive to
do a GetType and all these string comparisons, especially since you
indicated you'd be iterating through a large number of controls.

A more performant method would be to pass the type in, perhaps declare
an enum class with the possible type you might pass in....

enum MyTypes
{
TextBox,
DropDownList,
....
}

and then

public string GetValue(Page page, string fieldName, MyTypes theType)
{
....

switch(theType)
{
case TextBox: .... break;
}
....
}

Personally I'd prefer the first approach, depending on the size of the
site and how many users you need to support it's probably performant
enough. But only testing can tell.

--
Scott
http://www.OdeToCode.com

On Fri, 7 May 2004 13:01:07 -0700, D Sheldon
<an*******@discussions.microsoft.com> wrote:
Perfect! Thanks!

Now, here's another question.....

Currently, I have to create a version of the GetValue() method for each type of form object (ie drop down list, textbox, etc...). Is there a way that I can pass in the object type?

I tried this code with no luck.....

public string GetValue(Page objCallingPage, string strFieldName, object objFormObjectType)
{
objFormObjectType ctlControlFound = FindControl(strFieldName) as objFormObjectType;
if(ctlControlFound !=null)
{
switch(objFormObjectType)
{
case TextBox: return ctlControlFound.Text;
case DropDownList: return ctlControlFound.SelectedItem.Value.ToString();
}
}
else
{
return null;
}
}


Nov 18 '05 #6

P: n/a
Thanks for the help! I'll test out both methods to see which performs best.
Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.