add control in literal

nickyboy

New member
Joined
Mar 30, 2009
Messages
3
Programming Experience
1-3
if want to control if a statement is true or false in a literal, depanding on this the page should display an 'x' or a button

the x is already working but the button isn't, probably because it is serverside
i tried 2 ways to display the button but none worked...

the literal is inside a listview

does anyone know how i can get the button on the page?

here's my code:

<ItemTemplate runat="server">
<tr id="row" runat="server">
<td> <a href=<%# "detailrooster.aspx?DagID=" & Eval("DagID") %>><%#Eval("Datum").ToString.Substring(0, 9)%></a> </td>

<td> <%#Eval("Uur")%></td>
<td> <%#Eval("Naam")%></td>
<td align="center"><asp:Literal ID="LiteralBevestigd" runat="server" /> </td>
<td align="center"><asp:Literal ID="LiteralAfgekeurd" runat="server" /> </td>

</tr>

</ItemTemplate>


Private Sub Listview1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
Dim LiteralBevestigd As Literal = e.Item.FindControl("LiteralBevestigd")
Dim LiteralAfgekeurd As Literal = e.Item.FindControl("LiteralAfgekeurd")
'dim myliteral as literal = CTYPE(sender,listview1)
Dim lvdiContainer As ListViewDataItem = CType(LiteralBevestigd.NamingContainer, ListViewDataItem)
Dim bevestigd As String = DataBinder.Eval(lvdiContainer.DataItem, "Bevestigd").ToString()
Dim afgekeurd As String = DataBinder.Eval(lvdiContainer.DataItem, "Afgekeurd").ToString()




If afgekeurd.Equals("False") And bevestigd.Equals("False") Then

LiteralAfgekeurd.Text &= String.Format(" <asp:Button ID=" & Chr(34) & "Button1" & Chr(34) & "runat=" & Chr(34) & "server" & Chr(34) & " Text=" & Chr(34) & "Button" & Chr(34) & " onclick=" & Chr(34) & "Button1_Click" & Chr(34) & "/> ", e.Item.ID)
LiteralBevestigd.Text &= "<asp:Button ID='Button2'runat='server' Text='Button' onclick='Button1_Click'/> "


Else

If bevestigd.Equals("True") Then
LiteralBevestigd.Text &= "x"
Else
LiteralAfgekeurd.Text &= "x"
End If

End If

End Sub
 
How to add the button to Listview programtically

Hello and welcome to the forum.

That's correct guess. You cannot add a server control in this way.
Even if you add it syntactically correct (see below) it will not show up again.

VB.NET:
LiteralBevestigd.Text &= "<asp:Button ID=""Button2"" runat=""server"" Text=""Button"" onclick=""Button1_Click"" />"
Rather, you should instantiate a new button object and add to the controls collection.

VB.NET:
Dim button1 As New Button
button1.ID = "Button2"
button1.Text = "Button"
AddHandler button1.Click, AddressOf Button1_Click

'You may now add the button at the place you want it to appear
e.g. (rough example)
Listview1.FindControl("LiteralAfgekeurd").Controls.Add(button1)
Hope this helps
 
hint looks pretty good but i get the next exception

Object reference not set to an instance of an object.

the code i have:

Dim LiteralBevestigd As Literal = e.Item.FindControl("LiteralBevestigd")
Dim LiteralAfgekeurd As Literal = e.Item.FindControl("LiteralAfgekeurd")
'dim myliteral as literal = CTYPE(sender,listview1)
Dim lvdiContainer As ListViewDataItem = CType(LiteralBevestigd.NamingContainer, ListViewDataItem)
Dim bevestigd As String = DataBinder.Eval(lvdiContainer.DataItem, "Bevestigd").ToString()
Dim afgekeurd As String = DataBinder.Eval(lvdiContainer.DataItem, "Afgekeurd").ToString()

Dim button1 As New Button
button1.ID = "Button2"
button1.Text = "Button"
AddHandler button1.Click, AddressOf Button1_Click

'You may now add the button at the place you want it to appear e.g. (rough example)



If afgekeurd.Equals("False") And bevestigd.Equals("False") Then
ListView1.FindControl("LiteralAfgekeurd").Controls.Add(button1)
 
solved that one; next problem:

'System.Web.UI.WebControls.Literal' does not allow child controls.

code:

Dim LiteralBevestigd As Literal = e.Item.FindControl("LiteralBevestigd")
Dim LiteralAfgekeurd As Literal = e.Item.FindControl("LiteralAfgekeurd")
'dim myliteral as literal = CTYPE(sender,listview1)
Dim lvdiContainer As ListViewDataItem = CType(LiteralBevestigd.NamingContainer, ListViewDataItem)
Dim bevestigd As String = DataBinder.Eval(lvdiContainer.DataItem, "Bevestigd").ToString()
Dim afgekeurd As String = DataBinder.Eval(lvdiContainer.DataItem, "Afgekeurd").ToString()

Dim button1 As New Button
button1.ID = "Button2"
button1.Text = "Button"
AddHandler button1.Click, AddressOf Button1_Click

If afgekeurd.Equals("False") And bevestigd.Equals("False") Then
Dim sting As String = button1.Text

LiteralAfgekeurd.Controls.Add(button1)
 
Back
Top