How to dynamically add control to datagrid item template

G

Guest

I am trying to dynamically add a control within an itemtemplate container in
a gridview and need some assistance.

For example, my goal is to evaluate a column's value in a datagrid row and
depending on it's value dynamically add a few hyperlink controls to within
the template field.

I'm familiar with doing this in .NET 1.1 using the itemondatabound event and
interrogating the datagrid and am looking for a similiar way to do this with
the dataview, ideally all in a similiar event.

Any help is appreciated!

<asp:TemplateField HeaderText="Converted FileName"
SortExpression="ConvertedFileName">
<ItemTemplate>
<asp:Label ID="lblConvertedFileName"
runat="server" Text='<%# Bind("ConvertedFileName") %>'></asp:Label>
<br />
<asp:HyperLink ID="hlDownloadCFN" runat="server"
NavigateUrl="~/Admin/Admin_Bundles.aspx" Text="Download"></asp:HyperLink>
<br />
<asp:HyperLink ID="hlViewCFN" runat="server"
NavigateUrl="~/Admin/Admin_Bundles.aspx" Text="View"></asp:HyperLink>
<br />
<asp:HyperLink ID="hlUploadCFN" runat="server"
NavigateUrl="~/Admin/Admin_Bundles.aspx" Text="Upload"></asp:HyperLink>
<br />
<asp:HyperLink ID="hlDeleteCFN" runat="server"
NavigateUrl="~/Admin/Admin_Bundles.aspx" Text="Delete"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
 
G

Guest

Hi,
Now in gridview instead of ItemDataBound there is RowDataBound event.You can
something like this:
<asp:GridView ID="clients_gridview" runat="server"
AutoGenerateColumns="False"
DataSourceID="LinqDataSource1" AllowSorting="True"
onrowdatabound="GridView1_RowDataBound">
And then in GridView1_RowDataBound write something like this:
HyperLink hl = (HyperLink)e.Row.FindControl("nameofhyperlink");
hl.NavigateUrl = "yoururl";
Or try this:
((HyperLink)e.Row.Cells[1].Controls[0]).NavigateUrl = "yoururl";
RowDataBound event fires when data is explicitly bound to data web control.
Also you would find site http://gridviewguy.com/ pretty useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top