em3ricasforsale
em3ricasforsale

Reputation: 352

Checkboxes in ASP.NET

I am trying to use a checkbox that is dynamically declared in an .vb file that I am trying to write into my .aspx page. I am able to write a normal checkbox of

<input type='checkbox />

from the .vb Class using Response.write, but it comes up blank when using

<asp:Checkbox runat='server' />

I need to pass whether or not the box is checked back to the server, because I am having to either approve something if one is checked, reject something if the other is checked or do nothing if neither are checked. I have figured out how to make them mutually exclusive either way so that is not the problem. Does anyone have any recommendations?

Upvotes: 2

Views: 1375

Answers (3)

Michael Jasper
Michael Jasper

Reputation: 8058

Your problem lies in the order that the pages are compiled in: When you place an asp control like the asp:checkbox, it is compiled into a regular checkbox with some javascript attached when it is sent over to the client.

When you write the string "<input type='checkbox />" to the page from the code-behind it is writing that string directly to the page, after the aspx page has compiled its controls, but since that is valid html the browser renders the control. When you write the asp:checkbox, the browser doesn't know what to do with it, because it is not valid html. In addition, the page has already been compiled, so there is no chance of .net creating the correct control for you.

You need to programmatic add the control to the webpage by creating a new control through the code behind

This site does a great job explaining it

And @toodles seems spot on. Writing static html and asp.net are two totally different ball games. I would spend a bit of time (like hours/days) reading/watching learning material to help you get on your feet.

Upvotes: 1

toddles2000
toddles2000

Reputation: 1132

The technical answers you are getting are all good. However, your question indicates that you really need to start by learning how asp.net server controls work. I suggest spending a couple hours watching the videos at http://asp.net and particularly http://www.asp.net/general/videos/intro-to-aspnet-controls

Then focus on understanding the page lifecycle and you'll have enough of the basics to be much more effective at asp.net. Have fun!

Upvotes: 1

Michael
Michael

Reputation: 1879

You can't use response.write to create server controls. See this site for an example of the right way to do it: http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

Upvotes: 0

Related Questions