Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using asp.net gridview to display my records from list. My gridview will contain boundfields and templatefields. Template fields will have link button in itemtemplate to delete row by firing row_command event. My columns in gridview are to maintain display order in columns based on values set in database. In order to maintain column display order, i cloned gridview and added columns in gridview dynamically. It is working fine. But the problem is i need to add template field that will contain linkbutton in gridview dynamically.
The templatefield code that is to be created in runtime is:

XML
<asp:TemplateField>
    <ItemTemplate>
        <center>
            <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Bind("DrCrGroupNo") %>' />
        </center>
    </ItemTemplate>
</asp:TemplateField>


My code to clone and rebind columns is:

C#
var columnToMove = gridTransactionDetail.Columns.CloneFields();
 gridTransactionDetail.Columns.Clear();
gridTransactionDetail.Columns.Add(listTran);

In order to add template field in runtime, i added a code. My problems is this code is not working, The code i addded is :

public class TemplateHandler : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
LinkButton lnkDelete = new LinkButton();
lnkDelete.ID = "lnkDeleteThis";
lnkDelete.Text = "HI";
lnkDelete.Click += new EventHandler(Dynamic_Method);
container.Controls.Add(lnkDelete);
}
protected void Dynamic_Method(object sender, EventArgs e)
{
((LinkButton)sender).Text = "Hellooooo";
}
}

On Page_Init method(where clone and rebind columns is done ) as well as page_load, i added code to create gridview dynamically.The code is :

TemplateField TmpCol = new TemplateField();
TmpCol.HeaderText = "Click Me";
gridTransactionDetail.Columns.Add(TmpCol);
TmpCol.ItemTemplate = new TemplateHandler();


I though I will add remaining properties of linkbutton like commandname, commandargument, onclick on RowDataBound event. the code is :

var find =(LinkButton) e.Row.Cells[10].FindControl("lnkDeleteThis");
here, find value is null. As a result, i am not able to add properties and delete row from gridview, How can i do it? any help? thanks in advance.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900