8 Replies - 374 Views - Last Post: 06 May 2020 - 05:27 AM Rate Topic: -----

#1 DarenR   User is online

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,566
  • Joined: 12-January 10

Why cant i get text variable from control

Posted 05 May 2020 - 06:42 AM

So i am trying to change the text at run rime from an asp label control.

i can get the id and the label but the text option is not available?


foreach (Control C in this.Page.Form.Controls)
                        {
                            if (C is ContentPlaceHolder)
                            {
                                foreach (Control cphC in C.Controls) <--looks for the label
                                {

                                    if (cphC is Label) <-- finds the label (and it shows the text)
                                    {
                                        if (cphC.ID == dr[0].ToString())   <-- compares the label id to a dataset id 
                                        {
                                            cphC = dr[1].ToString(); <-- cant get the text property of cphC 
                                        }
                                    }

                                }


                            }

                        }


Is This A Good Question/Topic? 0
  • +

Replies To: Why cant i get text variable from control

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Why cant i get text variable from control

Posted 05 May 2020 - 06:53 AM

What type of application is this for? ASP.NET (classic)?

I'd guess (a wild guess) that cphC doesn't provide intellisense for a .Text, .Value (or even .Content) property because it is a generic Control; not all controls have such properties. You could probably cast it to (Label) [as it is a Label] to access the property(ies).
Was This Post Helpful? 1
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15782
  • View blog
  • Posts: 63,261
  • Joined: 12-June 08

Re: Why cant i get text variable from control

Posted 05 May 2020 - 06:54 AM

Gotta get your cast on!

   foreach (Control item in Page.Form.Controls)
            {
                if (item is Label)
                {
                    Label foo = (Label)item;

                    foo.Text = "nard";
                }
            }

Was This Post Helpful? 1
  • +
  • -

#4 DarenR   User is online

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,566
  • Joined: 12-January 10

Re: Why cant i get text variable from control

Posted 05 May 2020 - 06:58 AM

cant believe my dumb ass forgot to cast it. ugh
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15782
  • View blog
  • Posts: 63,261
  • Joined: 12-June 08

Re: Why cant i get text variable from control

Posted 05 May 2020 - 07:07 AM

That's why I am writing my own language with zero casting, no nulls, no zeros, and trinary switching. Hahaha
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Why cant i get text variable from control

Posted 05 May 2020 - 07:20 PM

And if you are using C# 7.0, you can skip the cast:
foreach (Control item in Page.Form.Controls)
{
    if (item is Label foo)
    {
        foo.Text = "nard";
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 DarenR   User is online

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,566
  • Joined: 12-January 10

Re: Why cant i get text variable from control

Posted 06 May 2020 - 04:50 AM

so i built a nice routine to find all the controls on the page and change the words according to what was in the database --
i was told it was to complicated to do that and just to hard code the wording -- eek


i am starting to see that they prefer everything to be hard coded instead of dynamic..
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7447
  • View blog
  • Posts: 25,085
  • Joined: 05-May 12

Re: Why cant i get text variable from control

Posted 06 May 2020 - 05:24 AM

Do you work for a software development group or an IT department?

For me, doing the transition from software development to IT, the biggest annoyance/peeve is the preference of IT to do cut and paste rather than refactoring the code or scripts to support remove the repetition. Perhaps it is the attitude that "This is known to work, don't mess with it because we have limited testing resources (as in none), and we get grilled by the C level if it blows up in production with the question: why change something that ain't broke." and "There is no technical debt -- 90% of what we do is a one-and-done. For anything new, just lift the code/script from the previously done and tweak for another one-and-done." So basically a lot of CYA, and all innovation is done in a green field rather than doing incremental improvements. Apparently there is money for new stuff, but incremental improvements are nice to have in the lights on budget.
Was This Post Helpful? 0
  • +
  • -

#9 DarenR   User is online

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,566
  • Joined: 12-January 10

Re: Why cant i get text variable from control

Posted 06 May 2020 - 05:27 AM

i was a software engineer then became a devops and now back to software engineer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1