Page Code :
Copy code The Code is as follows: <% @ page Language = "C #" autoeventwireup = "true" codefile = "radiobuttonlistdemo. aspx. cs"
Inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: radiobuttonlist id = "radiobuttonlist_demo" runat = "server" onselectedindexchanged = "radiobuttonlist_demo_selectedindexchanged"
Autopostback = "true">
</ASP: radiobuttonlist>
<Br/>
<Asp: Image id = "image_show" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
Background code: Copy code The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using cdatabase;
Using system. IO;
Public partial class _ default: system. Web. UI. Page
{
/// <Summary>
/// Page loading event
/// </Summary>
/// <Param name = "sender"> Control sending object </param>
/// <Param name = "E"> event object </param>
Protected void page_load (Object sender, eventargs E)
{
// Obtain the connectionstring Value
// Response. Write ("<SCRIPT> alert ('" + sqlhelper. constring + "') </SCRIPT> ");
If (! Ispostback)
{
// First, you must have a path to the file path in the Fuwa folder under the root directory of the system.
String Spath = server. mappath (request. applicationpath + "/Fuwa /");
// Obtain all the file names under this path including the path
String [] sfiles = directory. getfiles (Spath );
// Loop the paths of all objects
Foreach (string sfile in sfiles)
{
// Get the file name
String sname = path. getfilenamewithoutextension (sfile );
// Get the file name, including the extension
String sfilename = path. getfilename (sfile );
// Create the subitem of radiobuttonlist, using text/value overload
Listitem ritem = new listitem (sname, request. applicationpath + "/Fuwa/" + sfilename );
// Add the subitem to radiobuttonlist
Radiobuttonlist_demo.items.add (ritem );
}
// Set the display arrangement of the radio buttons in RBL
Radiobuttonlist_demo.repeatdirection = repeatdirection. horizontal;
Radiobuttonlist_demo.repeatlayout = repeatlayout. Table;
}
}
/// <Summary>
/// Select a change event
/// </Summary>
/// <Param name = "sender"> Control sending object </param>
/// <Param name = "E"> event object </param>
Protected void radiobuttonlist_demo_selectedindexchanged (Object sender, eventargs E)
{
Image_show.imageurl = radiobuttonlist_demo.selectedvalue.tostring ();
}
}
Key Points
Obtain the path of a directory under the website directory
Server. mappath (argurment)
Parameter usage
Request. Appliaction + "/directory name /"
This sentence means
Request the path under a directory on the server
All file names in the path after the path is complete
Use the directory object in system. Io
Getfiles (request. Appliaction) method
Only all file names in the directory can contain the extension
The path still needs to be obtained using request. Application + "/file /".
The comment has been clearly written.
You can practice it.