Link to home
Create AccountLog in
Avatar of AmyL
AmyL

asked on

add linkbutton template field at runtime

I am adding template fields dynamically at runtime, and they contain linkbuttons. Everything seems to work, however my event isn't firing (the page is posting back, but when I put code in the event, it doesn't fire). Can someone please tell me what I am missing?

Here is the code:
 
private void addFields()
{
string colToAdd = "col" + i.ToString();

                TemplateField lname = new TemplateField();
                lname.HeaderText = "";  
                lname.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, colToAdd, i.ToString());
                gridPath.Columns.Add(lname);
}

public class AddTemplateToGridView : ITemplate
    {
        ListItemType _type;
        string _colName;
        string _pos;

        public AddTemplateToGridView(ListItemType type, string colname, string pos)
        {
            _type = type;

            _colName = colname;

            _pos = pos;

        }
        void ITemplate.InstantiateIn(System.Web.UI.Control container)
        {

            switch (_type)
            {
                case ListItemType.Item:

                    

                    LinkButton ib = new LinkButton();
                    ib.CommandName = "FolderNav";
                    ib.DataBinding += new EventHandler(ib_DataBinding);
                    container.Controls.Add(ib);

                    break;
            }

        }

        void ib_DataBinding(object sender, EventArgs e)
        {


            LinkButton imb = (LinkButton)sender;
            GridViewRow container = (GridViewRow)imb.NamingContainer;
            object dataValue = DataBinder.Eval(container.DataItem, _colName);
            if (dataValue != DBNull.Value)
            {
                imb.Text = dataValue.ToString();
                imb.CommandArgument = _pos;
                
            }


        }

    }










    protected void gridPath_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Label2.Text = e.CommandName.ToString();
        if (e.CommandName.ToString().ToUpper() == "FOLDERNAV")
        {
            Label2.Text = e.CommandArgument.ToString();
        }

    }

Open in new window

Avatar of Jini Jose
Jini Jose
Flag of India image

did u added the eventhandlers for your linkbuttons ?
maybe because there is no ID for your linkbutton
so it is not finding its event in code behind
try to add an id
also make sure both are same capitalization
"FolderNav"   FOLDERNAV
ASKER CERTIFIED SOLUTION
Avatar of Muhammad Zaman
Muhammad Zaman
Flag of Pakistan image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of AmyL
AmyL

ASKER

I tried adding an id, and I tried creating the fields in page_load, but haven't had any luck yet.
Have you checked out the link i suggested.
Avatar of AmyL

ASKER

I was wrong - it was the fact that I needed to recreate the controls on page_load. Thanks!
Amy