Hosting controls in a custom server control

J

jbeckh2

Hi,

I am trying to create a custom server control that contains several
dropdownlists. The control looks like this:

Dim pnlHeader As New Panel

Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter)
pnlHeader.RenderControl(writer)
End Sub

Public Sub Setup_Control()
Dim table As New HtmlControls.HtmlTable
table.Attributes.Add("style", "width:100%")
pnlHeader.Controls.Add(table)

Dim row As New HtmlControls.HtmlTableRow

Dim cellLabel As New HtmlControls.HtmlTableCell
cellLabel.Width = "60%"
Dim lblProblems As New Label
lblProblems.ID = "lbl" & Category.ProblemTypeId
lblProblems.Text = "Choose Vendor for All " &
Category.ProblemType & " Problems."
cellLabel.Controls.Add(lblProblems)

Dim cellDDL As New HtmlControls.HtmlTableCell
cellDDL.Width = "30%"
Dim ddlAll As New DropDownList
ddlAll.ID = "ddlVendorForAll" &
Category.ProblemTypeId.ToString()
ddlAll.AutoPostBack = True
Common.FillList(ddlAll, HeatMapData.GetVendors(PropertyId,
"[Choose a Vendor]"))
cellDDL.Controls.Add(ddlAll)
listDDL.Add(ddlAll)
AddHandler ddlAll.SelectedIndexChanged, AddressOf
ddlAll_SelectedIndexChanged

Dim cellImage As New HtmlControls.HtmlTableCell
cellImage.Width = "9%"
Dim imgExpand As New WebControls.Image
imgExpand.ID = "imgExpand" &
Category.ProblemTypeId.ToString()
imgExpand.ImageUrl = "~/images/icons/118.png"
imgExpand.ToolTip = "View Details"


Dim imgCollapse As New WebControls.Image
imgCollapse.ID = "imgCollapse" &
Category.ProblemTypeId.ToString()
imgCollapse.ImageUrl = "~/images/icons/116.png"
imgCollapse.ToolTip = "Hide Details"
imgCollapse.Attributes.Add("style", "display:none")

cellImage.Controls.Add(imgExpand)
cellImage.Controls.Add(imgCollapse)

row.Controls.Add(cellLabel)
row.Controls.Add(cellDDL)
row.Controls.Add(cellImage)
table.Rows.Add(row)

pnlHeader.BackColor = Drawing.Color.FromArgb(255, 206,
204, 204)
End Sub

Protected Sub ddlAll_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
....
End Sub


The control renders fine. The problem I am having is that the
dynamically created dropdownlist (ddlAll) does not fire the
SelectedIndexChanged event. What am I missing?

Thanks,
Jeremy
 
B

bruce barker

when do you call Setup_Control() (and why do you have it?). it must be called
in oninit of the page, or better yet, change it to CreateChildControls.


-- bruce (sqlwork.com)
 
J

jbeckh2

The controls are added to a panel that is displayed after a button is
clicked on page. I set properties on the control and then call
Setup_Control creates all of the child controls on the button click.

when do you call Setup_Control() (and why do you have it?). it must be called
in oninit of the page, or better yet, change it to CreateChildControls.

-- bruce (sqlwork.com)

I am trying to create a custom server control that contains several
dropdownlists. The control looks like this:
Dim pnlHeader As New Panel
Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter)
pnlHeader.RenderControl(writer)
End Sub
Public Sub Setup_Control()
Dim table As New HtmlControls.HtmlTable
table.Attributes.Add("style", "width:100%")
pnlHeader.Controls.Add(table)
Dim row As New HtmlControls.HtmlTableRow
Dim cellLabel As New HtmlControls.HtmlTableCell
cellLabel.Width = "60%"
Dim lblProblems As New Label
lblProblems.ID = "lbl" & Category.ProblemTypeId
lblProblems.Text = "Choose Vendor for All " &
Category.ProblemType & " Problems."
cellLabel.Controls.Add(lblProblems)
Dim cellDDL As New HtmlControls.HtmlTableCell
cellDDL.Width = "30%"
Dim ddlAll As New DropDownList
ddlAll.ID = "ddlVendorForAll" &
Category.ProblemTypeId.ToString()
ddlAll.AutoPostBack = True
Common.FillList(ddlAll, HeatMapData.GetVendors(PropertyId,
"[Choose a Vendor]"))
cellDDL.Controls.Add(ddlAll)
listDDL.Add(ddlAll)
AddHandler ddlAll.SelectedIndexChanged, AddressOf
ddlAll_SelectedIndexChanged
Dim cellImage As New HtmlControls.HtmlTableCell
cellImage.Width = "9%"
Dim imgExpand As New WebControls.Image
imgExpand.ID = "imgExpand" &
Category.ProblemTypeId.ToString()
imgExpand.ImageUrl = "~/images/icons/118.png"
imgExpand.ToolTip = "View Details"
Dim imgCollapse As New WebControls.Image
imgCollapse.ID = "imgCollapse" &
Category.ProblemTypeId.ToString()
imgCollapse.ImageUrl = "~/images/icons/116.png"
imgCollapse.ToolTip = "Hide Details"
imgCollapse.Attributes.Add("style", "display:none")


