Adding hyperlinks dynamically by code - asp.net

I have a web form but I have to do this by code since I dont know the number of hyperlinks I need from the beginning.
How can I add some hyperlinks with Image in a label, the number of hyperlink depends on the number of rows of a query, and each row give me the link information to navigate.
Thanks in advance.

As you loop through your data you can manually add a link to the row something like this:
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
row.Cells.Add(New HtmlTableCell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
That of course adds the link as the first cell in an html table. Not sure how you plan to display your data.
In response to the comment below. You can instead insert the new cell something like this
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
Dim cell As New HtmlTableCell
row.Cells.Insert(1, cell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
You could also simply add the control to the existing cell that the label is in instead of making a new cell. You can do that by the index value of your existing cell (starting at 0 for each cell that is in the row)

This question is similar to what you want to do:
Auto increment asp control ID
Two options either use a repeater or dynamically add the controls to a panel or some other container control.

Related

Creating ASP.NET table header row

I don't know why this isn't working. I'm trying to create a table header section from the back end code, but everything is going into tbody.
Dim output As New Web.UI.WebControls.Table
'Create the header row
Dim hRow As New Web.UI.WebControls.TableHeaderRow
hRow.TableSection = Web.UI.WebControls.TableRowSection.TableHeader
hRow.Controls.Add(New Web.UI.WebControls.TableHeaderCell)
For Each d As GridDate In Dates
Dim hCell As New Web.UI.WebControls.TableHeaderCell
hCell.Text = d.Value
hRow.Controls.Add(hCell)
Next
output.Controls.Add(hRow)
The result is everything under tbody despite creating a header row and setting the section property to header. What am I doing wrong?
There was an error in the code I posted. In the last line of my code I was appending the new row to the controls collection:
output.Controls.Add(hRow)
Don't do this. It seems to bypass some of the properties unique to ASP.NET TableRows in final rending. In this case, it was ignoring the TableSection property despite being set correctly. You should append rows to the Rows collection instead:
output.Rows.Add(hRow)
Try this
Dim output As New Table
Dim hRow As New TableHeaderRow
For Each d As GridDate In Dates
Dim hCell As New TableHeaderCell
hCell.Text = d.Value
hCell.Scope = TableHeaderScope.Column
hRow.Cells.Add(hCell)
Next
output.Rows.Add(hRow)
This worked for me

Dynamically Adding Multiple User Controls vb.net

I have a User Control which returns a table of data, which in some cases needs to be looped, displaying one on top of another.
I am able to dynamically add a single instance of this by placing a fixed placeholder in the page.
I am now trying to work out how to add more than one, given that I don't know how many might be needed I don't want to hard code the Placeholders.
I've tried the following, but I am just getting one instance, presumably the first is being overwritten by the second
My HTML
<div id="showHere" runt="server"/>
VB
Dim thisPh As New PlaceHolder
thisPh.Controls.Add(showTable)
showHere.Controls.Add(thisPh)
Dim anotherPh As New PlaceHolder
anotherPh .Controls.Add(showTable)
showHere.Controls.Add(anotherPh)
How do I make it add repeated tables within the showHere div?
I would advise generating a different ID for each of your table. For example,
Dim i As Integer
i = 0
For each tbl in ShowTables
tbl.ID = "MyTab" + i.ToString()
i = i + 1
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
On other hand, it would make more sense to have a your user/custom control generate html for a single table and then nest your user/custom control within a repeater (or similar control such as ListView etc).
Did you tried simply, this:
For each tbl in ShowTables
showHere.Controls.Add(tbl)
showHere.Controls.Add(New LiteralControl("<br />"))
Next
After fussing with this issue myself, I stumbled across below solution.
On button click()
LocationDiv.Visible = True
Dim existingItems As New List(Of Object)
If Not Session("existingItems") Is Nothing Then
existingItems = CType(Session("existingItems"), List(Of Object))
For Each item As Object In existingItems
LocationDiv.Controls.Add(item)
Next
existingItems.Clear()
End If
LocationDiv.Controls.Add(New LiteralControl("<b>" & Text & "</b>"))
For Each item As Object In LocationDiv.Controls
existingItems.Add(item)
Next
Session.Add("existingItems", existingItems)

ASP Table doesnt save state

I created an ASP Table Control and added a row to it programatically. I then repeated the process to add another row. The first row that I added disappeared and only the newest row is being displayed. Is there a property that needs to be set to allow the state of the table to be saved?
EDIT:
All I do is click a button on the page and it adds a row to the table. Repeated clicks of the button only add one from to the table. If it helps, here is the function that adds the rows.
Protected Sub addItemButton(sender As Object, e As EventArgs)
'get the id of the item they selected
Dim id As String = CType(sender, Button).Text
'clear out the data being displayed
gridViewItemSearchItems.DataSource = Nothing
gridViewItemSearchItems.DataBind()
'add this item to the line table
Dim itemToAdd As Item = gp.Items.GetItemByKey(id)
Dim tableRow As New System.Web.UI.WebControls.TableRow()
Dim cellId As New System.Web.UI.WebControls.TableCell
cellId.Text = itemToAdd.Key.Id
Dim cellDesc As New System.Web.UI.WebControls.TableCell
cellDesc.Text = itemToAdd.Description
Dim cellMSRP As New System.Web.UI.WebControls.TableCell
cellMSRP.Text = gp.Items.GetItemMSRP(itemToAdd.Key)
Dim cellCost As New System.Web.UI.WebControls.TableCell
cellCost.Text = itemToAdd.CurrentCost.Value
Dim cellUnitPrice As New System.Web.UI.WebControls.TableCell
cellUnitPrice.Text = itemToAdd.StandardCost.Value
Dim cellDiscount As New System.Web.UI.WebControls.TableCell
cellDiscount.Text = "12%"
Dim cellQuantity As New System.Web.UI.WebControls.TableCell
cellQuantity.Text = "1"
tableRow.Cells.Add(cellId)
tableRow.Cells.Add(cellDesc)
tableRow.Cells.Add(cellMSRP)
tableRow.Cells.Add(cellCost)
tableRow.Cells.Add(cellUnitPrice)
tableRow.Cells.Add(cellDiscount)
tableRow.Cells.Add(cellQuantity)
tblItems.Rows.Add(tableRow)
End Sub
Since you're adding the rows to your table dynamically, they have to be re-created on each successive post-back.
Look at this answer for an explanation as to why dynamically added controls can disappear: ASP.NET dynamically created controls and Postback
EDIT: It is possible to dynamically add controls during a post-back, my answer here shows a way to do that using ViewState. Although, this does become very cumbersome. You might want to re-think the design of the page.

Creating dynamic RadiobuttonList

What im trying to accomplish is I have a table which im creating dynamically and I want in the first tablecell a radiobutton, second tablecell first name, third tablecell last name. The second and third tablecell work fine but having trouble with the radiobutton. Im making it a radiobutton list b/c only one name should be selectable. Below is how im creating the radiobutton list. I have omitted the second and third tablecell as its working properly. The yesNo variable is used to say is this the first row in the table. Any help would be very much appreciated.
tblrow = New TableHeaderRow
tblcell = New TableCell
If yesNo = "yes" Then
radList = New RadioButtonList
radList.ID = newVar & "_list"
Else
Dim item As New ListItem
radList.Items.Add(item)
End If
tableName.Rows.Add(tblrow)
Looks like you are creating a new RadioButtonList with one button in it for every row.
Try using instead the HtmlInputRadioButton class. Here is a nice description of it and it's use.
Here's the relevant bit:
HtmlInputRadioButton controls can be grouped together by specifying a common value for the Name property of each radio button you want to include in the group.
Note When you group HtmlInputRadioButton controls together, only one radio button in the group can be selected at a time.

How can I copy object types in VB.NET?

I am building an ASP.NET application that needs dynamic tables. That's another issue that I've already posted about (and gotten a pretty good response!). Now, I'm running into another issue - I want to add new rows to my table, but given that I will have 10-12 tables on one page, each containing different objects in their rows (text boxes, check boxes, etc.) I need a way of simply generically adding a new row that has the same objects as the first row in the table. Here's my code:
Private Sub AddTableRow(ByRef originalTable As System.Web.UI.WebControls.Table)
Dim originalRow As System.Web.UI.WebControls.TableRow = originalTable.Rows(1)
Dim insertingRow As New System.Web.UI.WebControls.TableRow
Dim insertingCells(originalRow.Cells.Count) As System.Web.UI.WebControls.TableCell
Dim index As Integer = 0
For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells
insertingCells(index) = New System.Web.UI.WebControls.TableCell
insertingCells(index).Controls.Add(cell.Controls.Item(0))
index += 1
Next
insertingRow.Cells.AddRange(insertingCells)
originalTable.Rows.Add(insertingRow)
End Sub
But I'm getting a null reference exception at the second to last line,
insertingRow.Cells.AddRange(insertingCells)
...and I can't figure out why. Is it because the contents of each cell are not being initialized with a new object? If so, how would I get around this?
Thanks!
EDIT:
The inside of my for loop now looks like this -
For Each cell As System.Web.UI.WebControls.TableCell In originalRow.Cells
Dim addedContent As New Object
Dim underlyingType As Type = cell.Controls.Item(0).GetType
addedContent = Convert.ChangeType(cell.Controls.Item(0), underlyingType)
insertingCells(index) = New System.Web.UI.WebControls.TableCell
insertingCells(index).Controls.Add(addedContent)
index += 1
Next
Stepping through with a debugger, I see that this strategy is working - but the additional table row still doesn't appear...and still does when I do this statically.
I think your culprit may be this line:
Dim insertingCells(originalRow.Cells.Count) As TableCell
Confusingly, the number you specify in an array declaration in VB.NET is the upper bound, not the number of elements. So Dim ints(10) As Integer will create an Integer() array with eleven elements, not ten (10 will be the highest index of the array).
Try this instead:
Dim insertingCells(originalRow.Cells.Count - 1) As TableCell

Resources