Link Button creation problem

Discussion in 'ASP .Net Web Controls' started by Burak, Sep 18, 2003.

  1. Burak

    Burak Guest

    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
    Burak, Sep 18, 2003
    #1
    1. Advertising

  2. Burak

    vMike Guest

    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>
    "Burak" <> wrote in message
    news:...
    > 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
    vMike, Sep 19, 2003
    #2
    1. Advertising

  3. Burak

    Burak Guest

    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


    "vMike" <> wrote in message news:<bkfggm$cq7$>...
    > 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>
    > "Burak" <> wrote in message
    > news:...
    > > 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
    Burak, Sep 22, 2003
    #3
  4. Burak

    vMike Guest

    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>
    "Burak" <> wrote in message
    news:...
    > 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
    >
    >
    > "vMike" <> wrote in message

    news:<bkfggm$cq7$>...
    > > 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>
    > > "Burak" <> wrote in message
    > > news:...
    > > > 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
    vMike, Sep 22, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan
    Replies:
    1
    Views:
    512
    Scott Allen
    Nov 5, 2004
  2. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    800
    Kevin Spencer
    May 17, 2005
  3. Steve
    Replies:
    0
    Views:
    335
    Steve
    May 25, 2005
  4. Suzanne

    Problems with Link Button and Button web controls

    Suzanne, Dec 3, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    148
    Suzanne
    Dec 3, 2003
  5. GIAM

    Problems with dynamic link creation

    GIAM, Apr 9, 2010, in forum: Javascript
    Replies:
    19
    Views:
    284
    Dr J R Stockton
    Apr 12, 2010
Loading...

Share This Page