zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

NetASP资源服务器 调用 路径 一般
2023-06-13 09:14:11 时间
页面代码:
复制代码代码如下:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="RadioButtonListDemo.aspx.cs"
Inherits="_Default"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:RadioButtonListID="RadioButtonList_Demo"runat="server"OnSelectedIndexChanged="RadioButtonList_Demo_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
<br/>
<asp:ImageID="Image_Show"runat="server"/>
</div>
</form>
</body>
</html>

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

usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingCDataBase;
usingSystem.IO;
publicpartialclass_Default:System.Web.UI.Page
{
///<summary>
///页面加载事件
///</summary>
///<paramname="sender">控件发送对象</param>
///<paramname="e">事件对象</param>
protectedvoidPage_Load(objectsender,EventArgse)
{
//取得ConnectionString的值
//Response.Write("<script>alert(""+SqlHelper.conString+"")</script>");
if(!IsPostBack)
{
//先要有路径系统根目录下福娃文件夹下的文件路径
stringsPath=Server.MapPath(Request.ApplicationPath+"/福娃/");
//取得这个路径下面所有的文件名包含其路径
string[]sFiles=Directory.GetFiles(sPath);
//循环所有文件的路径
foreach(stringsFileinsFiles)
{
//取文件名
stringsName=Path.GetFileNameWithoutExtension(sFile);
//取文件名,包含扩展名
stringsFileName=Path.GetFileName(sFile);
//建立RadioButtonList的子项,采用Text/Value的重载方式
ListItemrItem=newListItem(sName,Request.ApplicationPath+"/福娃/"+sFileName);
//将子项添加到RadioButtonList里
RadioButtonList_Demo.Items.Add(rItem);
}
//设置RBL中单选按钮的显示排列方式
RadioButtonList_Demo.RepeatDirection=RepeatDirection.Horizontal;
RadioButtonList_Demo.RepeatLayout=RepeatLayout.Table;
}
}
///<summary>
///选择项改变事件
///</summary>
///<paramname="sender">控件发送对象</param>
///<paramname="e">事件对象</param>
protectedvoidRadioButtonList_Demo_SelectedIndexChanged(objectsender,EventArgse)
{
Image_Show.ImageUrl=RadioButtonList_Demo.SelectedValue.ToString();
}
}

重点
取得网站目录下某一个目录的路径
采用Server.MapPath(Argurment)
参数采用
Request.Appliaction+"/目录名/"
这句话的意思是
请求服务器下的某个目录下的路径
路径完了就取的该路径下的所有文件名
通过System.IO中的Directory对象
的GetFiles(Request.Appliaction)方法
只能该目录下的所有文件名,可以包含扩展名
路径还是需要用Request.Application+"/File/"的方式来取得
注释已经写的很清楚了.
可以练习一下