views:

24

answers:

3

This seems really simple, but for some reason Im stumped..

Im dynamically generating an HTML Select Box, lets call it myselect. Im creating this select box based on some database values Im generating an HTML Select Box.. almost like a string that Im just spitting out to the page. So it's never a control in the codebehind, just part of a string thats rendered as HTML by the browser. If I submit my form, and in my codebehind I perform:

Dim myVal as String = Request.Form("myselect")

That code will give me the VALUE of the myselect select box. How can I refer to this control to cast it as a System.Web.UI.HtmlControls.HtmlSelect control? Request.Form seems to give me the value, but I want to reference the object itself..

A: 

If your dynamically generating the control in your code file, then it will not be available to you on post back. As long as you generate it again before the viewstate is processed (you can do it in your oninit), then you can access it as you would anything else.

MySelect.SelectedValue
Gabriel McAdams
Thanks Gabriel, let me clarify, based on some database values Im generating an HTML Select Box.. almost like a string that Im just spitting out to the page. So it's never a control in the codebehind, just part of a string thats rendered as HTML by the browser. I'll edit my question as well so maybe itll make more sense..
ewitkows
In that case, there is nothing you can do. Why is it that you need the control? Perhaps there is a better way of adding it to your page that will give you what you need.
Gabriel McAdams
A: 

If this select box isn't declared statically on your page (that is, you're adding it to the Control Collection), you'll have to do something like this: Dim MySelect as HtmlSelect = Page.FindControl("MySelect") as HtmlSelect

You'll have to forgive me if my casting syntax is a bit off -- I'm used to casting in C#.

jwiscarson
Nope, thanks though. The control isnt in the collection, its not a server control, just an HTML select box that's been rendered on the page...
ewitkows
You can't reference a control that's only rendered on the page. You have to specify the `runat="server"` property to reference controls in codebehind.
jwiscarson
I was concerned that was the case :(
ewitkows
What are you trying to accomplish? If this select box is created based on database values, I don't see any special reason why it can't be a server control.
jwiscarson
A: 

In response to the comments above (thanks for your help), I found what Gabriel McAdams and jwiscarson had to say were true. In browsing the Request object, I found that its nothing more than a collection of key\value pairs. Performing Request.Form("myformobj") will return a value, because thats all thats available to the application. If necessary, I suppose I can whip up some nifty javascript to track form object types, but it's certainly not necessary in my case.

Thanks for the help

ewitkows