pnlHeader.BackColor = Drawing.Color.FromArgb(255, 206,
204, 204)
End Sub
Protected Sub ddlAll_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
....
End Sub
The control renders fine. The problem I am having is that the
dynamically created dropdownlist (ddlAll) does not fire the
SelectedIndexChanged event. What am I missing?
Thanks,
Jeremy
 
B

bruce barker

but then on postback you need to recreate the control (and its children) in
the oninit event so it can receive the postback data and fire the onchange
event. you can store in viewstate a flag to determine if this must be done.

-- bruce (sqlwork.com)


The controls are added to a panel that is displayed after a button is
clicked on page. I set properties on the control and then call
Setup_Control creates all of the child controls on the button click.

when do you call Setup_Control() (and why do you have it?). it must be called
in oninit of the page, or better yet, change it to CreateChildControls.

-- bruce (sqlwork.com)

I am trying to create a custom server control that contains several
dropdownlists. The control looks like this:
Dim pnlHeader As New Panel
Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter)
pnlHeader.RenderControl(writer)
End Sub
Public Sub Setup_Control()
Dim table As New HtmlControls.HtmlTable
table.Attributes.Add("style", "width:100%")
pnlHeader.Controls.Add(table)
Dim row As New HtmlControls.HtmlTableRow
Dim cellLabel As New HtmlControls.HtmlTableCell
cellLabel.Width = "60%"
Dim lblProblems As New Label
lblProblems.ID = "lbl" & Category.ProblemTypeId
lblProblems.Text = "Choose Vendor for All " &
Category.ProblemType & " Problems."
cellLabel.Controls.Add(lblProblems)
Dim cellDDL As New HtmlControls.HtmlTableCell
cellDDL.Width = "30%"
Dim ddlAll As New DropDownList
ddlAll.ID = "ddlVendorForAll" &
Category.ProblemTypeId.ToString()
ddlAll.AutoPostBack = True
Common.FillList(ddlAll, HeatMapData.GetVendors(PropertyId,
"[Choose a Vendor]"))
cellDDL.Controls.Add(ddlAll)
listDDL.Add(ddlAll)
AddHandler ddlAll.SelectedIndexChanged, AddressOf
ddlAll_SelectedIndexChanged
Dim cellImage As New HtmlControls.HtmlTableCell
cellImage.Width = "9%"
Dim imgExpand As New WebControls.Image
imgExpand.ID = "imgExpand" &
Category.ProblemTypeId.ToString()
imgExpand.ImageUrl = "~/images/icons/118.png"
imgExpand.ToolTip = "View Details"
Dim imgCollapse As New WebControls.Image
imgCollapse.ID = "imgCollapse" &
Category.ProblemTypeId.ToString()
imgCollapse.ImageUrl = "~/images/icons/116.png"
imgCollapse.ToolTip = "Hide Details"
imgCollapse.Attributes.Add("style", "display:none")


pnlHeader.BackColor = Drawing.Color.FromArgb(255, 206,
204, 204)
End Sub
Protected Sub ddlAll_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
....
End Sub
The control renders fine. The problem I am having is that the
dynamically created dropdownlist (ddlAll) does not fire the
SelectedIndexChanged event. What am I missing?
Thanks,
Jeremy
 
J

jbeckh2

Do I need to rebind the dropdownlist?

but then on postback you need to recreate the control (and its children) in
the oninit event so it can receive the postback data and fire the onchange
event. you can store in viewstate a flag to determine if this must be done.

-- bruce (sqlwork.com)

