--------Web.config----------------
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="UrlReWrite" type="模块与处理程序.UrlReWrite"/>
</httpModules>
</system.web>
</configuration>
----------UrlReWrite.cs类------------
namespace 模块与处理程序
{
public class UrlReWrite:IHttpModule
{
public void Dispose()
{
//没有要释放的资源就什么也不写
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpRequest re = app.Request;
string path = re.Path;
if (path.Substring(path.LastIndexOf("/") + 1) == "viewDetail.html")
{
string[] strs = path.Split('/');
if (strs.Length != 4)
{
return;
}
else
{
string sid = strs[1];
string sname = strs[2];
app.Context.RewritePath("~/view.aspx?id=" + sid + "&name=" + sname);
}
}
}
}
}
-----------default2.aspx前台-------------------
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="sid" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="sid" HeaderText="sid" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="sname" SortExpression="sname" />
<asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
<asp:ImageField DataImageUrlField="photo"
DataImageUrlFormatString="~/images/{0}" HeaderText="照片">
</asp:ImageField>
<asp:HyperLinkField DataNavigateUrlFields="sid,sname"
DataNavigateUrlFormatString="{0}/{1}/viewDetail.html" HeaderText="操作"
Text="查看详情" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
SelectCommand="SELECT [sid], [sname], [sex], [age], [photo] FROM [student]">
</asp:SqlDataSource>
</div>
</form>
</body>
--------------view.aspx前台----------------------
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate> <table border="0" width="50%"><tr><th>ID</th><th>姓名</th><th>性别</th><th>年龄</th><th>照片</th></tr></HeaderTemplate>
<ItemTemplate>
<tr style="text-align:center;">
<td><%#Eval("sid") %></td>
<td><%#Eval("sname") %></td>
<td><%#Eval("sex") %></td>
<td><%#Eval("age") %></td>
<td><asp:Image ID="Image1" runat="server" Width="100px" Height="100px" ImageUrl='<%#"images/"+Eval("photo") %>'/></td>
</tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
----------view.aspx.cs后台---------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindStudent();
}
}
private void bindStudent()
{
string sid = this.Request["id"];
string constr = ConfigurationManager.ConnectionStrings["studentConnectionString"].ConnectionString;
string sql = "select sid,sname,age,sex,photo from student where sid=" + sid;
SqlConnection con = new SqlConnection(constr);
SqlDataAdapter adaper = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
adaper.Fill(dt);
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}