473,597 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using FindControl() in global code base

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
6 1628
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*******@disc ussions.microso ft.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
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_P 2, 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("txtFi rst_Name" + i.ToString())
string strLast_Name = GetValue("txtLa st_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(str FieldName) as TextBox
if(ctlControlFo und !=null

return ctlControlFound .Text

els

return null


Nov 18 '05 #3
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("txtFi rst_Name" + i.ToString(), Page)
string strLast_Name = GetValue("txtLa st_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.FindCont rol(strFieldNam e) as TextBox
if(ctlControlFo und !=null

return ctlControlFound .Text

els

return null

Bin Song, MCP
Nov 18 '05 #4
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.FindContro l(name) as TextBox;
....
}

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

On Fri, 7 May 2004 10:21:02 -0700, D Sheldon
<an*******@disc ussions.microso ft.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_P 2, 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("txtFi rst_Name" + i.ToString());
string strLast_Name = GetValue("txtLa st_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(str FieldName) as TextBox;
if(ctlControlFo und !=null)
{
return ctlControlFound .Text;
}
else
{
return null;
}
}


Nov 18 '05 #5
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 "DropDownLi st": ... 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*******@disc ussions.microso ft.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 objFormObjectTy pe)
{
objFormObjectTy pe ctlControlFound = FindControl(str FieldName) as objFormObjectTy pe;
if(ctlControlFo und !=null)
{
switch(objFormO bjectType)
{
case TextBox: return ctlControlFound .Text;
case DropDownList: return ctlControlFound .SelectedItem.V alue.ToString() ;
}
}
else
{
return null;
}
}


Nov 18 '05 #6
Thanks for the help! I'll test out both methods to see which performs best.
Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8502
by: clintonG | last post by:
Briefly stated, my problem is accessing and 'setting' properties of a label control declared in the template of another control noting that the other control is an instance of the beta 2 DetailsView control which supports a PagingTemplate. Somewhere I grabbed some declarations for a PagerTemplate to display paging as: <<First < Prev Next> Last>>
0
1896
by: zhaoJian | last post by:
Here it is my code ,but it can't update the database.How to do it ? In _UpdateUnit event, I can not get the original value to @Original_UnitID,so I set a hidden column named LabelKey.But It don't update the database.My server is SqlServer 2000. using System; using System.Collections;
4
2797
by: Kevin Phifer | last post by:
Ok, before anyone freaks out, I have a solution I need to create that gathers content from maybe different places. Each one can return a <form> in the html, so its the classic can't have more than one runat=server form on a asp.net page. However, I still want developers to be able to use asp.net controls to create some apps that are created on the page. So I need multiple forms on a asp.net page(doesn't everyone). I purchased the...
14
5772
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a button. Is this possible? & what is the BEST APPROACH for doing this? & also if any links are there do tell those to me too coz I have no idea how to go about doing it.
2
2773
by: Kamal Ahmed | last post by:
Hi all, I want to execute a String that is generated at Runtime and fills data in DropDownList Controls at Runtime. I want to do this at server side. As Eval functions works as client side function and there is not other function or command to do this example is here.. ("ddlSub" & CStr(nRow) & ".DataSource = dtrSubject") I want to evaluate this string to get name of control at runtime e.g. ddlSub1, ddlSub2, ddlSub3 and so on
1
2375
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
4
14980
by: Jim McGivney | last post by:
I am trying to form a string by concatenating the Text strings of a number of Label controls. The Labels are Label33 to Label64. I thought I would use a For Loop to increment through the labels. The code below does not work as Text is not a recognized property of myControl, which is the name of the control fetched by FindControl. Any suggestions as to how to use FindControl to obtain the Text value of the found control would be...
15
3896
by: | last post by:
I dynamically create controls (textboxes) in a repeater control. I know their names (eg. TextBox1). How do I find the text of TextBox1 in the Form? FindControl does not seem to work.
14
2957
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main reason you would want to do this is for example to trigger something from an OnPaint event without resorting to boolean switches-- say if a user presses the "M" key while the program is Painting, the user gets the PaintHandler to do something else. ...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5426
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3881
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2399
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.