Reputation: 98
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
Upvotes: 2
Views: 9872
Reputation: 1
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.
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
Upvotes: 0
Reputation: 778
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>
Upvotes: 0
Reputation: 1857
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
Upvotes: 2
Reputation: 13965
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.
Upvotes: 0