Adding controls to a server control.

W

Wade

Hi all,

Question regarding server controls. I have built a server control that
creates two listboxes and a few buttons. Here's a snippet to show how I've
done this (from AddAttributesToRender sub):

' Available listbox
writer.AddAttribute(HtmlTextWriterAttribute.Class,
AvailableListbox_Class)
writer.AddAttribute(HtmlTextWriterAttribute.Size,
Convert.ToString(AvailableListbox_Size))
writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:" &
Convert.ToString(AvailableListbox_Width) & ";")
writer.RenderBeginTag(HtmlTextWriterTag.Select)
writer.RenderEndTag()

' Add all items button
writer.AddAttribute(HtmlTextWriterAttribute.Class,
AddAllItems_Class)
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.GetPostBackEventReference(Me, "AddAllItems"))
writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:" &
Convert.ToString(AddAllItems_Width) & ";")
writer.RenderBeginTag(HtmlTextWriterTag.Button)
writer.Write(AddAllItems_Text)
writer.RenderEndTag()

I have an issue -- I'd like to also pass in a connection string and stored
procedure name to the server control (this is not a problem) and build a
datatable (also not a table) to bind to the listbox (the problem). I don't
know how to appropriately bind my data to the listbox.

Is there anyway to build a dynamic control (i.e. listbox) and then add it to
a placeholder or something that gets rendered from my server control, rather
than using the "writer.RenderBeginTag(HtmlTextWriterTag.Select"? The only
way I can see myself doing this is populating the datatable, and then
creating a for next loop in the between the Begin/End tag of my listbox.

Any suggestions or ideas? Ideally I'd like to take my dynamic listbox,
which is already bound to my datatable, and have it just render! :)

Thanks,

Wade
 
W

Wade

Hi all,

I solved my problem. Here's a snippet of the solution:

Private btnAddItems As Button

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
Me.EnsureChildControls()
MyBase.Render(writer)
End Sub

Public Sub btnAddItems_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Page.Response.Write("test")

End Sub


Private Sub Assignment_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Init
btnAddItems = New Button
btnAddItems.ID = "btnAddItems"
btnAddItems.Text = "Add"
btnAddItems.CausesValidation = False
AddHandler btnAddItems.Click, AddressOf btnAddItems_Click

Dim tbl As New Table
Dim tr As New TableRow
Dim tc As New TableCell

tc.Controls.Add(btnAddItems)
tr.Cells.Add(tc)
tbl.Rows.Add(tr)

Me.Controls.Add(tbl)
End Sub

Thanks!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top