Dynamically Adding Multiple User Controls vb.net - asp.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)

Related

How to change the text of multiple labels using a loop

I am trying to change labels in an asp.net web form. My label IDs are lbl1, lbl2, lb3, etc.
con.Open()
For i = 1 To 6
SQL = "SELECT Question FROM Question WHERE TestID=" & Session("TestID") & " AND QuestionID=" & Convert.ToString(i) & ";"
Dim cmd As New OleDbCommand(SQL, con)
Dim myControl As Control = FindControl("ContentHolder_" & "lbl" & Convert.ToString(i))
myControl.Text() = cmd.ExecuteScalar
Next
con.Close()
This obviously doesn't work as .Text is not a property of myControl.
I couldn't work out how to work around this problem.
p.s. I inspected the element of the label in google chrome and found out that it has ContentHolder_ before the name I gave in the id of the element. This is because I use a masterpage and thats why it is there.
Can anyone help?
Control is a base class of all types of webform controls. You need to use the specific type of control you are using. For example, if it is a <asp:Label> control, you need to use the Label type instead of the Control type. Like this:
Dim myControl As Label = CType(FindControl("ContentHolder_" & "lbl" &
Convert.ToString(i)), Label)
myControl.Text = "whatever"
There is a second thing wrong with your code, however. You are putting your SQL query in a loop. You should always try to reduce the number of round trips to your database. In this case, that means changing your code to query the database once, and then using an OleDbDataReader to read the data back. Your code will perform much faster that way.

Add New Item to Already Bound ListView in ASP Net (Unable to set DataKeys/FieldName)

My overall goal is to add fake/unbound items to a listview control (for final HTML Table output reasons). This is a code behind solution. Users will not be adding items as it will be outputted in a rigid table.
I have looked at several examples and while this is easy for a dropdown it is not for listview.
The code below works without error, but my item is not shown on runtime. I think the class is not setting the item fieldname correctly, but I can't figure out the right syntax to fix it.
ColumnNameAList.DataSource = PeriodDataView
ColumnNameAList.DataBind()
Dim test As New Example1("ColumnNameA")
Dim newItem As New ListViewDataItem(ColumnNameAList.Items.Count, ColumnNameAList.Items.Count)
newItem.DataItem = test
ColumnNameAList.Items.Insert(ColumnNameAList.Items.Count, newItem)
ColumnNameAList.Items.Add(newItem)
Here is the Example1 class that is supposed to set the DataValueField:
Public Class Example1
Public Sub New(ColumnNameA__1 As String)
ColumnNameA = ColumnNameA__1
End Sub
Private m_ColumnNameA As String
Public Property ColumnNameA() As String
Get
Return m_ColumnNameA
End Get
Set(value As String)
m_ColumnNameA = value
End Set
End Property
End Class
This outputs my original datasource list, but not the added item.
<ItemTemplate>
<td>
<%# Eval("ColumnNameA")%>
</td>
</ItemTemplate>
In the end I could only reliably solve this with a codebehind solution.
I made a copy of the original datasource, modified my copy and then databound to it.
Dim MyOriginalTableSource As Data.DataView = DataManager.example()
Dim ModifiedTable As DataTable = MyOriginalTableSource.ToTable
'do stuff here
Mylistbox.DataSource = ModifiedTable
Mylistbox.DataBind()
Won't work for everyone, but in this case it works fine for me.
There could be a couple of issues with the way you are approaching this, including that the ListView is already databound and that you are both adding and inserting the newItem.
When we have a scenario like this, we take one of two approaches:
1) Add the new item to the data source before the source is data bound.
2) Remove databinding and manually create each of the list view items, then add your new item at the beginning or end of the loop.
Another way to do it would be to inject it into the sql.
select col1, col2, col3 from table1 union select '1','2','3'
this would ensure that the item is always added, and asp.net doesn't need to know or care.
You can add this into the sql query or add it from the behind code before binding query. if you are not binding with sql, you can also do this to any list item with LINQ

Adding hyperlinks dynamically by code

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.

How to reference dynamic textboxes created at run time?

