程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> ASP.NET 服務器路徑和一般資源調用

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

編輯: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/"的方式來取得
注釋已經寫的很清楚了.
可以練習一下
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved