Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 13th, 2005, 01:07 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default How 2 Dynamically Replace Txt

In VB6 IIS applications (hereinafter “IIS,” as distinguished from “.NET”), the contents of what arrived at the browser could be changed just about anywhere on the page through the mechanism of replaceable tags.

Using that capability I have several areas in my application in IIS (which I am now trying to move into .NET) where I fill in the contents of JScript variables, and <select> lists (for example) at the time that the page is requested.

For instance, I send a string of existing initials to a variable, for later comparison in validation if needed:
Code:
    var Inits = <WC_Inits></WC_Inits>;
which might become something like
Code:
    var Inits = "±JU±KR±KU±LD±LM±NT±";
    (the two tags beginning with “WC_”—and all that might be between them—is replaced from the database when the page is processed.

In another case, I have:

    <select id="Nouns">
        <WC_Nouns_Options></WC_Nouns_Options>
    </select>

I fill in the options from the database at the time the page is processed. The nature of the list of options is that there is a machine-utilized code (the “value”) and a plain text string that the user sees. EG:
Code:
    <option value="ACT">ACTUATOR       </option>
    <option value="ADA">ADAPTER        </option>
    <option value="ADH">ADHESIVE       </option>
    <option value="AIC">AIR CONDITIONER</option>
    <option value="AIF">AIR FILTER     </option>
    <option value="ALA">ALARM          </option>
    I know that I can add a Web Control Drop-down List, but the items all must have the value attribute equal the presentation text, which will not work in my case.

Using .NET, how can I fill in content that is sent to the browser with dynamic content?

Is there a way to put a placeholder / bookmark / token into the text of the .aspx file which can be replaced at runtime (or, more accurately, at “Request-time”)?
  #2 (permalink)  
Old May 13th, 2005, 02:38 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The drop down list web control is not restricted in the way you think. You can specify which field you want bound to the visible text and option value independently:

myDDL.DataTextField = "DescriptiveNameField"
myDDL.DataValueField = "MachineCodeField"


As far as the javascript goes...

Initially I used asp:literals where I needed to write dynamic information to areas of javascript:

<script language="javascript">
alert("<asp:literal runat="server" id="litJS" />");
</script>

However, this is hard to read and gets clunky once you have lots in there and lots of corresponding literals in the code-behind. So now I wrap my javascript inside of a single asp:literal control and place replacement tokens in the javascript where I need to write the dynamic information. Something along these lines:

<asp:literal runat="server" id="litJS" enableviewstate="false">
    <script language="javascript">
    alert("%%MYDYNAMICTEXT%%");
    </script>
</asp:literal>

Then in the code-behind, just do a simple replace on the literal control text:

litJS.Text = litJS.Text.Replace("%%MYDYNAMICTEXT%%", "Hello World")

-Peter
  #3 (permalink)  
Old May 13th, 2005, 03:05 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Are there properties for independently setting the text and value of a web control which is not bound?
  #4 (permalink)  
Old May 13th, 2005, 04:17 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you manually create list items, their constructor has arguments for both value and text:

http://msdn.microsoft.com/library/en...ctorTopic3.asp

-Peter
  #5 (permalink)  
Old May 16th, 2005, 02:37 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

It absolutely floors me that they didn't include a Boolean argument for 'Selected!'

Is there a way—in code—to set one of the options in a select element to be the selected item?

Of course setting an item as selected can be done in the web page, but I don't see a way to do this in code:
Code:
<asp:DropDownList id="D1" runat="server">
    <asp:ListItem  [u]Selected=True</u> Value="Val 1"></asp:ListItem>
    <asp:ListItem                  Value="Val 2"></asp:ListItem>
</asp:DropDownList[blue]>
I tried using an HtmlSelect control, and putting a literal between the <select> and </select>, but that generates an error, because an HtmlSelect item cannot have an asp:literal child.

But if I create the whole <select> list within a literal, I suspect I will not be able to use the _ServerChange() event to test whether the user has made a selection on that element.

Sheesh! What I want to do is so cotton-pickin’ easy, but every board I come up with to span this chasm is just too short to do the job...

I need to fill one list (Nouns) from the database.
When a noun is selected, I need to modify a second list of Noun-Name Modifiers, and repaint the page in a fashion that keeps the selection made on the Nouns DDL.

So if they pick a noun like "A/C Plant," the modifier list gets 2 options: Nothing or "363 TON, R-114."
If they pick "Air Conditioner," the modifier list gets 2 options: Nothing or "SELF-CONTAINED."
If they pick "Alarm," the modifier list gets 3 options: Nothing, "BELL" or "WATER SWITCH."

The modifier list has to change to reflect the currently-selected noun.

I have a function, Nouns() which returns all the nouns, just as a string of options, their values, and their text, in straight HTML. I can easily adapt this to accept a value, and set the option that matches that argument to selected. But how to then get the noun selection list to have that set of options for its inner html (while still retaining the ability to use the ServerChange() event?

Ideas?
Comments?
Alternate approaches?

(I have this working perfectly in VB6/IIS. Seems upgrading ought not to remove functionality...)
  #6 (permalink)  
Old May 16th, 2005, 03:47 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Brian,

It seems that you are overcomplicating things a bit.

Regardless of how you populate a list of options in a select control (listbox, dropdownlist, etc) .NET will retain the list and state of the list for you. You just need to populate it the right way. This mean that you need to populate it only when it changes. This usually occurs under only two conditions:

1. The page is fresh (Not IsPostBack) and has no data in it.
2. You have done something that should affect the list, and therefore refresh it.

In your example, your list of nouns is static and should only be populated when you hit the page for the first time (this is condition 1):

If Not IsPostBack Then
   Nouns() 'Populate my list of nouns
End If

Then, your list of modifiers gets changed when you change the selected item of the noun list (condition 2). This can be done in the noun selector onchange event.

I think one of the problems is that you are trying to mix too much ASP style code technique into ASP.NET. The ASP.NET server controls take away much of the burden we had as ASP developers (maintaining control state by specifying which item is selected for example). If you bind/populate your data the right way, you don't need to do any of this control overhead.

-Peter
  #7 (permalink)  
Old May 16th, 2005, 04:17 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I believe I am starting to see the light on how to do this, thanks to your guidance.

So rather than loading a string with "<option value=SomeVal>SomeText</option>" groupings, then somehow affixing that to the HTML of the <select>, I would instead directly fill the <asp:DropDownList . . .> when first processing the page, and then leave its contents be after that. (For each row in the records, create a new ListItem, change its value and text, than then .Items.Add(<NewItem>).) Whatever is selected will stay selected, yes?

Then when the selected item changes, clear the Modifiers list, and refill it (through the same method) with items pertaining to the noun that has just been selected. Is that more the idea?

I got stuck on the concept of filling in the contents with an ASP Literal, and reasoned that everytime the page was processed, that Literal would need to be handled (which, of course, it would). In re-processing that literal, I just could not arrive at how to maintain the selection, you see.
  #8 (permalink)  
Old May 16th, 2005, 04:40 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Presisely! You are catching on now. You don't even need to do the manual .Add() call. Particularly if you are going to be populating the list with results from a database, you can just databind to it. If you need to preselect an item there are methods available on the ListItemCollection class (ddl.Items) to find an item so you can mark it as selected. As long as you don't re-populate the list of items .NET will keep the selected item selected. In fact, I believe you can modify the list and the selected item will remain (assuming that you don't remove the item that is selected. You can add items to the list without harm.

-Peter
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
export in txt format mallick_kuet BOOK: Professional Crystal Reports for VS.NET 0 January 20th, 2007 05:22 AM
Reading a txt file AlphaX Beginning VB 6 2 February 20th, 2006 11:22 PM
DATE within txt box alannoble Excel VBA 1 December 22nd, 2004 12:36 PM
Dynamically replace nested frames? fs22 Javascript How-To 4 June 23rd, 2004 10:57 AM



All times are GMT -4. The time now is 11:37 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.