Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Can you add HTML controls to the VIEWSTATE?

Author  Topic 

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-08 : 11:34:05
I have a webform with an ASP.Net Dropdownlist that posts back to update another ASP.Net Dropdownlist with new values.

However, during the postback while all the ASP.Net controls retain their value through the VIEWSTATE my HTML controls do not. I need to have these controls as HTML controls and not reset upon the quick postback for more information.

Is there anyway to easily add these values to the view state?

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-08 : 12:07:21
Do you add a runat="server" attribute for the HTML controls? That should be all you need.

I would think, though, it would be much easier to use all asp.net server controls ...
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-08 : 12:50:08
runat="server" isn't working for me

this is what the resulting HTML code looks like:

<select name="doc6" id="doc6" tabindex="18" style="font-size:8pt;width:142px" runat="server">
<option value="1">Verified</option>
<option selected="selected" value="2">Not Received</option>
<option value="3">Received, Not Verified</option>
<option value="4">Received, Incomplete</option><option value="5">Not Applicable</option>
</select>


I can't use ASP.Net controls because these HTML controls are generated dynamically and while I'm sure it is possible to make them ASP.Net it would be much more complex than it is worth
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-08 : 13:05:57
You can't do it that way in ASP.Net; you cannot generate HTML using one method and expect the server to process that HTML as part of the page's compiled class. The runat="server" must be present in the source code for the control to get "hooked" up to the ASP.NET page's class; otherwise, it is just text rendered and sent to the browser (like the old ASP model).

If you need to fill something dynamically, you create an asp:DropDown control and fill up the contents of that control using standard ASP.NET code. It's very easy to do, especially if those values come from a database. If you are using portions of ASP.NET for some things but not for others, then you are making quite a big mess.

If you don't want to properly use ASP.NET, then you won't be able to take advantage of post backs and viewstates and the other features it offers. If you do want to use it properly, and you need help dynamically filling up this drop-down list, let me know and I can help you out.
Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-08 : 13:13:34
jsmith8858:

I'm not filling them dynamically I'm creating them dynamically. I run a query and the number of rows in the query is the number of dropdownlists I need.

I don't know of any other way to generate all of my dropdownlist other than HTML controls
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-02-08 : 14:10:56
Same thing. You can use a repeater to create multipe controls, or you can do it in code.

EDIT: I just saw that you said the number of drop-down lists you need is coming from a table.

Is that correct? If the table returns 10 rows, you need 10 drop-down *lists*, or 10 *items* in one list? If you need 10 lists, what values do you put in the drop-down list? Where do those come from?

If you need to create multiple controls, one per row in a recordset, you can use a <asp:Repeater> control to do this.

A more manual way to do it is to do it all with code. You can create an asp:Panel tag where you want the drop-downs to go, then in your code you just keep creating new controls and adding them to the panel's controls property.

For example:

on a blank web form, just put this tag on the page somewhere:

<asp:Panel runat="server" id="spot"/>

Then, on you Page_Load() event, you can put code like this:


Dim ddl As DropDownList

ddl = New DropDownList
ddl.Items.Add(New ListItem("Item 1 text", "Item 1 value"))
ddl.Items.Add(New ListItem("Item 2 text", "Item 2 Value"))

spot.Controls.Add(ddl)

ddl = New DropDownList
ddl.Items.Add(New ListItem("list 2, item 1", "1"))
ddl.Items.Add(New ListItem("list 2, item 2", "2"))

spot.Controls.Add(ddl)


that puts two new drop downs, created dynamically, on the page. You can use this basic idea and loop through the rows in a data table or whatever you need.

Go to Top of Page

Billkamm
Posting Yak Master

124 Posts

Posted - 2006-02-09 : 09:39:31
jsmith8858: For every row in the table which a dropdownlist is created for another query is ran to get the values for that dropdownlist.

The code is generating a list of tasks to do from a table of possible tasks, but each tasks has different statuses in the status dropdownlist. So instead of just Complete/Incomplete for all of them each task has its own list of values for the status dropdownlist.

My first attempt was to just play <% GenerateTasksHtml() %> at the place in the HTML design where I want it to appear. It then loops through and writes out all of the TD and TR tags and places things like document names, date the status dropdown was last changed, etc... along with the status dropdownlist itself.

I will look into the repeater idea and see if I can get this to solve my problem. On another note after I generate all these dropdownlists I pull in the values using request.form and write a giant INSERT INTO statement from the values.
Go to Top of Page
   

- Advertisement -