Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Question - select/list box

Status
Not open for further replies.

Edge118

Programmer
Jun 15, 2004
104
0
0
US
I just started learning asp .net yesterday and I had a question regarding the following code:
Code:
<%@ Page Language="C#" %>

<html>
   <head>
      <link rel="stylesheet"href="intro.css">
   </head>

   <body><!-- #include File="C:\Inetpub\[URL unfurl="true"]wwwroot\DotNetJunkies\DotNetJunkiesWeb\_js\omni.inc"[/URL] -->

       <center>

       <form action="intro3.aspx">

           <h3> Name: <input name="Name" type=text value="<%=HttpUtility.HtmlEncode(Request.QueryString["Name"])%>">

           Category:  <select name="Category" size=1>

                         <%
                             String [] values = { "psychology", "business", "popular_comp" };

                             for (int i=0; i<values.Length; i++) {
                          %>

                                <option <% if (Request.QueryString["Category"] == values[i]) { Response.Write("selected"); } %>>
                                   <%=values[i]%>
                                </option>

                          <% } %>

                      </select>

           </h3>

           <input type=submit name="Lookup" value="Lookup">

           <p>

           <% if (Request.QueryString["Lookup"] != null) { %>

              Hi <%=HttpUtility.HtmlEncode(Request.QueryString["Name"]) %>, you selected: <%=HttpUtility.HtmlEncode(Request.QueryString["Category"]) %>

           <% } %>

       </form>

       </center>

   </body>
</html>

In the line:
Code:
<option <% if (Request.QueryString["Category"] == values[i]) { Response.Write("selected"); } %>>
I'm not sure how the select box is being populated through the Response.Write("selected")
How does "selected" represent the array of string values?

I hope I wasn't too confusing. Thanks for any help you can give me.


"Pin me. Perl me."

Regards,

Chris
 
I looked close and saw that the
Code:
<%=values[i]%>
is populating the list box, but what does the
Code:
{ Response.Write("selected"); }
do?

"Pin me. Perl me."

Regards,

Chris
 
Well, as it looks, it prints the string "selected" and according to its placement and the HTML spec, that would make that particular list item appear selected by default in the users browser (if the value of "Category" from the query string matches the current list item being added)

"Pin me. Perl me."
heh, .NET's just my day job, Perl's where the fun is. :)

________________________________________
Andrew
 
It's a C# IF statement.

if (if some condition is true)
{
then do some action here between the curly brackets

}

or else if it is not true then just skip the action in the curly brackets and resume to the next line of code which is here.
 
Also, that's actually a very poor example of ASP.NET as it uses 3 things that should be avoided (these have come from classic ASP):

1) It uses include files
2) It uses Response.Write
3) It uses "inline" if statements enclosed in script tags (<% and %>).

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top