Using FindControl() in global code base

G

Guest

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?
 
S

Scott Allen

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
 
G

Guest

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
 
G

Guest

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
 
S

Scott Allen

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,
 
S

Scott Allen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top