0

I am having a GridView in my web page. 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.

Now I want to display this status column values as a link ,If it is having the value either "Failed" or "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:

<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....

8
  • How does the markup for grdActionList look like? Commented Feb 21, 2014 at 9:26
  • @Andrei: Do u mean the design of the grid? Commented Feb 21, 2014 at 9:27
  • Yes. The content of aspx file related to the Grid Commented Feb 21, 2014 at 9:28
  • Its simply a Grid, have taken from the tools and binded with the Datatable values in that.... How can I create the template column based on the cell values as a link Commented Feb 21, 2014 at 9:33
  • 2
    For now all your columns are auto generated. This is fine if you are ok with default gird appearance, however when you need customization you should switch to manual columns declaration Commented Feb 21, 2014 at 9:41

4 Answers 4

1

On GridViewDataBound event, just hide the link and display a simple label if the value is 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;
        }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried with your code, Its creating hyperlinks for the entire column and Completed status also displaying as link
Do you have OnDataBound="onGridViewDataBound" in your asp:GridView tag? Else it won't fire the C# onGridViewDataBound() event handler which hides the link if status = "completed".
Yes, I have that in my code. I'll edit my post and will update my latest code... The problem is , OnDataRowBound event it does not enter into the condition if((gvr.FindControl("Label1") as Label).Text == "Complete")
Please see I have updated the C# code where it says "updated Line". Use that line and it will work.
The problem is, gvr.FindControl("Label1") as Label).Text does not return the cell value. When I am using the following attribute inside the label control Text="<%# Eval("Status") %>" , I'm getting some formation error. So I changed it like Text="<%# Container.DataItem %>"
|
0

you have to work in design file means .aspx file you have to use and

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

Comments

0

You can place an handler on RowDataBound event

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

Comments

0

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:

<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.

6 Comments

Please note she has clearly stated My status column always filled with the 3 values(Complete, Queued and Failed) randomly.
@P5Coder, well, I understand it as the column can contain one of three values, depending on which hyper link is either shown or not. Is this incorrect?
She has auto-generated the columns means she clearly hasnt specified any urls to those links. But i think the hyperlink again will depend on the value.
I am having one doubt in this. Are Hyperlink and Link button same? I need link buttons on that particular cells not as hyperlink.
@Suryakavitha, Hyperlink and LinkButton are a bit different and behave differently. Basically HyperLink is a simple link, while LinkButton behaves like a button although on the page it appears as a link as well. For eaxmple, click ob hyperlink will cause a redirect, while click on link button causes a postback of the current page. You should use the one suiting your needs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.