Dynamically adding different controls to different rows in DataGrid does not work

J

Jonas

Hi!

I've got a datagrid which I dynamically want to add controls to, and the
type of control depends on user rights for the item shown in the row. I use
the Datagrid ItemDatabound event to add either a linkbutton or a literal to
a certain column, but it does not work if I don't leave the declaration of
the linkbutton in the aspx-page. I can leave it commented, but it must be
left there. When I click on the link, the page submits and returns but now
the whole column is blank, and I'm still on the same page instead of being
redirected.

TIA

/Jonas

Html-code in aspx-page:

<asp:TemplateColumn>
<HeaderStyle Width="5%" CssClass="listheader"></HeaderStyle>
<ItemStyle CssClass="listitem"></ItemStyle>
<ItemTemplate>
<!--<asp:LinkButton id="lnbtDelegationDecision"
Runat="server"
CommandName="DelegationDecision.Read"
CommandArgument='<%#DataBinder.Eval(Container.DataItem,
"DelegationDecisionID")%>'
ToolTip="Tooltip">Text</asp:LinkButton>-->
</ItemTemplate>
</asp:TemplateColumn>

Code-behind in the ItemDataBound event:

If blnUserIsAuthorized Then
Dim ctl As New LinkButton
With ctl
.ID = "lnbtDelegationDecision"
.CommandArgument = e.Item.DataItem("DelegationDecisionID").ToString
.CommandName = "DelegationDecision.Read"
.ToolTip = "ToolTip"
.Text = "Text"
.EnableViewState = True
End With
' Add new control to third column in datagrid
e.Item.Controls(2).Controls.Add(ctl)
Else
Dim ctl As New Literal
ctl.Text = "Dummy"
' Add new control to third column in datagrid
e.Item.Controls(2).Controls.Add(ctl)
End If
 

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,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top