国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET 服務器路徑和一般資源調用

2024-07-10 13:19:18
字體:
來源:轉載
供稿:網友
頁面代碼:

復制代碼 代碼如下:


<%@ 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 runat="server">
<div>
<asp:RadioButtonList runat="server" OnSelectedIndexChanged="RadioButtonList_Demo_SelectedIndexChanged"
AutoPostBack="true">
</asp:RadioButtonList>
<br />
<asp:Image 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>控件發送對象</param>
/// <param>事件對象</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>控件發送對象</param>
/// <param>事件對象</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/"的方式來取得
注釋已經寫的很清楚了.
可以練習一下
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿鲁科尔沁旗| 崇阳县| 陵水| 炉霍县| 紫阳县| 灵台县| 鹤岗市| 张家口市| 侯马市| 新龙县| 沈丘县| 句容市| 西畴县| 巩留县| 武城县| 望江县| 南京市| 临澧县| 宜章县| 龙海市| 深圳市| 安塞县| 梧州市| 黄冈市| 岐山县| 山东省| 文水县| 宣恩县| 加查县| 兰溪市| 灵武市| 湟中县| 鄂托克前旗| 连平县| 清远市| 伊金霍洛旗| 曲松县| 新巴尔虎右旗| 临高县| 明水县| 南和县|