2

How can I make dynamically add link buttons to a form in asp.net(vb.net)?

Here is the code I tried:

<form id="form1" runat="server">
    <% For index As Integer = 1 To 10 %>
        <asp:LinkButton ID="#EVAL<%=index%>" runat="server"><%=index%></asp:LinkButton>
    <% Next %>
</form>

I tried dynamically using panel, but this create problem when use br tag then that will not give new line.

That code

Dim lk As LinkButton

For index As Integer = 1 To 10
    lk = New LinkButton
    lk.ID = index
    lk.Text = index
    Panel1.Controls.Add(lk)
Next
2
  • It's really not clear you're asking. Commented Sep 6, 2013 at 12:11
  • 1
    Use the "code" button to format your code instead of using ` everywhere. Commented Sep 6, 2013 at 12:12

4 Answers 4

2

Instead of trying to loop through them on the front end code, I would recommend using a panel and adding the controls to that.

First you would create your panel:

<form id="form1" runat="server">
    <ASP:Panel Runat="server" ID="Panel1" />
</form>

Then on the code behind, you would create your links dynamically and add them to that panel:

    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        Panel1.Controls.Add(lk)
    Next

If you need to wire events up to each link, you would use the AddHandler and attach the proper sub:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        AddHandler lk.Click, AddressOf DoSomething
        Panel1.Controls.Add(lk)
    Next
End Sub

Sub DoSomething(ByVal sender As Object, ByVal e As EventArgs)
    'Handle click here
End Sub

To resolve the issue with a line break, simple create a label with a <br> tag in it:

    For index As Integer = 1 To 10
        Dim lk As New LinkButton
        lk.ID = index
        lk.Text = index
        AddHandler lk.Click, AddressOf DoSomething
        Panel1.Controls.Add(lk)

        Dim lbl as new label
        lbl.text = "<br>"
        Panel1.Controls.Add(lbl)
    Next
Sign up to request clarification or add additional context in comments.

Comments

0

Are you asking how to create a <BR/> tag dynamically?

Try this:

Dim lk As LinkButton 
Dim br as HtmlGenericControl

 For index As Integer = 1 To 10
     lk = New LinkButton
     lk.ID = index
     lk.Text = index
     Panel1.Controls.Add(lk)

     br = New HtmlGenericControl("br")
     Panel1.Controls.Add(br)
 Next

This is all from memory, but that's the general idea.

ETA: BTW, I'm not sure index will work as the link button ID. I think IDs need to start with an alpha character.

Comments

0

Try this,

<script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton lnk1 = new LinkButton();
            lnk1.Text = "testtest";

            LinkButton lnk2 = new LinkButton();
            lnk2.Text = "test";
            pnl1.Controls.Add(lnk1);
            pnl1.Controls.Add(lnk2);
        }
    </script>

<asp:Panel ID="pnl1" runat="server">
    </asp:Panel>

Comments

0

This is an example of how I did it using a mix of the previous answers. Also gets data form a mysql database to populate the panel with buttons.

  • now set up the available audits for this client
  • they will be based on the id and the audits table
  • now get data from audits table

Code

    Dim myStrSql As String = "SELECT audit,user_id,total_questions FROM audits WHERE user_id = " & myGlobalId & ";"
    Dim mySet As New DataSet
    Dim myda As New MySql.Data.MySqlClient.MySqlDataAdapter
    Dim myAudit As String = ""
    Dim myTotalQuestions As Integer = 0

    openMySql()

    myda = New MySql.Data.MySqlClient.MySqlDataAdapter(myStrSql, myConnection)
    myda.Fill(mySet, "SOURCE")

    Dim i As Integer = 0

    If mySet.Tables("SOURCE").Rows.Count > 0 Then
        For i = 0 To mySet.Tables("SOURCE").Rows.Count - 1
            myAudit = IIf(IsDBNull(mySet.Tables("SOURCE").Rows(i).Item("audit").ToString), "", mySet.Tables("SOURCE").Rows(i).Item("audit").ToString)
            myTotalQuestions = IIf(IsDBNull(mySet.Tables("SOURCE").Rows(i).Item("total_questions").ToString), 0, CInt(mySet.Tables("SOURCE").Rows(i).Item("total_questions").ToString))
            'now create a button for each
            Dim btn As New Button()
            btn.ID = "btn_" & Replace(Replace(Replace(Replace(Replace(Replace(myAudit, ",", ""), ".", ""), ":", ""), ";", ""), ":", ""), "?", "")
            btn.Text = myAudit & " : " & myTotalQuestions & " questions."
            AddHandler btn.Click, AddressOf MenuButtonClick
            Panel1.Controls.Add(btn)
            'this puts in a new line in the panel
            Dim lbl As New Label
            lbl.Text = "<br>"
            Panel1.Controls.Add(lbl)
        Next

    End If
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.