Datagrid Hyperlink Doubt

Discussion in 'ASP .Net Web Controls' started by Arun, May 7, 2005.

  1. Arun

    Arun Guest

    Hi ,

    I have doubt regarding disabling Hyerlink in Datagrid. For some cases I
    don't want to show Hyperlink
    and some cases I want to show the Hyperlink Column. I don't want to hide the
    entire Column.

    -thanks
    Arun
    Arun, May 7, 2005
    #1
    1. Advertising

  2. Arun

    Brock Allen Guest

    You will have to then hide the indvidual controls in the cell that you don't
    want visible. A good place to do that is in the ItemDataBound event of the
    Grid, or you can build your own TemplateColumn.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi ,
    >
    > I have doubt regarding disabling Hyerlink in Datagrid. For some cases
    > I
    > don't want to show Hyperlink
    > and some cases I want to show the Hyperlink Column. I don't want to
    > hide the
    > entire Column.
    > -thanks
    > Arun
    Brock Allen, May 7, 2005
    #2
    1. Advertising

  3. Thanks for Brock's good suggestions.

    Hi Arun,

    IMO, if your want to individually hidden some certain controls in a
    columns, I think at first we should choose TemplateColumn since its the
    most flexible one. Then, we should explicitly assign an "ID" for those
    controls we want to reference later. After that, we can access those
    controls in DataGrid's ItemDataBound/ItemCreated event or in any other
    control (on the same page)'s post back event and modify those controls'
    propety.

    For example, suppose we have the following datagrid template:

    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="dgLink" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:BoundColumn DataField="title" HeaderText="Title"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Link">
    <ItemTemplate>
    <asp:HyperLink id="hlVisit" runat="server" NavigateUrl='<%#
    ((System.Data.DataRowView)Container.DataItem)[1] %>'>Visit</asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>
    </form>


    Then, we can hidden the hyperlink controls in some certain rows throw
    ItemDataBound event or in the button's postback event like below:

    ======= using itemdatabound event=========
    private void dgLink_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    if(e.Item.ItemIndex == 4)
    {
    HyperLink hlVisit = e.Item.FindControl("hlVisit") as HyperLink;

    if(hlVisit != null)
    {
    hlVisit.Visible = false;
    }
    }
    }
    }

    =====using button's click event=========

    private void btnSubmit_Click(object sender, System.EventArgs e)
    {
    DataGridItem dgi = dgLink.Items[4];
    HyperLink hlVisit = dgi.FindControl("hlVisit") as HyperLink;

    if(hlVisit != null)
    {
    hlVisit.Visible = false;
    }
    }


    Hope also helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], May 8, 2005
    #3
    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. davetichenor
    Replies:
    1
    Views:
    803
    Eliyahu Goldin
    Oct 30, 2006
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    593
  3. Dave
    Replies:
    0
    Views:
    922
  4. Replies:
    0
    Views:
    535
  5. Peter Otten
    Replies:
    2
    Views:
    102
    Cousin Stanley
    Aug 10, 2013
Loading...

Share This Page