asp.net 在grideview中添加链接列

<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:HyperLinkField DataTextField="Name" DataNavigateUrlFields="Id" DataNavigateUrlFormatString="~/Details.aspx?Id={0}"
            HeaderText="Name" ItemStyle-Width = "150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width = "150" />
    </Columns>
</asp:GridView>
DataTextField – Here we set the name of the field or column that we need to display.
DataNavigateUrlFields – Here we set the name of the field or column that we need to pass via QueryString parameter in URL.
DataNavigateUrlFormatString – Here we set the URL of the page along with the QueryString Parameter and also provide a place holder {0} that will be replaced by the actual value of the column that we have set in the DataNavigateUrlFields field at runtime.
 
第二个例子:
 
 
把 No的值传给 Details页面
 
 
那么怎么在Details的页面中获得传递出的参数呢
用Request.Url.ToString()方法获得 路径 然后截取就好了
public partial class CommonPage_Bulletin_Mg_Details : System.Web.UI.Page
{
    protected string ReverseString(string s)
    {
        char[] chars = s.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }  
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = Request.Url.ToString();
        string s = "";
        for (int i = url.Length-1; i >= 0; i--)
        {
            if (url[i] == '=') break;
            s += url[i];
        }
        s=ReverseString(s);
        OpreationDB db = new OpreationDB();
        string sqlStr = "select Bcontent from Bulletin where No =" + s;
        SqlDataReader dr =db.ExceRead(sqlStr);
        if (dr.Read())
        {
            tb1.Text = dr["Bcontent"].ToString().Trim();
        }
    }
}

 

posted on 2016-07-14 08:39  Beserious  阅读(291)  评论(0编辑  收藏  举报