欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET 服务器路径和一般资源调用

程序员文章站 2024-03-08 22:45:10
页面代码: 复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="radiobutto...
页面代码:
复制代码 代码如下:

<%@ 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>无标题页</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>

后台代码:
复制代码 代码如下:

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>
/// 页面加载事件
/// </summary>
/// <param name="sender">控件发送对象</param>
/// <param name="e">事件对象</param>
protected void page_load(object sender, eventargs e)
{
//取得connectionstring的值
//response.write("<script>alert('" + sqlhelper.constring + "')</script>");
if (!ispostback)
{
//先要有路径 系统根目录下 福娃文件夹 下的文件路径
string spath = server.mappath(request.applicationpath + "/福娃/");
//取得这个路径下面所有的文件名 包含其路径
string[] sfiles = directory.getfiles(spath);
//循环所有文件的路径
foreach (string sfile in sfiles)
{
//取文件名
string sname = path.getfilenamewithoutextension(sfile);
//取文件名, 包含扩展名
string sfilename = path.getfilename(sfile);
//建立radiobuttonlist的子项,采用 text/value 的重载方式
listitem ritem = new listitem(sname, request.applicationpath + "/福娃/" + sfilename);
//将子项添加到radiobuttonlist里
radiobuttonlist_demo.items.add(ritem);
}
//设置rbl中单选按钮的显示排列方式
radiobuttonlist_demo.repeatdirection = repeatdirection.horizontal;
radiobuttonlist_demo.repeatlayout = repeatlayout.table;
}
}
/// <summary>
/// 选择项改变事件
/// </summary>
/// <param name="sender">控件发送对象</param>
/// <param name="e">事件对象</param>
protected void radiobuttonlist_demo_selectedindexchanged(object sender, eventargs e)
{
image_show.imageurl = radiobuttonlist_demo.selectedvalue.tostring();
}
}

重点
取得网站目录下某一个目录的路径
采用server.mappath(argurment)
参数采用
request.appliaction + "/目录名/"
这句话的意思是
请求服务器下的某个目录下的路径
路径完了就取的该路径下的所有文件名
通过system.io中的directory对象
的getfiles(request.appliaction)方法
只能该目录下的所有文件名,可以包含扩展名
路径还是需要用request.application + "/file/"的方式来取得
注释已经写的很清楚了.
可以练习一下