Home All Groups Group Topic Archive Search About
Author
21 Apr 2005 7:39 AM
thomas via DotNetMonster.com
I create throug LinkButton several dynamic textboxes. I've got two methods
two register the control, that the don't get lost on the postback!

private void registerDynConrol(System.Web.UI.Control control)
{
  ArrayList dynCtlArray = (ArrayList) this.ViewState["dynCtlArray"];

  if(dynCtlArray == null)
  {
     dynCtlArray = new ArrayList();
     this.ViewState.Add("dynCtlArray", dynCtlArray);
  }
  dynCtlArray.Add(new Triplet(control.Parent.ID,conntrol.GetType()
..FullName, control.ID));

}

and i override the LoadViewState method:

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
ArrayList dynCtlArray = (ArrayList) this.ViewState["dynCtlArray"];

if(dynCtlArray != null)
{
Control parent;
Control control = new Control();
  foreach(Triplet ctlInfo in dynCtlArray)
  {
    parent = Page.FindControl(ctlInfo.First.ToString());
    Type type = Type.GetType("System.Web.UI.WebControls.TextBox,
System.Web,Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a", true);
    control = (Control) Activator.CreateInstance(type);
    control.ID = ctlInfo.Third.ToString();
    parent.Controls.Add(control);
    string s = ((TextBox) control).Text;                   
  }           

}
}

this two methods work properly and after the postback, the textbox is stil
there INCLUDING the text, which I wrote in before the postback! the only
problem is, that i can not read this string out during the LoadViewState-
methode:
string s = ((TextBox) control).Text;   
this string s is always "" (empty)!! and I have no clue why? although the
string is visible in the textbox, which i recreated in the  LoadViewState-
methode!

thanks for any help

Author
21 Apr 2005 9:48 AM
Brock Allen
You don't need to get low down with LoadViewState. Here's a sample to do
it another way:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">
    ArrayList DynamicControls
    {
        get
        {
            if (ViewState["DynamicControls"] == null)
            {
                ViewState["DynamicControls"] = new ArrayList();
            }
            return (ArrayList)ViewState["DynamicControls"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            for (int i = 0; i < DynamicControls.Count; i++)
            {
                string s = (string)DynamicControls[i];
                Control c = null;
                switch (s)
                {
                    case "t":
                        c = new TextBox();
                        break;
                    case "d":
                        c = new DropDownList();
                        break;
                    case "l":
                        c = new Label();
                        break;
                }
                if (c != null)
                {
                    _ph.Controls.Add(c);
                }
            }
        }
    }

    protected void _button1_Click(object sender, EventArgs e)
    {
        TextBox t = new TextBox();
        _ph.Controls.Add(t);

        DynamicControls.Add("t");
    }


    protected void _button2_Click(object sender, EventArgs e)
    {
        DropDownList d = new DropDownList();
        _ph.Controls.Add(d);

        d.Items.Add("Red");
        d.Items.Add("Green");
        d.Items.Add("Blue");

        DynamicControls.Add("d");
    }

    protected void _button3_Click(object sender, EventArgs e)
    {
        Label l = new Label();
        _ph.Controls.Add(l);

        l.Text = DateTime.Now.ToString();

        DynamicControls.Add("l");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat=server ID=_button1 Text="Add TextBox" OnClick="_button1_Click"
/>   
        &nbsp;
        <asp:Button runat=server ID=_button2 Text="Add DropDownList" OnClick="_button2_Click"
/>   
        &nbsp;
        <asp:Button runat="server" ID="_button3" Text="Add Label" OnClick="_button3_Click"
/>
        <br />
        <asp:PlaceHolder runat="server" ID="_ph"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>


-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> I create throug LinkButton several dynamic textboxes. I've got two
> methods two register the control, that the don't get lost on the
> postback!
>
> private void registerDynConrol(System.Web.UI.Control control)
> {
> ArrayList dynCtlArray = (ArrayList) this.ViewState["dynCtlArray"];
> if(dynCtlArray == null)
> {
> dynCtlArray = new ArrayList();
> this.ViewState.Add("dynCtlArray", dynCtlArray);
> }
> dynCtlArray.Add(new Triplet(control.Parent.ID,conntrol.GetType()
> .FullName, control.ID));
> }
>
> and i override the LoadViewState method:
>
> protected override void LoadViewState(object savedState)
> {
> base.LoadViewState (savedState);
> ArrayList dynCtlArray = (ArrayList) this.ViewState["dynCtlArray"];
> if(dynCtlArray != null)
> {
> Control parent;
> Control control = new Control();
> foreach(Triplet ctlInfo in dynCtlArray)
> {
> parent = Page.FindControl(ctlInfo.First.ToString());
> Type type = Type.GetType("System.Web.UI.WebControls.TextBox,
> System.Web,Version=1.0.5000.0, Culture=neutral,
> PublicKeyToken=b03f5f7f11d50a3a", true);
> control = (Control) Activator.CreateInstance(type);
> control.ID = ctlInfo.Third.ToString();
> parent.Controls.Add(control);
> string s = ((TextBox) control).Text;
> }
> }
> }
> this two methods work properly and after the postback, the textbox is
> stil
> there INCLUDING the text, which I wrote in before the postback! the
> only
> problem is, that i can not read this string out during the
> LoadViewState-
> methode:
> string s = ((TextBox) control).Text;
> this string s is always "" (empty)!! and I have no clue why? although
> the
> string is visible in the textbox, which i recreated in the
> LoadViewState-
> methode!
> thanks for any help
>