Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 August 20th, 2003, 07:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default Declaring and instantiating...

Truly a beginner... Well, a asked the question http://p2p.wrox.com/topic.asp?TOPIC_ID=2638, and in relation to this I have now tried to switch to web controls instead of html controls, however...

I am doing an automated generation of user interfaces based on data retrieved from a database. In this process I have a switch that looks something like this
Code:
private WebControl CreateControl(ref OleDbDataReader reader)
{    
  WebControl control;
  switch(reader.GetInt32(2))
  {
    case 1: // textbox    
      control = new TextBox();                
      break; 
    case 2: // password    
      control = new TextBox();
      break; 
    case 3: // checkbox
      control = new CheckBox();    
      break; 

    ...

    default: // whatever
      control = null;
      break;             
  }

  if(!reader.IsDBNull(3)) 
    control.CssClass = reader.GetString(3);
  return control;
}
This can work however I can only use the properties of the WebControl class (e.g. CssClass); not those from e.g. TextBox even though 'control' has been instantiated as such.

Is there a way to do this in a smart way so that I do not have to declare each different child class of the WebControl class seperately?

Thanks

Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old August 20th, 2003, 10:22 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You might try just having the function return Object so you can properly return any web control. Presumably, you are taking the result from this function and putting it in a Controls collection so having the function return Object shouldn't be a problem.
 
Old August 21st, 2003, 07:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried to return an Object instead, however that is even more general than the WebControl instance that I returned before, and therefore I could not set the general WebControl properties.

The thing is that the WebControl is too general too, and therefore when assigning e.g. TextBox properties, the compiler still thinks that it is just a WebControl and not the child (TextBox).

A way around could be to pass the control collection to the function and then add the individual controls after doing somthing like this...
Code:
TextBox textBoxControl = new TextBox();
...in each of the cases of the switch, however this is not an elegant way of doing it. There must be a way to make the compiler aware that even though the object has been declared general as a WebControl, it has now been instantiated as a TextBox or another more specific WebControl, and can therefore e.g. assign to the TextMode property of the TextBox or some other property specific to the individual controls.

Thanks anyway.

Jacob.

 
Old August 21st, 2003, 08:59 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

One way or another you are going to have to cast the control created by this function into the desired web control so you can manipulate it's specific properties. It seems that you are trying to do only a few lines of code with this function. Perhaps it just makes more sense to do what you want back in the calling code. I'm making the presumption that you are calling this function from within a loop that's iterating thru a datareader. I guess I'm failing to see the desired outcome of this function short of creating a control and setting the CssClass. Are you trying to eliminate the need for repeating this chunk in each switch case?
Code:
if(!reader.IsDBNull(3))
    control.CssClass = reader.GetString(3);



 
Old August 21st, 2003, 10:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is quite true... I am iterating through some reader generating controls. The function shown is however not finished. The thing is that I want to assign properties in the switch aswell.

It took quite a while to figure it out, but the answer was quite simple. Must answers are when you know the answer :). The top of the function now looks like this...
Code:
private WebControl CreateControl(ref OleDbDataReader reader)
{    
    string dbTable = "";
    WebControl control;
    OleDbDataReader controlReader = null;
    switch(reader.GetInt32(2))
    {
        case 1:    // textbox
            control = new TextBox();
            dbTable = "ControlTEXT";
            break; 
        case 2:    // password    
            control = new TextBox();
            ((TextBox)control).TextMode = TextBoxMode.Password;
            dbTable = "ControlPASSWORD";
            break;


The casting does the trick. I can set the properties I want and still return the WebControl variable. E.g. in...
Code:
((TextBox)control).TextMode = TextBoxMode.Password;
Thanks for your help.

Jacob.






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Role Provider not instantiating jsqrd ASP.NET 2.0 Basics 0 March 26th, 2007 05:24 PM
Instantiating Interfaces simsdagr8 C# 2005 4 November 30th, 2006 07:37 AM
Help!! Problem instantiating WORD object using C#. handymulia ASP.NET 1.0 and 1.1 Basics 3 November 16th, 2006 05:18 PM
Instantiating COM interop classes planoie VS.NET 2002/2003 0 September 30th, 2003 01:47 PM





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