8 Replies - 2207 Views - Last Post: 30 November 2012 - 07:49 AM Rate Topic: -----

#1 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 07:47 AM

Greetings all,
So, I have this thing where I'm pulling a string of data from a table and I need to populate multiple controls some are in ASCX files and others are just textboxes on the page.

I'm putting the string into an array and splitting it on the "\t" tab delimiter.
But, after that, I'm stuck on how to put that data into all the controls.

var custId = rcbarchive.SelectedValue;
            var newCustId = Convert.ToInt32(custId);

            using (CP_McCain db = new CP_McCain())
            {
                var dt = db.vdpCustomizationCollection.GetByPrimaryKey(newCustId);
                var vdpDataString = dt.VDPDataString;

                const char tabDelimiter = '\t';
                string[] result = vdpDataString.Split(tabDelimiter);

                foreach (var oResult in result)
                {
                    HeadlineTextTB.Text += oResult;//maybe?
                   //this is where I get lost
                   //need to find a control within an asxc file
                   //and populate it.
                }
            }


Any help today would be greatly appreciated. Thanks, all.

Is This A Good Question/Topic? 0
  • +

Replies To: asp.net c# populate multiple controls on page from tab delimited array

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 08:19 AM

I'm not sure in this situation that a foreach is the way to go. If the string is always the same size and each element will always correspond to the same control (ie. TextBox1 always gets element[0], TextBox2 always gets element[1], etc.) then you could do something like

TextBox1.Text = element[0];
TextBox2.Text = element[1];
UserControl1.TextBoxOneText = element[2];



In order to get access to the controls in the UserControl I would create a public property for each control I needed access to. So in the UserControl code file

