Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6609745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:45:15+00:00 2026-05-25T19:45:15+00:00

I’m creating a GridView with dynamic controls(including itemtemplates). Here is the html code. <asp:GridView

  • 0

I’m creating a GridView with dynamic controls(including itemtemplates).
Here is the html code.

<asp:GridView ID="gvItems" runat="server" 
                        AutoGenerateColumns="False" CellPadding="4" 
                        GridLines="None" 
                        Width="95%" EmptyDataText="Records Not Found!!!!" 
                        onselectedindexchanged="gvItems_SelectedIndexChanged" 
                        onrowdatabound="gvItems_RowDataBound" onrowcommand="gvItems_RowCommand" 
                        onselectedindexchanging="gvItems_SelectedIndexChanging">
                        <RowStyle CssClass="GVRowStyle" />
                        <Columns>

                        </Columns>
                        <PagerStyle CssClass="gridPager" />
                        <SelectedRowStyle BackColor="#DCCDDA" Font-Bold="true" ForeColor="#510030" />
                        <HeaderStyle CssClass="Gheader" />
                        <AlternatingRowStyle CssClass="AlternatingRowStyle" />
                        <EditRowStyle BackColor="#7C6F57" />
                        <EmptyDataRowStyle  CssClass="EmptyRowStyle" />
                    </asp:GridView>

Here is the class for Adding ItemTemplate……………………………..

public class GridViewTemplate : ITemplate
{
ListItemType _templateType;
string _columnName;

public GridViewTemplate(ListItemType type, string colname)
 {
    _templateType = type;
    _columnName = colname;
 }

void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
    switch (_templateType)
    {
        case ListItemType.Header:
            Label lbl = new Label();  
            lbl.Text = _columnName;   
            container.Controls.Add(lbl);
            break;

        case ListItemType.Item:
            LinkButton Lb1 = new LinkButton(); 
            Lb1.CommandName = "Select";
            Lb1.DataBinding += new EventHandler(tb1_DataBinding);
            container.Controls.Add(Lb1);
            break;

        case ListItemType.EditItem:

            break;

        case ListItemType.Footer:
            CheckBox chkColumn = new CheckBox();
            chkColumn.ID = "Chk" + _columnName;
            container.Controls.Add(chkColumn);
            break;
    }
 }

 void tb1_DataBinding(object sender, EventArgs e)
 {
    LinkButton LinkData = (LinkButton)sender;
    GridViewRow container = (GridViewRow)LinkData.NamingContainer;
    object dataValue = DataBinder.Eval(container.DataItem, _columnName);
    if (dataValue != DBNull.Value)
    {
        LinkData.Text = dataValue.ToString();
        LinkData.ForeColor = System.Drawing.Color.Red;
        LinkData.CommandName = "Select";
    }
}

}

Here is the page code…………………………………..

for (int i = 0; i < dtGrid.Columns.Count; i++)
        {
            string columnName = dtGrid.Columns[i].ColumnName;
            BoundField bField = new BoundField();
            TemplateField tField = new TemplateField();
            if (i == 0)
            {
                tField.HeaderTemplate = new GridViewTemplate(ListItemType.Header,    columnName);
                tField.ItemTemplate = new GridViewTemplate(ListItemType.Item, columnName);
                tField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                tField.ItemStyle.ForeColor = System.Drawing.Color.Red;
                gvItems.Columns.Add(tField);

            }
            else
            {

                bField.DataField = columnName;
                bField.HeaderText = columnName;
                bField.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                gvItems.Columns.Add(bField);
            }

Now Problem is when i click on linkbutton it disappear and no event other than rowdatabound is raised.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:45:15+00:00Added an answer on May 25, 2026 at 7:45 pm

    Since you’re creating these columns dynamically, you’ll need to recreate the controls at every postback. As for the event handler not firing, make sure you create the columns early enough in the page lifecycle. I would suggest creating the dynamic columns OnInit, and see if that fixes the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.