Adding dynamically user control (ASCX) into asp.net page and handling OnClick event

Discussion in 'ASP .Net Building Controls' started by Frank, Nov 24, 2003.

  1. Frank

    Frank Guest

    Hi,

    I've been reading a lot's of Q&A on user controls but none seem to
    answer my question. Here it is. I need to add dynamically a user
    control, in a htmltablecell, in an asp.net page (done in VB). This is
    the way, that I thought, was the wright way doing it:

    Dim oTable As New HtmlTable()
    Dim oRow As New HtmlTableRow()
    Dim oCell As New HtmlTableCell()
    Dim uc As Control = Page.LoadControl(ControlName)

    oTable = New HtmlTable()
    oRow = New HtmlTableRow()
    oCell = New HtmlTableCell()
    oCell.Controls.Add(uc)
    oRow.Controls.Add(oCell)
    oTable.Controls.Add(oRow)

    Return oTable

    The control I try to load contain two ASP:TextBox and one ASP:Button,
    this is a login user control to access a restricted section of the web
    site.

    The control loads fine in the asp.net page when I do it that way, but
    when I hit the button, the page do not trigger the event and do only a
    postback. Can someone tell me what's missing in the control page and
    also in the asp.net page ? Do I need also to add something to handle
    postback event for the data in the textbox ?

    Thanks

    Frank
     
    Frank, Nov 24, 2003
    #1
    1. Advertising

  2. hi frank, you need to add the control even after a postback, it will
    maintain state so you need not worry about losting the state it was in
    before the postback just coz you are adding the control again.

    Make sure you are loading your control and adding it to you table even after
    the page is posting back ;)

    "Frank" <> wrote in message
    news:...
    > Hi,
    >
    > I've been reading a lot's of Q&A on user controls but none seem to
    > answer my question. Here it is. I need to add dynamically a user
    > control, in a htmltablecell, in an asp.net page (done in VB). This is
    > the way, that I thought, was the wright way doing it:
    >
    > Dim oTable As New HtmlTable()
    > Dim oRow As New HtmlTableRow()
    > Dim oCell As New HtmlTableCell()
    > Dim uc As Control = Page.LoadControl(ControlName)
    >
    > oTable = New HtmlTable()
    > oRow = New HtmlTableRow()
    > oCell = New HtmlTableCell()
    > oCell.Controls.Add(uc)
    > oRow.Controls.Add(oCell)
    > oTable.Controls.Add(oRow)
    >
    > Return oTable
    >
    > The control I try to load contain two ASP:TextBox and one ASP:Button,
    > this is a login user control to access a restricted section of the web
    > site.
    >
    > The control loads fine in the asp.net page when I do it that way, but
    > when I hit the button, the page do not trigger the event and do only a
    > postback. Can someone tell me what's missing in the control page and
    > also in the asp.net page ? Do I need also to add something to handle
    > postback event for the data in the textbox ?
    >
    > Thanks
    >
    > Frank
     
    Alessandro Zifiglio, Nov 24, 2003
    #2
    1. Advertising

  3. Hi Frank,

    At which event are you adding the UC?

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Frank" <> wrote in message
    news:...
    > Hi,
    >
    > I've been reading a lot's of Q&A on user controls but none seem to
    > answer my question. Here it is. I need to add dynamically a user
    > control, in a htmltablecell, in an asp.net page (done in VB). This is
    > the way, that I thought, was the wright way doing it:
    >
    > Dim oTable As New HtmlTable()
    > Dim oRow As New HtmlTableRow()
    > Dim oCell As New HtmlTableCell()
    > Dim uc As Control = Page.LoadControl(ControlName)
    >
    > oTable = New HtmlTable()
    > oRow = New HtmlTableRow()
    > oCell = New HtmlTableCell()
    > oCell.Controls.Add(uc)
    > oRow.Controls.Add(oCell)
    > oTable.Controls.Add(oRow)
    >
    > Return oTable
    >
    > The control I try to load contain two ASP:TextBox and one ASP:Button,
    > this is a login user control to access a restricted section of the web
    > site.
    >
    > The control loads fine in the asp.net page when I do it that way, but
    > when I hit the button, the page do not trigger the event and do only a
    > postback. Can someone tell me what's missing in the control page and
    > also in the asp.net page ? Do I need also to add something to handle
    > postback event for the data in the textbox ?
    >
    > Thanks
    >
    > Frank
     
    Victor Garcia Aprea [MVP], Nov 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. osi_nl
    Replies:
    0
    Views:
    1,208
    osi_nl
    Aug 19, 2006
  2. RSH
    Replies:
    1
    Views:
    406
  3. pbd22
    Replies:
    1
    Views:
    1,011
    bruce barker
    Oct 24, 2007
  4. Travis Pruitt

    Dynamically Adding User Control with Child User Control

    Travis Pruitt, Jan 13, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    188
    Travis Pruitt
    Jan 13, 2005
  5. dotnet_nut

    asp.net 2.0 Wizard Control and User Control (.ascx)

    dotnet_nut, Oct 24, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    272
    dotnet_nut
    Oct 24, 2006
Loading...

Share This Page