重写URL

--------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();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值