0

I am trying to dynamically create a template field which has an item template consisting of a hyperlink field. Trying to recreate the below code in the backend asp.net code:

<asp:TemplateField HeaderText="Total" SortExpression="TotalCases" ItemStyle-HorizontalAlign ="Center" ControlStyle-ForeColor="Black">
                           <ItemTemplate>
                              <asp:HyperLink ID="TotalCases" Target="_blank" runat="server" Text='<%# Eval("TotalCases") %>' />
                           </ItemTemplate>
                        </asp:TemplateField>

I have created a new template field called totalcases and now I am trying to get the item template as a hyperlink field which is where I am getting stuck

TemplateField TotalCases = new TemplateField();
            TotalCases.HeaderText = "Total";
            TotalCases.SortExpression = "TotalCases";
            TotalCases.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            TotalCases.ControlStyle.ForeColor = System.Drawing.Color.Black;
            TotalCases.ItemTemplate = new HyperLink(); //THIS DOESNT WORK
            clientgv.Columns.Add(TotalCases);

Error: cannot convert type System.Web.UI.WebControls.Hyperlink to System.Web.UI.Template. An explicit conversion exists.

I am trying to get the hyperlink URL assigned in the rowdatabound method but it cant find the hyperlink and is coming back as null on debug

 HyperLink hltc = (HyperLink)e.Row.FindControl("TotalCases");
8
  • How do you put the hyperlink field inside the item template? Commented Jun 25, 2019 at 10:02
  • why would you want to do this? It would be much easier to hide the existing column then to add one programatically. Commented Jun 25, 2019 at 10:04
  • I need it to be done dynamically as it loops through a sql code and generates gridview based on the number of rows in the output. I dont want this hardcoded i want it dynamically done in the back end code. Its my requirements.The existing column will then be removed once I have figured out how to dynamically create it in the back end Commented Jun 25, 2019 at 10:07
  • class HyperlinkColumn : ITemplate{ public void InstantiateIn(System.Web.UI.Control container) { HyperLink hypLink = new yperLink(); container.Controls.Add(link); }} Try this link stackoverflow.com/questions/13288215/… Commented Jun 25, 2019 at 10:10
  • Dont think i get it. In the example they add 'link' what is link? Its not defined anywhere Commented Jun 25, 2019 at 11:05

1 Answer 1

1

Have a look on the below code.

TemplateField customField = new TemplateField();
customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "FirstName", ControlType.HyperLink);
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "First Name", ControlType.Label);
gv.Columns.Add(customField);


 public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private ControlType controlType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, string colname, ControlType contType)
        {
            templateType = type;
            columnName = colname;
            controlType = contType;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = "<b>" + columnName + "</b>";
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    WebControl firstName = null;
                    switch (controlType)
                    {
                        case ControlType.Label:
                            firstName = new Label();
                            break;
                        case ControlType.HyperLink:
                            firstName = new HyperLink();
                            break;
                        default:
                            break;
                    }
                    firstName.DataBinding += new EventHandler(this.FirstName_DataBinding);
                    container.Controls.Add(firstName);
                    break;
            }
        }

        private void FirstName_DataBinding(Object sender, EventArgs e)
        {
            GridViewRow row = null;
            switch (controlType)
            {
                case ControlType.Label:
                    Label l = (Label)sender;
                    row = (GridViewRow)l.NamingContainer;
                    l.Text = DataBinder.Eval(row.DataItem, "FirstName").ToString();
                    break;
                case ControlType.HyperLink:
                    HyperLink l2 = (HyperLink)sender;
                    row = (GridViewRow)l2.NamingContainer;
                    l2.Text = DataBinder.Eval(row.DataItem, "FirstName").ToString();
                    l2.NavigateUrl = "https://www.google.com";
                    break;
            }
        }
    }
    public enum ControlType
    {
        Label = 1,
        HyperLink = 2
    }
Sign up to request clarification or add additional context in comments.

Comments

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.