Link Button creation problem

B

Burak

Hello,

I have some issues with creating link buttons dynamically.

This is my link button creation string:

strUrl = "<asp:LinkButton CommandName='Onet3_Cd'
onfiltered='LinkButton_Command' runat='server' " & _
"Text='" & rdrOnet3("ONET3_TITLE") & "'
CommandArgument='" & rdrOnet3("ONET3_CD") & "' id='lb"
& i & "'></asp:LinkButton>"

Dim ctrl As Control =
Page.ParseControl(strUrl.ToString())
plcCategory.Controls.Add(ctrl) ' place holder object

The link button shows up fine when I run this, but
when I click on the link, it doesn't take me to

Sub LinkButton_Command(ByVal sender As Object, ByVal e
As CommandEventArgs)

Dim strDesc As String
strDesc = sender.text
Response.Write(sender.text)
End Sub

I based my code on

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr\
fsystemwebuiwebcontrolslinkbuttonclasscommandnametopic.asp

The click works if I hard code the link button on the
html page, but if I create it dynamically, the click
does not take me to the above sub.

Do you know how to fix this?

Thanks,

Burak
 
V

vMike

You might try something like this. An alternative is to take out the
withevents and the comment out the handler section and then put in the lines
I have commented out.

<%@ Page Language="vb" Strict=True %>
<html>
<script language="vb" runat="server">
Protected Dim Withevents ctrl as New linkbutton
Sub Page_Load(sender as object, e as EventArgs)
'Dim ctrl as new linkbutton 'add this if withevents line taken out
ctrl.id="link1"
ctrl.text = "link create"
ctrl.commandargument = "myargument"
ctrl.commandname = "mycommandname"
'addhandler ctrl.click, addressof LinkButton_Command 'add this if
withevents line taken out
plcCategory.Controls.Add(ctrl)
end sub

Sub LinkButton_Command(ByVal sender As Object, ByVal e As EventArgs) handles
ctrl.click 'comment after handers if withevents is removed
Dim strDesc As String
strDesc = directcast(sender,linkbutton).text
Response.Write(strDesc & "<br>")
Response.Write(directcast(sender,linkbutton).commandname & "<br>")
Response.write(directCast(sender,linkbutton).commandargument & "<br>")

End Sub

</script>
<form id="form1" runat="server">
<asp:placeHolder id="plcCategory"
runat="Server"/>

</form>

</html>
 
B

Burak

Hi,

Thank you this works fine for one linkbutton.

How can I have a bunch of list buttons arrangd ina vertical fashion?

Thank you,

Burak
 
V

vMike

You could use a for... next loop. But if you can , might opt to bind a
datatable or dataview to a datalist template. but I don't know exactly what
you are doing.
Something like this would be in your template page. In you codebehind you
would bind the datalist to your data.

<ASP:DataList id="Designers" showheader="false" showfooter="false"
repeatcolumns="1" repeatdirection="horizontal" borderwidth="0"
runat="server">
<ItemTemplate>
<asp:LinkButton
Cssclass="designers"
CommandArgument=<%# DataBinder.Eval(Container.DataItem,
"DesignerID") %>
CausesValidation="false"
OnClick="designer_click"
text=<%# DataBinder.Eval(Container.DataItem, "DesignerName")
%>
runat="server">

</asp:LinkButton>
</ItemTemplate>
</ASP:DataList>
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top