当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET 服务器路径和一般资源调用

ASP.NET
asp.net 禁用viewstate在web.config里
asp.net 虚方法、抽象方法、接口疑问
c# 操作符?? null coalescing operator
.net 反序题目的详细解答
implicitly convert type ''int'' to ''short''的原因与解决方法
比较完整的 asp.net 学习流程
官网 Ext direct包中.NET版的问题
C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等)
c# 连接字符串数据库服务器端口号 .net状态服务器端口号
ASP.NET 路径问题的解决方法
asp.net TemplateField模板中的Bind方法和Eval方法
asp.net Web.config 详细配置说明
asp.net 2个日期之间的整月数的算法
ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
Request.RawUrl 属性的应用收
.net 读取项目AssemblyInfo.cs属性值
asp.net URL 显示乱码 解决方法
asp.net 页面之间传递参数的几种方法
asp.net 一个封装比较完整的FTP类
C# FTP,GetResponse(),远程服务器返回错误

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 50 ::
收藏到网摘: n/a

ASP.NET 服务器路径和一般资源调用,实现代码。 页面代码:
复制代码 代码如下:

<%@ 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/"的方式来取得
注释已经写的很清楚了.
可以练习一下