There has to be a better way...



  • currentControl is from a System.Web.UI.ControlCollection, so it is cast as a "Control". There has to be a better way to figure out what the actual subclass is.

     Current code:

    if (foundControl != null)
                    {
                        Type controlType = currentControl.GetType();                  
                       
                        if (controlType == typeof(Label))
                        {                       
                            Label control = (Label)currentControl;
                            control.Text = foundControl[_displayLabelColumn].ToString();
                        }
                        else if (controlType == typeof(Literal))
                        {
                            Literal control = (Literal)currentControl;
                            control.Text = foundControl[_displayLabelColumn].ToString();
                        }
                        else if (controlType == typeof(LinkButton))
                        {
                            LinkButton control = (LinkButton)currentControl;
                            control.Text = foundControl[_displayLabelColumn].ToString();
                        }
                       
                    }

     Repeat for every control that has a text property.



  •  Of course there's a better way.  I'll pseudocode it.

    1)  Generate random string of characters s.

    2)  if s = type goto 4

    3)  goto 1

    4) ??? 

    5)  Profit!



  • @belgariontheking said:

    Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?

    So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.



  • @AbbydonKrafts said:

    @belgariontheking said:
    Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?

    So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.

     

    Why the fuck doesn't it do that for me?



  • @dhromed said:

    Why the fuck doesn't it do that for me?
     

    Because you are not special enough.



  • @MasterPlanSoftware said:

    Because you are not special enough.

    :'( 



  • @belgariontheking said:

     Of course there's a better way.  I'll pseudocode it.

    1)  Generate random string of characters s.

    2)  if s = type goto 4

    3)  goto 1

    4) ??? 

    5)  Profit!

     

    Hey MasterPlanSoftware, whats step 4?


  • Winner of the 2016 Presidential Election

    I was dealing with this same issue. I made a simple class that is used like so:

    string strValue = new ValueControl( myControl ).Value;

    The class figures out if it's using Text, Checked, or SelectedValue based on the kind of control. Then I was able to write a loop for all controls in a form to collect all the values, without having to do any special-cases.

    Edit: Yes, I know, a function could have done the same. The class actually did a few more things than that, though.



  • @joe.edwards@imaginuity.com said:

    I was dealing with this same issue. I made a simple class that is used like so:

    string strValue = new ValueControl( myControl ).Value;

    The class figures out if it's using Text, Checked, or SelectedValue based on the kind of control. Then I was able to write a loop for all controls in a form to collect all the values, without having to do any special-cases.

    Edit: Yes, I know, a function could have done the same. The class actually did a few more things than that, though.

     

     Thats kind of backwards from what I need. I'm setting the text property, not retrieving it.

    This code is for this stupid localization system. Basicly, translators can use a tool to translate all the pages, which stores all text in a couple of database tables (mapping PageID and ControlID). Then when the page is rendered, all of the text gets swapped out with the proper localization.

     I fought long and hard for a different solution, but this is what we have to build. I did build in cacheing and defaulting to english without running the sproc, so it will only be a slow site for our global customers.



  •  @Jonathan Holland said:

    if (foundControl != null)
                    {
                        Type controlType = currentControl.GetType();                  
                       
                        if (controlType == typeof(Label))
                        {                       

    This kind of crud is all over the UI for a tool I do some maintenance on.  What's worse is that the types are most often types the original author wrote together, and gave each and every one a non-virtual copy of the same property.

     



  • Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control.  So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?

     

    e.g.

    System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;

    if(control!=null)

        control.Text = foundControl[_displayLabelColumn].ToString();

     

     



  • @Morbii said:

    Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control.  So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?

     

    e.g.

    System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;

    if(control!=null)

        control.Text = foundControl[_displayLabelColumn].ToString();

     

     

     

     

    This is WebForms, not winForms.

     System.Web.UI.WebControls.Label is a subclass of System.Web.UI.Control.

    This allows Label to become a part of ControlCollection.

     However, when iterating a ControlCollection:

    foreach (Control c in ControlCollection)

    you end up with the parent class, Control, not the subclass, hence having to check the type and then cast it again.

    Control only exposes common virtual properties, since text is only on a handfull of subcontrols, it is not a property of control. 

     Ugly.


     



  •  

     AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary...

    In
    Flex2 depending on the component you can tell if it has a label or
    "text" (yes there is a difference, no i don't know why couldn't they
    jsut use text property everywhere)... So you can just see
    .hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
    set that property.

    In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks.

    (go Ruby!)



  • @dlikhten said:

     

     AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary...

    In
    Flex2 depending on the component you can tell if it has a label or
    "text" (yes there is a difference, no i don't know why couldn't they
    jsut use text property everywhere)... So you can just see
    .hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
    set that property.

    In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks.

    (go Ruby!)

     

    Programming via Reflection could be implemented here, but I think that would make a slow solution even slower.

     What I really want to do is this:

    Type controlType = currentControl.getType()

    (currentControl as controlType).text = Foo;


    Yes, that looks like I'm casting an object to its own type, so it won't work. But it makes sense in my head dammit.



  • @AbbydonKrafts said:

    @belgariontheking said:
    Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?

    So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.

    I don't think it was clear in my tag that this happens when I type my post, not my tag.  

    I can't wait until this becomes part of the tag cloud. 



  • create a Interface, call it HasLabelControl.  There does not need to be any methods in the interface.

    Create a subclass of each control you're interested in (Literal, LinkButton,  Label, etc).  For example, ourLiteral, ourLinkButton, etc.  

    For a class like ourLiteral, you'd inherit from Literal and HasLabelControl.  The ourLiteral class does not need any members, because all the functionality is in Literal already.  Use ourLiteral where you'd use a Literal.  (and the same for the others...)

     Then your code can be (I think, you'd have to check the syntax)

    HasControl c = foundControl as HasLabelControl;
    if (c != null) c.Text = ..........;




  • @Jonathan Holland said:

    @dlikhten said:

     

     AH the beauty of dynamic languages... As long as you follow convention then figuring out who you are is unnecessary...

    In
    Flex2 depending on the component you can tell if it has a label or
    "text" (yes there is a difference, no i don't know why couldn't they
    jsut use text property everywhere)... So you can just see
    .hasOwnProperty("text") or .hasOwnProperty("label") and then just poof
    set that property.

    In strongly typed languages I suppose you can make a class that just takes the object, introspects it, and returns a function for setting the Text property (unless its not a function in which case its even worse), if null is returned then wah-wah, otherwise just set it. and no more giant if blocks.

    (go Ruby!)

     

    Programming via Reflection could be implemented here, but I think that would make a slow solution even slower.

     What I really want to do is this:

    Type controlType = currentControl.getType()

    (currentControl as controlType).text = Foo;


    Yes, that looks like I'm casting an object to its own type, so it won't work. But it makes sense in my head dammit.

     

    Just letting you know... This is solved by using.... get this... Inheritence...

    If all your objects extends something called UIComponent and part of UI component is a text property... or say ControlComponent in your case. Just cast to ControlComponent and use ControlComponent.Text = "xxx makes the world go round".

    You can use interfaces instead if you don't want to change behavior. Using interfaces you can isolate behaviors and just check if its an instance of that interface.



  • @belgariontheking said:

    @AbbydonKrafts said:

    @belgariontheking said:
    Filed under: FUCKING HELL WHY DOES IT DELETE TWO CHARACTERS WHEN I HIT BACKSPACE ONCE?

    So it's not just me. I brought up a bunch of tag flaws in another sidebar post, and this was one of them. I'm gonna use that tag now.

    I don't think it was clear in my tag that this happens when I type my post, not my tag.  

    I can't wait until this becomes part of the tag cloud. 

     

    Let the revolution begin!



  • @dogbrags said:

    create a Interface, call it HasLabelControl.  There does not need to be any methods in the interface.

    Create a subclass of each control you're interested in (Literal, LinkButton,  Label, etc).  For example, ourLiteral, ourLinkButton, etc.  

    For a class like ourLiteral, you'd inherit from Literal and HasLabelControl.  The ourLiteral class does not need any members, because all the functionality is in Literal already.  Use ourLiteral where you'd use a Literal.  (and the same for the others...)

     Then your code can be (I think, you'd have to check the syntax)

    HasControl c = foundControl as HasLabelControl;
    if (c != null) c.Text = ..........;


     

     I understand how interfaces work. Doing that would involve rewriting all of the pages that use these controls to use the new controls that implement IOverComplicated.

     



  • @Jonathan Holland said:

    @Morbii said:

    Perhaps I'm reading the docs wrong, but doesn't System.Web.UI.WebControls.Label inherit from System.Windows.Forms.Label to get its Text member, which is then inherited from System.Windows.Forms.Control.  So... can't you just cast it as a System.Windows.Forms.Control and use the Text property?

     

    e.g.

    System.Windows.Forms.Control control = currentControl as System.Windows.Forms.Control;

    if(control!=null)

        control.Text = foundControl[_displayLabelColumn].ToString();

     

     

     

    This is WebForms, not winForms.

     

    I realized that this was WebForms, but it was my error anyway.  I was actually looking at the Microsoft.Office.Tools.Excel.Controls namespace whose Label class does derive from System.Windows.Forms and I assumed I was looking at System.Windows.WebForms.  Oops :o


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.