Hyperlink template column and change link text based on Condition

Discussion in 'ASP .Net Datagrid Control' started by Paul D. Fox, Jun 15, 2005.

  1. Paul D. Fox

    Paul D. Fox Guest

    How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

    <asp:TemplateColumn HeaderText="Status">
    <ItemTemplate>
    <a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
    <asp:HyperLink ID="lnkApproval"></asp:HyperLink>
    </a>
    </ItemTemplate>
    </asp:TemplateColumn>

    And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

    Paul
    Paul D. Fox, Jun 15, 2005
    #1
    1. Advertising

  2. Paul D. Fox

    Kim Quigley Guest

    Paul,

    There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    If something then
    CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
    End If
    End If

    Your column would need to look like this

    <asp:TemplateColumn>
    <ItemTemplate>
    <a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
    </ItemTemplate>
    </asp:TemplateColumn>

    "Paul D. Fox" <> wrote in message news:...
    How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

    <asp:TemplateColumn HeaderText="Status">
    <ItemTemplate>
    <a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
    <asp:HyperLink ID="lnkApproval"></asp:HyperLink>
    </a>
    </ItemTemplate>
    </asp:TemplateColumn>

    And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

    Paul
    Kim Quigley, Jun 16, 2005
    #2
    1. Advertising

  3. Paul D. Fox

    Paul D. Fox Guest

    Thanks Kim, that was a big help however, I had to resort to:
    "If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then"

    In order for it to properly execute. I don't understand why your IF statement failed.

    Paul
    "Kim Quigley" <> wrote in message news:...
    Paul,

    There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    If something then
    CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
    End If
    End If

    Your column would need to look like this

    <asp:TemplateColumn>
    <ItemTemplate>
    <a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
    </ItemTemplate>
    </asp:TemplateColumn>

    "Paul D. Fox" <> wrote in message news:...
    How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

    <asp:TemplateColumn HeaderText="Status">
    <ItemTemplate>
    <a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
    <asp:HyperLink ID="lnkApproval"></asp:HyperLink>
    </a>
    </ItemTemplate>
    </asp:TemplateColumn>

    And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

    Paul
    Paul D. Fox, Jun 16, 2005
    #3
  4. Paul D. Fox

    Kim Quigley Guest

    I'm glad you were able to figure it out :)
    "Paul D. Fox" <> wrote in message news:%...
    Thanks Kim, that was a big help however, I had to resort to:
    "If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then"

    In order for it to properly execute. I don't understand why your IF statement failed.

    Paul
    "Kim Quigley" <> wrote in message news:...
    Paul,

    There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    If something then
    CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
    End If
    End If

    Your column would need to look like this

    <asp:TemplateColumn>
    <ItemTemplate>
    <a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
    </ItemTemplate>
    </asp:TemplateColumn>

    "Paul D. Fox" <> wrote in message news:...
    How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

    <asp:TemplateColumn HeaderText="Status">
    <ItemTemplate>
    <a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
    <asp:HyperLink ID="lnkApproval"></asp:HyperLink>
    </a>
    </ItemTemplate>
    </asp:TemplateColumn>

    And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

    Paul
    Kim Quigley, Jun 16, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    692
    Remon van Vliet
    Jun 15, 2005
  2. davetichenor
    Replies:
    1
    Views:
    821
    Eliyahu Goldin
    Oct 30, 2006
  3. Michael
    Replies:
    4
    Views:
    877
    Steven Cheng [MSFT]
    Jun 20, 2008
  4. Earl Teigrob

    Making datagrid hyperlink column displays Link or Text???

    Earl Teigrob, Jan 19, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    173
    Earl Teigrob
    Jan 19, 2004
  5. Bill W.
    Replies:
    13
    Views:
    293
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page