RenderControl and DropDownList and "must be placed inside a form tag"
Hi,
I'm getting the following error message when trying to run my aspx page....
Control 'drddropDown1' of type 'DropDownList' must be placed inside a form tag with runat=server
Basically, I'm trying to reponse.write a control on to the page.
It happens on the drddropDown1.RenderControl(myHtmlTextWriter)
code.
I dropped a dropdownlist control into a webpage(aspx), and then I call the function below with the control as an argument. Here is the code...
a Code:
Public Function BuildAndDisplay(ByRef drddropDown1 As DropDownList)
'I do stuff here, where I add items to the dropdownlist control
Dim myTextWriter As New System.IO.StringWriter
Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)
drddropDown1.RenderControl(myHtmlTextWriter)
HttpContext.Current.Response.Write(myTextWriter.ToString)
End Function
I'm baffled. I'm not sure what I'm doing wrong. :-(
My other concern, is if I get this to work, while I be able to get a postback on an event.
Any help is greatly appreciated. Let me know if you need more info from me.
Re: RenderControl and DropDownList and "must be placed inside a form tag"
I'm getting closer....
Code:
function ForcePostBack(control)
{
__doPostBack(control,'SelectedIndexChange',this.value);
}
vbcode Code:
drddropDown1.Attributes.Add("OnChange", "ForcePostBack('drddropDown1','')")
vbcode2 Code:
Private Sub drddropDown1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles drddropDown1.SelectedIndexChanged
Response.Write("<script>alert('test');</script>")
End Sub
The page post back, when you change an item in the drop down list, but the only problem now is that it's not executing the code in the drddropDown1_SelectedIndexChanged Sub. It's posting back, but maybe it does not know what event to process.
Help me if you can. :-(
Re: RenderControl and DropDownList and "must be placed inside a form tag"
You shouldn't be using response to generate asp.net controls. Let the framework do it for you with the controls collection of the page.
http://support.microsoft.com/kb/317515
if you need them in a certain spot, consider using a placeholder.
Bill