Using FindControl() in global code base

Discussion in 'ASP .Net' started by =?Utf-8?B?RCBTaGVsZG9u?=, May 7, 2004.

  1. 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?
    =?Utf-8?B?RCBTaGVsZG9u?=, May 7, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?RCBTaGVsZG9u?=

    Scott Allen Guest

    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
    <> 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
    Scott Allen, May 7, 2004
    #2
    1. Advertising

  3. 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
    =?Utf-8?B?RCBTaGVsZG9u?=, May 7, 2004
    #3
  4. 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
    =?Utf-8?B?QmluIFNvbmcsIE1DUA==?=, May 7, 2004
    #4
  5. =?Utf-8?B?RCBTaGVsZG9u?=

    Scott Allen Guest

    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
    <> 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;
    > }
    >}
    >
    Scott Allen, May 7, 2004
    #5
  6. =?Utf-8?B?RCBTaGVsZG9u?=

    Scott Allen Guest

    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
    <> 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;
    > }
    >}
    Scott Allen, May 7, 2004
    #6
  7. Thanks for the help! I'll test out both methods to see which performs best.
    =?Utf-8?B?RCBTaGVsZG9u?=, May 10, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lau Lei Cheong
    Replies:
    2
    Views:
    5,072
    Lau Lei Cheong
    Dec 15, 2004
  2. =?Utf-8?B?emFtYml6emk=?=

    Using FindControl on editable datagrid

    =?Utf-8?B?emFtYml6emk=?=, Jun 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,776
    =?Utf-8?B?emFtYml6emk=?=
    Jun 3, 2005
  3. jobs
    Replies:
    0
    Views:
    401
  4. eiji
    Replies:
    5
    Views:
    454
    Rolf Magnus
    Dec 6, 2005
  5. OK
    Replies:
    5
    Views:
    609
    Peter Bromberg [C# MVP]
    Nov 26, 2007
Loading...

Share This Page