ASP.NET 服務(wù)器路徑和一般資源調(diào)用
更新時間:2009年08月04日 16:20:41 作者:
ASP.NET 服務(wù)器路徑和一般資源調(diào)用,實現(xiàn)代碼。
頁面代碼:
<%@ 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>無標(biāo)題頁</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">控件發(fā)送對象</param>
/// <param name="e">事件對象</param>
protected void Page_Load(object sender, EventArgs e)
{
//取得ConnectionString的值
//Response.Write("<script>alert('" + SqlHelper.conString + "')</script>");
if (!IsPostBack)
{
//先要有路徑 系統(tǒng)根目錄下 福娃文件夾 下的文件路徑
string sPath = Server.MapPath(Request.ApplicationPath + "/福娃/");
//取得這個路徑下面所有的文件名 包含其路徑
string[] sFiles = Directory.GetFiles(sPath);
//循環(huán)所有文件的路徑
foreach (string sFile in sFiles)
{
//取文件名
string sName = Path.GetFileNameWithoutExtension(sFile);
//取文件名, 包含擴(kuò)展名
string sFileName = Path.GetFileName(sFile);
//建立RadioButtonList的子項,采用 Text/Value 的重載方式
ListItem rItem = new ListItem(sName, Request.ApplicationPath + "/福娃/" + sFileName);
//將子項添加到RadioButtonList里
RadioButtonList_Demo.Items.Add(rItem);
}
//設(shè)置RBL中單選按鈕的顯示排列方式
RadioButtonList_Demo.RepeatDirection = RepeatDirection.Horizontal;
RadioButtonList_Demo.RepeatLayout = RepeatLayout.Table;
}
}
/// <summary>
/// 選擇項改變事件
/// </summary>
/// <param name="sender">控件發(fā)送對象</param>
/// <param name="e">事件對象</param>
protected void RadioButtonList_Demo_SelectedIndexChanged(object sender, EventArgs e)
{
Image_Show.ImageUrl = RadioButtonList_Demo.SelectedValue.ToString();
}
}
重點
取得網(wǎng)站目錄下某一個目錄的路徑
采用Server.MapPath(Argurment)
參數(shù)采用
Request.Appliaction + "/目錄名/"
這句話的意思是
請求服務(wù)器下的某個目錄下的路徑
路徑完了就取的該路徑下的所有文件名
通過System.IO中的Directory對象
的GetFiles(Request.Appliaction)方法
只能該目錄下的所有文件名,可以包含擴(kuò)展名
路徑還是需要用Request.Application + "/File/"的方式來取得
注釋已經(jīng)寫的很清楚了.
可以練習(xí)一下
復(fù)制代碼 代碼如下:
<%@ 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>無標(biāo)題頁</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>
后臺代碼:
復(fù)制代碼 代碼如下:
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">控件發(fā)送對象</param>
/// <param name="e">事件對象</param>
protected void Page_Load(object sender, EventArgs e)
{
//取得ConnectionString的值
//Response.Write("<script>alert('" + SqlHelper.conString + "')</script>");
if (!IsPostBack)
{
//先要有路徑 系統(tǒng)根目錄下 福娃文件夾 下的文件路徑
string sPath = Server.MapPath(Request.ApplicationPath + "/福娃/");
//取得這個路徑下面所有的文件名 包含其路徑
string[] sFiles = Directory.GetFiles(sPath);
//循環(huán)所有文件的路徑
foreach (string sFile in sFiles)
{
//取文件名
string sName = Path.GetFileNameWithoutExtension(sFile);
//取文件名, 包含擴(kuò)展名
string sFileName = Path.GetFileName(sFile);
//建立RadioButtonList的子項,采用 Text/Value 的重載方式
ListItem rItem = new ListItem(sName, Request.ApplicationPath + "/福娃/" + sFileName);
//將子項添加到RadioButtonList里
RadioButtonList_Demo.Items.Add(rItem);
}
//設(shè)置RBL中單選按鈕的顯示排列方式
RadioButtonList_Demo.RepeatDirection = RepeatDirection.Horizontal;
RadioButtonList_Demo.RepeatLayout = RepeatLayout.Table;
}
}
/// <summary>
/// 選擇項改變事件
/// </summary>
/// <param name="sender">控件發(fā)送對象</param>
/// <param name="e">事件對象</param>
protected void RadioButtonList_Demo_SelectedIndexChanged(object sender, EventArgs e)
{
Image_Show.ImageUrl = RadioButtonList_Demo.SelectedValue.ToString();
}
}
重點
取得網(wǎng)站目錄下某一個目錄的路徑
采用Server.MapPath(Argurment)
參數(shù)采用
Request.Appliaction + "/目錄名/"
這句話的意思是
請求服務(wù)器下的某個目錄下的路徑
路徑完了就取的該路徑下的所有文件名
通過System.IO中的Directory對象
的GetFiles(Request.Appliaction)方法
只能該目錄下的所有文件名,可以包含擴(kuò)展名
路徑還是需要用Request.Application + "/File/"的方式來取得
注釋已經(jīng)寫的很清楚了.
可以練習(xí)一下
您可能感興趣的文章:
相關(guān)文章
OpenCV 3.1.0+VS2015開發(fā)環(huán)境配置教程
這篇文章主要為大家詳細(xì)介紹了OpenCV 3.1.0+VS2015開發(fā)環(huán)境配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
深入理解__doPostBack 客戶端調(diào)用服務(wù)端事件
__doPostBack是一個純粹并且是非常簡單的javascript函數(shù),大部分的頁面PostBack都是由它觸發(fā)的。2008-08-08
ASP.NET MVC制作404跳轉(zhuǎn)實例(非302和200)
本篇文章主要介紹了ASP.NET MVC制作404跳轉(zhuǎn)實例(非302和200) ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04

