GridView中的Href

时间:2015-02-18 03:16:08

标签: c# asp.net gridview

我只想问一下如何在GridView中使用不同的链接。

这是我的GridView的HTML代码。

<asp:GridView ID="gridsummary" runat="server" AutoGenerateColumns="False" 
                HorizontalAlign="Left">
               <Columns>
                   <asp:BoundField DataField="sstat" HeaderText="STATUS">
                   <ItemStyle Width="250px" HorizontalAlign="Left" VerticalAlign="Top" />
                   </asp:BoundField>
                   <asp:BoundField DataField="ctr" HeaderText="COUNT" >
                   <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="150px" />
                   </asp:BoundField>
                   <asp:BoundField DataField="sstat" HtmlEncode="False" DataFormatString="<a href='webViewDelivered.aspx'>View</a>" />
               </Columns>
               <HeaderStyle CssClass="GRIDVIEW_TITLE" />
               <RowStyle CssClass="GRIDVIEW_DETAILS" />
            </asp:GridView>

加载页面时,这是我的c#代码。

if (!IsPostBack)
        {
            dsSummary = dm.MainDash(Session["Username"].ToString(), "");
            gridsummary.DataSource = dsSummary.Tables[0];
            gridsummary.DataBind();


            string sDelivered = string.Empty;
            string sNoAirway = string.Empty;
            string sIntransit = string.Empty;
            string sReturned = string.Empty;
            DataTable dt = new DataTable();
            dt = dsSummary.Tables[0];
            List<string> slistPostal = new List<string>();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr[0].ToString() == "DELIVERED")
                {
                    sDelivered = dr["HTML"].ToString();
                    Delivered.InnerHtml = sDelivered;
                }

                if (dr[0].ToString() == "FOR PROCESSING")
                {
                    sNoAirway = dr["HTML"].ToString();
                    NoAirway.InnerHtml = sNoAirway;
                }


                if (dr[0].ToString() == "IN TRANSIT")
                {
                    sIntransit = dr["HTML"].ToString();
                    Intransit.InnerHtml = sIntransit;
                }

                if (dr[0].ToString() == "RETURNED")
                {
                    sReturned = dr["HTML"].ToString();
                    Returned.InnerHtml = sReturned;
                }

            }
        }

我想要的是这样的:

如果我选择GridView的第一行,第三个BoundField就是这个

<asp:BoundField DataField="sstat" HtmlEncode="False" DataFormatString="<a href='webViewDelivered.aspx'>View</a>" />

如果我选择第二行,则第三个BoundField将更改为此

<asp:BoundField DataField="sstat" HtmlEncode="False" DataFormatString="<a href='webViewForProcessing.aspx'>View</a>" />

当选择不同的行时,href将根据我要加载的页面进行修改,是否有条件?

PS:我的存储过程很长,所以对于你的参考sstat等于上面的条件..例如在我的SP中我有select 'DELIVERED' AS sstat

1 个答案:

答案 0 :(得分:0)

使用TemplateField而不是BoundField

    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <HyperLink ID="RedirectBtn" runat="server"
                OnClick="RedirectBtn_Click" />

        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center"></ItemStyle>
    </asp:TemplateField>

您可以在此之后在网格的OnRowDataBound事件中添加您想要的任何内容。如果您的RowDataBound事件名为Grid_RowDataBound

protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.DataItem == null)
        return;

    DataRowView row = e.Row.DataItem as DataRowView;

    HyperLinkbtn = e.Row.FindControl("RedirectBtn") as HyperLink;
    b.NavigateUrl = "write your link";
    //if you want to open new tab
    b.Target="_blank";

}

您正在将事件添加到网格中,如下所示:

  

OnRowDataBound =&#34; Grid_RowDataBound&#34;

如果您希望可以使用标记制作,如果您的href是静态的,并且您不需要对每一行进行更改。

<asp:TemplateField  HeaderText="YourHeaderText">
    <ItemTemplate>
        <a target='_blank' href='Your link'>Text/a>
    </ItemTemplate>
</asp:TemplateField>