public string TextBoxOneText
{
    get
    {
        return SomeTextBox.Text;
    }
    set
    {
        SomeTextBox.Text = value;
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 09:04 AM

I just posted a long winded response, but it didn't actually post... so let me try this again.

Thanks for the quick reply. I appreciate it.
You have a valid point - but the string value may not always be the same length. It will vary from user to user. There are about 6 text boxes, and about 12-15 other fields on the page contained in User Controls (.ascx files). Incidentally, if there are no values to populate those other fields, they will not be visible to the end user.

You mentioned "element[0]", "element[1]", etc... I'm assuming you are referring to my use of the declared variable "result". So my question is this: would your reference to "element" be the equivalent of "result"? meaning, "result[0]", "result[1]", etc?

I ask because I'm not the world's best coder, and I love to know the why(s) and why not(s) of programming.

Also, since some of these text boxes and drop downs ARE inside of ascx files, would something like this, work?
(btw, using some telerik controls, here)
UserControl userControl;
Control ctrl = Page.FindControl("SomeASCXFileControl");
userControl = (UserControl) ctrl;
RadComboBox SomeVariableNameForControl = userControl.FindControl("SomeExistingRadComboBox") as RadComboBox;
SomeVariableNameForControl.SelectedItem.Text = Result[0];


This post has been edited by mb2000inc: 29 November 2012 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 12:37 PM

Yeah, I used element but you'd just replace that with result as you said. For the user control I would really recommend making the property of each control that you want to set a value for be a property of the control as I showed in the previous post. This will keep you from having to do a bunch of FindControl methods and keep your code a lot cleaner. It's also a little quicker since the code has direct access to the control you're setting rather than having to search through the entire usercontrol to find a control.
Was This Post Helpful? 1
  • +
  • -

#5 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 01:24 PM

Ok, cool... so let me ask you this...
On the flip side of all that, I will potentially have 81 values that need populating. Would it be better to LOOP through all the controls and set the values in a foreach loop?
foreach (Control cntrl in SomePanelOfControls.Controls)
{
    for (int i = 0; i < 1; i++)
    {
         //start setting values with result[i] - being counted in above statement....?
    }
}



If so, I have the first text box is not in any control
then i have 12-15 controls in several UserControls that need populating...
would I do multiple loops? - if indeed, that would be a way to go?

Or should I just stick with what you have suggested?

Sorry for all the questions - I just want to do good clean work. ;)
Am I over thinking it and making it more difficult than it actually is?

This post has been edited by mb2000inc: 29 November 2012 - 01:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 02:18 PM

Ok, "Fast Reply" button doesn't work... this is the second time, I've had to redo a post... I think I need to report that, lol.


Ok, so I took your advice and kept it simple.

                HeadlineTextTB.Text = result[0];

                ProductOneOptionsTMS.ProdSelectedValue = result[1];
                ProductOneOptionsTMS.ProdBrandTbValue = result[2];
                ProductOneOptionsTMS.ProdProdNameValue = result[3];
                ProductOneOptionsTMS.ProdDescValue = result[4];
                ProductOneOptionsTMS.ProdDistValue = result[5];
                ProductOneOptionsTMS.ProdPackCaseValue = result[6];
                ProductOneOptionsTMS.ProdAvgSavCaseValue = result[7];
                ProductOneOptionsTMS.ProdSuggServSizVale = result[8];
                ProductOneOptionsTMS.ProdCostPieceTbValue = result[9];
                ProductOneOptionsTMS.ProdCostServValue = result[10];
                ProductOneOptionsTMS.ProdMenuPriceValue = result[11];
                ProductOneOptionsTMS.ProdOperatorProfValue = result[12];
                ProductOneOptionsTMS.ProdPrepInstructValue = result[13];



Only problem is that I get the first value "HeadlineTextTB.Text = result[0];"
after that - I get nothing.

The items that follow are blank - but I know there's values.
Suggestions?

(note, I have six sets of these - then three more text boxes after that)

This post has been edited by mb2000inc: 29 November 2012 - 02:18 PM

Was This Post Helpful? 0
  • +
  • -

#7 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 29 November 2012 - 06:11 PM

are you getting any errors? Did you step through in with the debugger to see what it's doing? What do those properties you created in the usercontrol look like?

I would think there's probably a better way to handle this, but I don't know enough about your requirements or how the data from this string relates to the controls or why some controls are in usercontrols while others aren't.

Trying to do it this way you're going to have to put in checks to make sure you don't get an index out of range exception. This will happen if you try to set the value of a control to an index of the string that doesn't exist. If you have 81 controls, but only 36 values then you want to make sure you're not attempt to access 37-81 of the array because they won't exist, if that makes sense.

This post has been edited by Nakor: 29 November 2012 - 06:12 PM

Was This Post Helpful? 1
  • +
  • -

#8 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 30 November 2012 - 06:59 AM

I didn't get any errors - they just showed up as blank.
The properties were basically, like you mentioned:

        public string ProdBrandTbValue
        {
            get { return Product_BrandTB.Text; }
            set { Product_BrandTB.Text = value; }
        }



There are four fields outside of the user controls - a headline field, a contact field, a company field, and a phone field. All of the other fields are a user control used 6 times, each named differently so as not to duplicate.
They all contain product data that can be edited by a user. That information is stored in a string that is used in customizing data contained in a generic restaurant menu (pdf format). It's actually a pretty slick application to customize any print jobs. The string format is required for the PDF output... and it works going in, but the client wants to be able to go back and re-edit somethings, if necessary, so they save it in their archives and want to be able to pull it back up and edit as needed, then re-order it.

This string needs to be split out (on the tab delimiter) and each of their selections/text, needs to be put back into their prospective textboxes and/or drop downs. Does that make more sense?

I agree about the checks, because if some of the selections/text may be null or empty. That would mean that those fields don't get shown on the page.
Was This Post Helpful? 0
  • +
  • -

#9 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: asp.net c# populate multiple controls on page from tab delimited array

Posted 30 November 2012 - 07:49 AM

I think I got it!!!

What's happening, is since some controls are not visible on page load, there has to be a check, before hand, to see if there is any data for the controls. If there IS data, then the controls need to be visible, then populated.

And here's another kicker. The guy who started this project was saving items out of order (even though it's a little ridiculous to do it that way, it's the way the web service calls for it). SO, I had to figure out which order things come and go.

OY! So, anywho... Thank you. Thank you for all your help. You're awesome.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1