Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 6th, 2011, 11:14 PM
FOD FOD is offline
Authorized User
 
Join Date: May 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting string concatenations

I have a web form with 20 textboxes on it which I've named txtItem1 through txtItem20. Ultimately my goal is to collect the data from these 20 textboxes and place them in an array (and eventually my database) using a loop through the textbox numbers (rather than individually call them out and add them). When I attempt to concatenate the variable name "txtItem" + i++ + ".text" in a do while loop all it adds to the array is the string of the concatenation. How do I to convert/cast the concatenation so that it provides the same result as txtItem1.text without treating it as just text?

I'm guessing the answer is simple and I'm just overlooking it.

Thanks in advance!
 
Old September 7th, 2011, 07:46 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Here are few hints

http://dotnetdud.blogspot.com/2011/0...arraylist.html

http://www.dreamincode.net/forums/to...-of-textboxes/

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old September 7th, 2011, 10:14 PM
FOD FOD is offline
Authorized User
 
Join Date: May 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Shasur I'll check them out and let you know if I have any questions!
 
Old September 8th, 2011, 08:26 AM
Registered User
 
Join Date: Sep 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by FOD View Post
I have a web form with 20 textboxes on it which I've named txtItem1 through txtItem20. Ultimately my goal is to collect the data from these 20 textboxes and place them in an array (and eventually my database) using a loop through the textbox numbers (rather than individually call them out and add them). When I attempt to concatenate the variable name "txtItem" + i++ + ".text" in a do while loop all it adds to the array is the string of the concatenation. How do I to convert/cast the concatenation so that it provides the same result as txtItem1.text without treating it as just text?

I'm guessing the answer is simple and I'm just overlooking it.

Thanks in advance!
Hi,

i think if u create a instance for textbox in a loop and add i++ to that instance than it may work.

i am not sure about this but i am giving just suggetion.

regards,
Pushpa B
 
Old September 13th, 2011, 12:09 AM
FOD FOD is offline
Authorized User
 
Join Date: May 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Null Reference Exception error

So I thought I had figured it out but now I am getting a Null Reference Exception even though the textboxes have data in them. Just to clarify what I am trying to do is pull the text out of a bunch of textboxes so I can push it over to my database using EF. Any thoughts on what may be causing the error and how I should fix it?

for (int i = 1; i <= 20; i++)
{
var quicklistitems = new QuickListItem();
quicklistitems.ListID = quicklist.ID;
quicklistitems.Item = ((TextBox)FindControl("txtListItem" + i)).Text;
context.QuickListItems.AddObject(quicklistitems);
}


Thanks,
FOD
 
Old May 1st, 2014, 01:58 AM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

This method concatenating your text boxes together should work. However, there is 1 minor flaw with "FindControl", it only finds controls that are a direct child of the page/control being searched. If you know that the control exists within the page/control and its not being found when you do your search it is more than likely a grandchild of the page/control that you are calling "FindControl" on. In this instance you need a recursive search to traverse all children and grandchildren controls until it is found or null if not found.


Code:
public Control FindControlRecursive( Control root, String controlId )
{
     Control rVal = null;
      
     if( root.ID == controlId )
           rVal = root;
     else
     {
          // perform recursive depth first search until control found
          foreach( Control c in root.Controls )
          {
               rVal = FindControlRecursive( c, controlId )
               if( rVal != null )
                     break; // found control stop looking 
          }
     }
    return rVal; // we return the control or null if not found
}

public Control FindControlRecursive( Page root, String controlId )
{
      Control rVal = null;
      
      // perform recursive depth first search until control found
      foreach( Control c in root.Controls )
       {
            rVal = FindControlRecursive( c, controlId )
            if( rVal != null )
                 break; // found control stop looking 
       }
}

Last edited by mmorgan30; May 1st, 2014 at 09:32 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
converting node to string mister_mister XSLT 2 January 24th, 2008 12:23 PM
Converting a string to an expression Nick.Net VB.NET 2002/2003 Basics 1 March 5th, 2004 09:41 PM
Converting String to an integer Louisa VB.NET 2002/2003 Basics 2 March 5th, 2004 10:25 AM
Converting int to string Judex C++ Programming 3 March 1st, 2004 10:44 AM
Converting a string to a number steve456 Javascript How-To 2 November 17th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.