locked
Dynamic control definition in the database RRS feed

  • Question

  • User-1758902976 posted

    I know we can add controls by defining 
    Dim objTextbox as new TextBox 
    objTextbox.ID="ID1"
    objTextbox.text="some text"

    Add this to a placeholder etc.

    But my requirement has been different where controls definition should be in the database.

    i.e How do I create a control based on the definition string stored in database.

    Say for example for 3 rows stored in a table (ID, Control_Definition)

    1, <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    2, <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    3, <asp:Button ID="Button2" runat="server" Text="Button" />

    There is no limitation to the attributes and no validation (if such attribute exists to that control).
    Also Ignore other details like events, stylesheet etc for now

    So by this was I can give user to define their controls by adding definition to database.

     

    Any ideal?

     

    thanks
    A Iqbal

    Wednesday, January 27, 2010 10:07 AM

Answers

  • User1960438654 posted

    I would consider the possibility create set of methods within the special Helper class for different controls for updating the properties. Then these methods could be named similarly (e.g. UpdateProperties()) and have different types of input parameters. Example:

    public class SuperHelper {
    
    public string TextValue;
    public string CaptionValue;
    
    public UpdateProperties (TextBox tb)
    {
    tb.Text = TextValue;
    }
    
    public UpdateProperties(Label lb)
    {
    lb.Text = CaptionValue;
    }
    
    }

    Then you can also implement appropriate initialization method for this class which is getting DataTable or DataRow and initialize all internal properties of the Helper class for usage within these other UpdateProperties methods.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2010 9:47 AM

All replies

  • User1960438654 posted

    The concrete format of storing the control definitions doesn't matter within your scenario consideration - even in case you are storing the exact string of the proper ASP.NET control description - you should parse it and create the controls by something like the following code snippet:

    TextBox tb=null;Label lb = null;Button bt = null;
    if (field2StringValue.ToLower().Contains("textbox"))
    tb = new TextBox();
    ... (for Label and Button cases) ...
    if (tb != null)<add control tb to your collection>
    ... (process Label and Button variants) ...

    Wednesday, January 27, 2010 10:29 AM
  • User-1758902976 posted

    Hi bigTrend,
    Thanks for quick reply.

    Yes, I am getting this.

    further question
    <asp
    :TextBox ID="TextBox1" runat="server" text="simple text" cssClass="class1"  ></asp:TextBox>

    1. tb = new TextBox();
    2. tb.text="simple text"


    In Line 2, is there a way to make this more general
    In detail:

    dim propName as string
    Dim PropertyValue as string
    propName="text"
    PropertyValue="simple text"

    'validate if propName for tb exist
    tb.propName=PropertyValue

    So it take care of assigning properties to their values

    How do I achieve this?


    Thursday, January 28, 2010 9:41 AM
  • User1960438654 posted

    I would consider the possibility create set of methods within the special Helper class for different controls for updating the properties. Then these methods could be named similarly (e.g. UpdateProperties()) and have different types of input parameters. Example:

    public class SuperHelper {
    
    public string TextValue;
    public string CaptionValue;
    
    public UpdateProperties (TextBox tb)
    {
    tb.Text = TextValue;
    }
    
    public UpdateProperties(Label lb)
    {
    lb.Text = CaptionValue;
    }
    
    }

    Then you can also implement appropriate initialization method for this class which is getting DataTable or DataRow and initialize all internal properties of the Helper class for usage within these other UpdateProperties methods.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2010 9:47 AM