Show boundcolumn as hyperlink

4 posts, 1 answers
  1. Tina
    Tina avatar
    130 posts
    Member since:
    Jul 2011

    Posted 19 Aug 2012 Link to this post

    I want to know if it is possible to show the gridboundcolumn as a hyperlink from code?(instead of using hyperlink)?
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 Aug 2012 Link to this post

    Hi Tina,

    Try the following code snippet to show the gridboundcolumn as a hyperlink.In the case of adding controls to the cells of GridBoundColumn,you cannot use ItemCreated only, but a combination of ItemCreated and ItemDataBound. This is due to the fact that the control created in ItemCreated will be erased when data-binding this control. Also, if you create the control in ItemDataBound when the controls are created from ViewState, the grid will not raise ItemDataBound, and the control will not be created and would not raise postback events. The solution for such cases is to create the control in ItemDataBound and recreate this control if needed on ItemCreated for subsequent postbacks.

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = new HyperLink();
            link.Text = item["UniqueName"].Text; //accessing the GridBoundColumn to get the text
            link.NavigateUrl = "Radgrid1.aspx";
            item["UniqueName"].Controls.Add(link);
        }
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            HyperLink link = new HyperLink();
            link.Text = item["UniqueName"].Text; //accessing the GridBoundColumn to get the text
            link.NavigateUrl = "Radgrid1.aspx";
            item["UniqueName"].Controls.Add(link);
        }
    }

    Thanks,
    Shinu.
  3. Tina
    Tina avatar
    130 posts
    Member since:
    Jul 2011

    Posted 20 Aug 2012 Link to this post

    Thanks, It works.I want to show a symbol(*) as required in edit mode not required field validator.but to show th field is mandatory.Can you share your thoughts?
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Aug 2012 Link to this post

    Hi Tina,

    I suppose you want to show a '*' symbol in the right side of the TextBox(BoundColumn in edit mode). Please try the following code snippet.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            Label lbl = new Label();
            lbl.Text = "*";
            lbl.ForeColor = Color.Red;
            edit["UniqueName"].Controls.Add(lbl);     
        }
    }

    Thanks,
    Shinu.
Back to Top