The controls are added to a panel that is displayed after a button is
clicked on page. I set properties on the control and then call
Setup_Control creates all of the child controls on the button click.
when do you call Setup_Control() (and why do you have it?). it must be called
in oninit of the page, or better yet, change it to CreateChildControls.
-- bruce (sqlwork.com)
:
Hi,
I am trying to create a custom server control that contains several
dropdownlists. The control looks like this:
Dim pnlHeader As New Panel
Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter)
pnlHeader.RenderControl(writer)
End Sub
Public Sub Setup_Control()
Dim table As New HtmlControls.HtmlTable
table.Attributes.Add("style", "width:100%")
pnlHeader.Controls.Add(table)
Dim row As New HtmlControls.HtmlTableRow
Dim cellLabel As New HtmlControls.HtmlTableCell
cellLabel.Width = "60%"
Dim lblProblems As New Label
lblProblems.ID = "lbl" & Category.ProblemTypeId
lblProblems.Text = "Choose Vendor for All " &
Category.ProblemType & " Problems."
cellLabel.Controls.Add(lblProblems)
Dim cellDDL As New HtmlControls.HtmlTableCell
cellDDL.Width = "30%"
Dim ddlAll As New DropDownList
ddlAll.ID = "ddlVendorForAll" &
Category.ProblemTypeId.ToString()
ddlAll.AutoPostBack = True
Common.FillList(ddlAll, HeatMapData.GetVendors(PropertyId,
"[Choose a Vendor]"))
cellDDL.Controls.Add(ddlAll)
listDDL.Add(ddlAll)
AddHandler ddlAll.SelectedIndexChanged, AddressOf
ddlAll_SelectedIndexChanged
Dim cellImage As New HtmlControls.HtmlTableCell
cellImage.Width = "9%"
Dim imgExpand As New WebControls.Image
imgExpand.ID = "imgExpand" &
Category.ProblemTypeId.ToString()
imgExpand.ImageUrl = "~/images/icons/118.png"
imgExpand.ToolTip = "View Details"
Dim imgCollapse As New WebControls.Image
imgCollapse.ID = "imgCollapse" &
Category.ProblemTypeId.ToString()
imgCollapse.ImageUrl = "~/images/icons/116.png"
imgCollapse.ToolTip = "Hide Details"
imgCollapse.Attributes.Add("style", "display:none")
cellImage.Controls.Add(imgExpand)
cellImage.Controls.Add(imgCollapse)
row.Controls.Add(cellLabel)
row.Controls.Add(cellDDL)
row.Controls.Add(cellImage)
table.Rows.Add(row)
pnlHeader.BackColor = Drawing.Color.FromArgb(255, 206,
204, 204)
End Sub
Protected Sub ddlAll_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
....
End Sub
The control renders fine. The problem I am having is that the
dynamically created dropdownlist (ddlAll) does not fire the
SelectedIndexChanged event. What am I missing?
Thanks,
Jeremy
 
J

jbeckh2

Nevermind, I worked around it. I'm just inspecting the internal
controls through a method call and just worked around the event
problem.

Do I need to rebind the dropdownlist?

but then on postback you need to recreate the control (and its children) in
the oninit event so it can receive the postback data and fire the onchange
event. you can store in viewstate a flag to determine if this must be done.
-- bruce (sqlwork.com)
The controls are added to a panel that is displayed after a button is
clicked on page. I set properties on the control and then call
Setup_Control creates all of the child controls on the button click.
On Mar 17, 10:45 am, bruce barker
when do you call Setup_Control() (and why do you have it?). it must be called
in oninit of the page, or better yet, change it to CreateChildControls.
-- bruce (sqlwork.com)
:
Hi,
I am trying to create a custom server control that contains several
dropdownlists. The control looks like this:
Dim pnlHeader As New Panel
Protected Overrides Sub RenderContents(ByVal writer as HtmlTextWriter)
pnlHeader.RenderControl(writer)
End Sub
Public Sub Setup_Control()
Dim table As New HtmlControls.HtmlTable
table.Attributes.Add("style", "width:100%")
pnlHeader.Controls.Add(table)
Dim row As New HtmlControls.HtmlTableRow
Dim cellLabel As New HtmlControls.HtmlTableCell
cellLabel.Width = "60%"
Dim lblProblems As New Label
lblProblems.ID = "lbl" & Category.ProblemTypeId
lblProblems.Text = "Choose Vendor for All " &
Category.ProblemType & " Problems."
cellLabel.Controls.Add(lblProblems)
Dim cellDDL As New HtmlControls.HtmlTableCell
cellDDL.Width = "30%"
Dim ddlAll As New DropDownList
ddlAll.ID = "ddlVendorForAll" &
Category.ProblemTypeId.ToString()
ddlAll.AutoPostBack = True
Common.FillList(ddlAll, HeatMapData.GetVendors(PropertyId,
"[Choose a Vendor]"))
cellDDL.Controls.Add(ddlAll)
listDDL.Add(ddlAll)
AddHandler ddlAll.SelectedIndexChanged, AddressOf
ddlAll_SelectedIndexChanged
Dim cellImage As New HtmlControls.HtmlTableCell
cellImage.Width = "9%"
Dim imgExpand As New WebControls.Image
imgExpand.ID = "imgExpand" &
Category.ProblemTypeId.ToString()
imgExpand.ImageUrl = "~/images/icons/118.png"
imgExpand.ToolTip = "View Details"
Dim imgCollapse As New WebControls.Image
imgCollapse.ID = "imgCollapse" &
Category.ProblemTypeId.ToString()
imgCollapse.ImageUrl = "~/images/icons/116.png"
imgCollapse.ToolTip = "Hide Details"
imgCollapse.Attributes.Add("style", "display:none")
cellImage.Controls.Add(imgExpand)
cellImage.Controls.Add(imgCollapse)
row.Controls.Add(cellLabel)
row.Controls.Add(cellDDL)
row.Controls.Add(cellImage)
table.Rows.Add(row)
pnlHeader.BackColor = Drawing.Color.FromArgb(255, 206,
204, 204)
End Sub
Protected Sub ddlAll_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)
....
End Sub
The control renders fine. The problem I am having is that the
dynamically created dropdownlist (ddlAll) does not fire the
SelectedIndexChanged event. What am I missing?
Thanks,
Jeremy
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top