I have an ASP project which references a WCF service. Does exactly half of what I need.
A button on the page calls a function from the WCF, which returns a list of objects (variable names). When returned, the vb code dynamically adds textboxes to a panel on the page. Like this:
For Each LetterVariables In LetterVarList
tb = New TextBox
lb = New Label
lb.Text = LetterVariables._key & " "
tb.ID = LetterVariables._key
pnlVars.Controls.Add(lb)
pnlVars.Controls.Add(tb)
Dim LineBreak As LiteralControl = New LiteralControl("<br />")
pnlVars.Controls.Add(LineBreak)
Next
Now the problem is, after this is finished the user will enter some values into those texboxes. I (somehow) need to reference those texboxes to snag the values when a user clicks another button.
How can I do this?
Thanks,
Jason
You can give the TextBox an ID which you could use FindControl to retrieve.
tb.ID = "txt" + LetterVariables._key.ToString();
Then when you want to reference it.
TextBox txtBox = (TextBox)FindControl("txt" + someKey);
Something like that might work for you.
Don't forget to recreate the controls on post back BEFORE the controls are loaded with the posted values.

dynamically set control ID

i have few texboxt and dropdownlist, which their id would be something like "txtName1, txtName2, txtName3..." and "ddlAction1, ddlAction2, ddlAction3...."! I would to to dynamically set the textboxt and dropdownlist id into something like this:
for i as integer = 0 to 6
a = txtName+i.text
b = ddlAction+i.SelectedValue
next i
Need help from you guys to do this! thanks....
The key is FindControl, which looks up a control by its expected ID:
For i As Integer = 0 To 5
Dim txt As TextBox = TryCast(Me.Page.FindControl("txtName" & i.ToString()), TextBox)
Dim ddl As DropDownList = TryCast(Me.Page.FindControl("ddlAction" & i.ToString()), DropDownList)
If txt IsNot Nothing AndAlso ddl IsNot Nothing Then
Dim a As String = txt.Text
Dim b As String = ddl.SelectedValue
End If
Next
It will return null/nothing if a control with that ID isn't found.
Note that FindControl will only search the given control's (or Page's) immediate children, not the entire control tree. To search recursively, you need to use your own FindControl method.
Private Function FindControlRecursive(ByVal control As Control, ByVal id As String) As Control
Dim returnControl As Control = control.FindControl(id)
If returnControl Is Nothing Then
For Each child As Control In control.Controls
returnControl = child.FindControlRecursive(id)
If returnControl IsNot Nothing AndAlso returnControl.ID = id Then
Return returnControl
End If
Next
End If
Return returnControl
End Function
.Net requires you to resolve your variable names at compile time, rather than runtime like your code is trying to do. This is a good thing, as it prevents you from making certain kinds of errors. But it does mean you'll need to look at an alternative approach for this particular problem.
One option is a FindControl -based method. But odds are the controls you care about are grouped together on the page. If they aren't already, put them in a common container control, like a panel. Then you can do something like this (requires System.Linq):
For Each t As TextBox In MyContainer.Controls.OfType(Of TextBox)()
a = t.Text
Next t
For Each d As DropDownList In MyContainer.Controls.OfType(Of DropDownList)()
b = d.SelectedValue
Next d
Also, I hope you're really doing something other than assignment inside your loop. Otherwise, most of the work is for nothing as you will exit the loop having simply assigned the value from the last iteration to your variable.
Finally, it seems like these controls might work in pairs. To me, that's a situation that calls out for you to implement a user control.
I'm not a webforms expert, but I believe the page stores a reference to each control present in the page.
You can think of webforms like an n-ary tree... each control has a parent and can have 0 to many children. So, if these are static controls you should just be able to grab a reference to their parent and iterate over that... with no need for the children's ids.
Also, you can query for children based on their id... something like myControl["myID1"] so you can concat the number with the string and get the control that way.
Lastly, if these are purely dynamic controls, i.e. you don't know how many there are, just store references to them in an ordered collection and iterate over them that way.
EDIT:
Here we go:
WebControl myControl;
myControl.Controls.Add(someControlReference);
Then, to grab a control by ID:
WebControl someControl = myControl.FindControl("someControlID1");
From there you can do like:
string a = someControl.Text

Resources