简体   繁体   English

使用C#asp.net在gridview中创建超链接按钮单元格

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

I am having a GridView in my web page. 我的网页上有GridView。 Which is displaying the data with the columns as Status, Name , Id and Action . 这将显示数据,其中包含状态,名称,标识和操作列 My status column always filled with the 3 values( Complete, Queued and Failed ) randomly. 我的状态栏始终随机填充3个值( 完成,已排队失败 )。

Now I want to display this status column values as a link ,If it is having the value either "Failed" or "Queued". 现在,我想将此状态列值显示为链接,如果它的值是“ Failed”或“ Queued”。 But "Complete" status should not be display as a link. 但是,“完成”状态不应显示为链接。

How Can I achive this design during the runtime? 在运行时如何实现此设计?

My Code for binding the data into the grid is, 我的将数据绑定到网格的代码是

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

using this code I am just simply binding the values in the grid. 使用此代码,我只是简单地将网格中的值绑定。 I am not able to create the Status column as having link buttons based on the binded values for that status column. 我无法基于该状态列的绑定值创建具有链接按钮的“状态”列。

My .aspx Code is: 我的.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"/>

Please help me to do this further.... 请帮助我进一步执行此操作。

On GridViewDataBound event, just hide the link and display a simple label if the value is complete . 在GridViewDataBound事件上,如果值是complete ,则仅隐藏链接并显示一个简单标签。
ASP.NET: 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#: 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;
        }
}

you have to work in design file means .aspx file you have to use and 您必须在设计文件中使用.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>

You can place an handler on RowDataBound event 您可以在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);
    }
}

For now you have your columns auto generated, so first disable that feature. 目前,您已自动生成了列,因此请先禁用该功能。 Then you need to define each column as a BoundField, and for the hypelink, taking into account your condition, the best way is to define template field: 然后,您需要将每列定义为一个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>

Note that this is just a variation - you have not specified what values does Status column contain, so I assumed this is int-based enumeration. 请注意,这只是一个变体-您尚未指定“ Status列包含哪些值,因此我假设这是基于整数的枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM