Link to home
Start Free TrialLog in
Avatar of simonlevey
simonlevey

asked on

LinkButtons

Hi Guys,

I have a problem with a site that I am developing. I have a need to randomly generate some linkbuttons. I have built the following example to demonstrate my problem. The following generates the linkbuttons. When the user clicks on the buttons, a textbox.text property is made equal to the command argument of the linkbutton.

Private Sub link_click(ByVal sender As System.Object, ByVal e As EventArgs)
TextBox1.Text = sender.commandargument
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim n As Integer
For n = 0 To 10
lnkbVisitID = New LinkButton()
lnkbVisitID.Text = n.ToString
lnkbVisitID.ID = "lnk" & n.ToString
lnkbVisitID.CommandArgument = n
PlaceHolder1.Controls.Add(lnkbVisitID)
lnkbVisitID.CausesValidation = False
lnkbVisitID.ForeColor = Color.FromName("#ff3333")
AddHandler lnkbVisitID.Click, AddressOf link_click
Next
End Sub

This works well, however I want to have it so that the link buttons are generated when a user clicks on a button, as follows:

Private Sub link_click(ByVal sender As System.Object, ByVal e As EventArgs)
TextBox1.Text = sender.commandargument
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer
For n = 0 To 10
lnkbVisitID = New LinkButton()
lnkbVisitID.Text = n.ToString
lnkbVisitID.ID = "lnk" & n.ToString
lnkbVisitID.CommandArgument = n
PlaceHolder1.Controls.Add(lnkbVisitID)
lnkbVisitID.CausesValidation = False
lnkbVisitID.ForeColor = Color.FromName("#ff3333")
AddHandler lnkbVisitID.Click, AddressOf link_click
Next
End Sub

When I use this code, the page refreshes but the link_click sub is never called.

Well, I hope that someone will be able to help.

Many thanks

Simon
Avatar of Justin_W
Justin_W

When you dynamically add controls to a web form, you must manually recreate that same control, with the exact same ID and position in its parent's Controls collection, during every postback.  You must also manually re-add the event handlers to them.  If you do that, the controls' other properties will be repopulated from viewstate during a special "LoadViewState" phase that occurs after the regular one and is specifically for dynamic controls.  Since the Button1_Click method is not called during the PostBack that is caused by clicking a LinkButton, the LinkButtons do not exist during that PostBack.
Avatar of simonlevey

ASKER

Thanks Justin,

How would I go about doing this?
1. When you create and add the LinkButtons to the Form, also store some info in the page's ViewState.
2. Use the info during PostBacks to rebuild and readd the controls during the Load phase.
Sounds good but I don't know how to do this. Could you give me a small coded example please?
Justin,

Had a think about this myself and got it sort of working. I used the following:

Private Sub link_click(ByVal sender As System.Object, ByVal e As EventArgs)
        TextBox1.Text = sender.commandargument
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ViewState("intTotal") = DropDownList1.SelectedIndex
        Panel1.Visible = True
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack Then
            Select Case ViewState("intTotal")
                Case 0
                    Dim n As Integer
                    For n = 0 To 10
                        lnkbVisitID = New LinkButton()
                        lnkbVisitID.Text = n.ToString
                        lnkbVisitID.ID = "lnk" & n.ToString
                        lnkbVisitID.CommandArgument = n
                        lnkbVisitID.CausesValidation = False
                        lnkbVisitID.ForeColor = Color.FromName("#ff3333")
                        AddHandler lnkbVisitID.Click, AddressOf link_click
                        PlaceHolder1.Controls.Add(lnkbVisitID)
                    Next

                Case 1
                    Dim n As Integer
                    For n = 11 To 20
                        lnkbVisitID = New LinkButton()
                        lnkbVisitID.Text = n.ToString
                        lnkbVisitID.ID = "lnk" & n.ToString
                        lnkbVisitID.CommandArgument = n
                        lnkbVisitID.CausesValidation = False
                        lnkbVisitID.ForeColor = Color.FromName("#ff3333")
                        AddHandler lnkbVisitID.Click, AddressOf link_click
                        PlaceHolder1.Controls.Add(lnkbVisitID)
                    Next
            End Select
        End If
    End Sub

This works in a way, but the user has to click on the button twice to get the link buttons generated. It seems that the page is going through postback before viewstate(intTotal) is being set. I know that I can use a case select to check the dropdown lists index, and this works however the situation with the ViewState("intTotal") variable exacly replicates a the problem that I am having with my production website.

Could you help further??

I hope so. I will of course up the points a bit.

If you want more detail, please let me know.

Thanks

Simon
ASKER CERTIFIED SOLUTION
Avatar of Justin_W
Justin_W

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Spot on.

All working now

Thanks
You're welcome.

P.S.
I thought you were going to "up the points a bit".  It's not that big a deal, but I'm trying to earn my first 10000 so I can qualify for premium services.  Since I'm almost there, every little bit helps.  Thanks.