簡體   English   中英

使用C#asp.net在gridview中創建超鏈接按鈕單元格

[英]Create hyperlink button cells in gridview using C# asp.net

我的網頁上有GridView。 這將顯示數據,其中包含狀態,名稱,標識和操作列 我的狀態欄始終隨機填充3個值( 完成,已排隊失敗 )。

現在,我想將此狀態列值顯示為鏈接,如果它的值是“ Failed”或“ Queued”。 但是,“完成”狀態不應顯示為鏈接。

在運行時如何實現此設計?

我的將數據綁定到網格的代碼是

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dtActionList = clsactionList.GetADActionList();
        grdADActionList.DataSource = dtActionList;
        grdADActionList.DataBind();
    }
    protected void grdADActionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow gvr in grdADActionList.Rows)
        {
            if ((gvr.FindControl("Label1") as Label).Text == "Completed")
            {
                (gvr.FindControl("Label1") as Label).Visible = true;
                (gvr.FindControl("HyperLink1") as HyperLink).Visible = false;
            }
        }
    }

使用此代碼,我只是簡單地將網格中的值綁定。 我無法基於該狀態列的綁定值創建具有鏈接按鈕的“狀態”列。

我的.aspx代碼是:

<asp:GridView ID="grdADActionList" runat="server" Height="83px" Width="935px" AutoGenerateColumns="false" OnRowDataBound="grdADActionList_RowDataBound">

     <Columns>
      <asp:TemplateField HeaderText="Status" SortExpression="Status">
            <ItemTemplate>
                 <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='http://localhost:52807/Default.aspx?'><%# Eval("Status") %>
                 </asp:HyperLink>
                 <asp:Label ID="Label1" runat="server" Text="<%# Container.DataItem %>" Visible="False"></asp:Label>
            </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="GivenName" HeaderText="GivenName"/>

請幫助我進一步執行此操作。

在GridViewDataBound事件上,如果值是complete ,則僅隱藏鏈接並顯示一個簡單標簽。
ASP.NET:

<asp:TemplateField HeaderText="Status" SortExpression="Status">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='your_url'>
            <%# Eval("Status") %>
        </asp:HyperLink>
        <asp:Label ID="Label1" runat="server" Text="<%# Eval("Status") %>" Visible="False"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

C#:

protected void onGridViewDataBound()
{
    foreach(GridViewRow gvr in grd)
        if((gvr.FindControl("Label1") as Label).Text.ToLower() == "complete") // Updated Line
        {
            (gvr.FindControl("Label1") as Label).Visible = true;
            (gvr.FindControl("HyperLink1") as HyperLink).Visible = false;
        }
}

您必須在設計文件中使用.aspx文件,並且

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
   <asp:TemplateField HeaderText="Hyperlink">
<ItemTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" 
        NavigateUrl='<%# Eval("CODE", @"http://localhost/Test.aspx?code={0}") %>' 
        Text='link to code'>
    </asp:HyperLink>
</ItemTemplate>

您可以在RowDataBound事件上放置處理程序

protected void gw_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)    
    {

        DataRowView v_DataRowView = (DataRowView)e.Row.DataItem;

        string NavigateUrl = <....place your link here with DataRowView info>

        e.Row.Attributes.Add("onclick", NavigateUrl);
    }
}

目前,您已自動生成了列,因此請先禁用該功能。 然后,您需要將每列定義為一個BoundField,對於hypelink,考慮到您的條件,最好的方法是定義模板字段:

<asp:GridView ID="grdADActionList" runat="server" BorderStyle="Double" BorderWidth="3px" 
               Height="83px" Width="935px"
               AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name"/>
            <asp:BoundField DataField="Action" HeaderText="Action"/>
            <asp:BoundField DataField="Id" HeaderText="Id"/>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:HyperLink runat="server" NavigateUrl="~/link/address" Text='<%# Eval("Status") %>'
                                   Visible='<%# (int)Eval("Status") != 1 %>'/>
                    <asp:Label runat="server" Text='<%# Eval("Status") %>'
                               Visible='<%# (int)Eval("Status") == 1 %>'>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

請注意,這只是一個變體-您尚未指定“ Status列包含哪些值,因此我假設這是基於整數的枚舉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM