Create labels dynamicly on ASP.NET (VB) - asp.net

I want to create labels in my page dynamicly, for example the user will choose in a textbox the number of labels, and I will display the number of this label with .text = "XYZ".
Thanks.

The quick and dirty method (this example adds 10 labels and literals to a PlaceHolder on an ASP.NET page:
Dim c As Integer = 0
While c < 10
Dim lab As New Label()
Dim ltr As New Literal()
lab.Text = c.ToString()
ltr.Text = "<br/>"
PlaceHolder1.Controls.Add(lab)
PlaceHolder1.Controls.Add(ltr)
C+=1
End While

Look at using a Repeater control:
Using the ASP.NET Repeater Control
Data Repeater Controls in ASP.NET

There's a number of things that will need to be done to make this work, but to simply dynamically create controls and add them to the page, you will need a Placeholder on your ASPX page:
<asp:TextBox ID="txtLabelCount" runat="server" />
<asp:Button ID="btnCreate" runat="server" Text="Create" /><br />
<asp:Placeholder ID="PlaceHolder1" runat="server" />
Then, in btnCreate's click event handler:
' Number of labels to create. txtLabelCount should be validated to ensure only integers are passed into it
Dim labelCount As Integer = txtLabelCount.Text
For i As Integer = 0 To labelCount - 1
' Create the label control and set its text attribute
Dim Label1 As New Label
Label1.Text = "XYZ"
Dim Literal1 As New Literal
Literal1.Text = "<br />"
' Add the control to the placeholder
PlaceHolder1.Controls.Add(Label1)
PlaceHolder1.Controls.Add(Literal1)
Next

Related

Reference specific asp.net control using vb.net code bhind variable

I have read through many different solutions both here on SO and other sites but this keeps eluding me.
I am trying to reference an asp.net textbox and image button from code behind vb.net.
I have tried this code but the variable tBox says it equal is nothing.
Dim callBtn As ImageButton = CType(sender, ImageButton)
Dim tBox As TextBox
Dim iButton As ImageButton
Dim cNumber As Integer = Convert.ToInt32(callBtn.ID.Substring(11))
tBox = CType(Page.FindControl("TextBox" & cNumber), TextBox)
'The line below is commented out because it did not work either but
was the first one I tried
'tBox = DirectCast(Page.FindControl("TextBox" & cNumber), TextBox)
tBox.Text = "" <--- On this line tBox is nothing
tBox.Visible = False
I have placed a break point on the tBox.Text line and tBox equals nothing.
How can I reference the text box from a variable? I also need to reference and image button as well but need to get this one to work first.
If there is a question on here that exactly answers this please point me to it as I could not find one.
Edit: Update
Here is the code that creates a textbox and image button.
<asp:ImageButton ID="ImageButton1" runat="server" Visible="false" OnClick="removeFile" ImageUrl="~/Images/red-x-md20x20.png" ImageAlign="Top" ToolTip="Click To Remove File" /> 
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Width="300px" />
It turns out that I was not looking for the right Name. Being on a content page I had to reference the master page first.
I used the following:
Dim ctrlNameT As String = "ctl00$ContentPlaceHolderRight$TextBox" & cNumber
This did the trick!

Render control as ASP not HTML

I'm in the need to take an object of WebControls type and generate the ASP.NET markup.
Dim lblTest as New Label
lblTest.ID = "lblTest"
lblTest.Text = "Text Here"
lblTest.CssClass = "myClass"
Would generate the following string.
<asp:Label id="lblTest" runat="server" CssClass="myClass" Text="Text Here" />
I'm hoping to expand this idea to numerous controls that have the flexibility to handle numerous attributes. The end result would be to take a collection of objects and to generate either a .ascx or .aspx based upon the user's input.
If you use a Placeholder, you could then add as many controls to it as you'd like.
Dim lblTest As New Label
lblTest.ID = "lblTest"
lblTest.Text = "Text Here"
lblTest.CssClass = "myClass"
PlaceHolder1.Controls.Add(lblTest)

Making an ASP.Net command button invisible or hidden with code-behind coding

We have a command button on an ASP.Net web form with a GridView that the user sends the data displayed in the GridView as an email.
On this GridView is a "Select" command button that we would like to temporaraly remove from the GridView when the user clicks on an Image Button and have the button appear again when the email has been sent.
We want to remove the button because it shows up rendered in the email which we don't want included in the email.
Can you tell me how to use coding in a code-behind file that will refresh the GridView without the button?
Here is some markup showing the button:
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button
ID="ButtonSelect"
runat="server"
CausesValidation="False"
CommandName="Select"
Text="Select Schedule Item Details" />
</ItemTemplate>
This is the coding we are using that sends out the email of the GridView:
Protected Sub ImageButtonEmailThisList_Click(sender As Object, e As ImageClickEventArgs)
' Get the rendered HTML.
'-----------------------
Dim SB As New StringBuilder()
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)
' Remove the select button for a short while.
'--------------------------------------------
GridViewSummary.RenderControl(htmlTW)
' Get the HTML into a string.
' This will be used in the body of the email report.
'---------------------------------------------------
Dim dataGridHTML As String = SB.ToString()
Dim SmtpServer As New SmtpClient()
ObjMailMessage = New MailMessage()
Try
With ObjMailMessage
.To.Add(New MailAddress(TextBoxEmailRecipient.Text))
.Subject = "Knowledge Academy Teacher's Schdule"
.Body = dataGridHTML
.IsBodyHtml = True
.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
End With
SmtpServer.Send(ObjMailMessage)
LabelEmailMessage.Text = "<i>Email sent to " & TextBoxEmailRecipient.Text & "!</i>"
ImageButtonEmailThisList.Visible = True
Catch ex As Exception
MsgBox(ex.ToString())
Finally
' Refresh the GridView with select button back in place.
'-------------------------------------------------------
End Try
End Sub
The commented sections show where we would like to add the coding to hide and show the button again.
Maybe you can hide the column :
GridViewSummary.Columns(11).Visible = False
And then :
GridViewSummary.Columns(11).Visible = True

Updating placeholder during runtime

here is the problem I am having with placeholder:
I have a repeater and within that repeater, I have an item template. Now this template is formatted with a couple of tables, but for this question I have removed them to make things easier to read:
<asp:Repeater ID="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="phAnswers" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:Repeater>
Then, on the event OnItemDataBound, I create a new placeholder, bind it to the existing on (phAnswers), however the placeholder is not updated with the radiobuttons/textboxs that are created:
Dim rdList As New RadioButtonList
Dim newRadio As New RadioButton
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim tempPH As PlaceHolder
tempPH = e.Item.FindControl("phAnswers")
For x As Integer = 0 To (t_MC.Count - 1)
newRadio = New RadioButton
newRadio.ID = "Answer" + x.ToString
newRadio.Text = t_MC(x).Value
rdList.Controls.Add(newRadio)
Next
tempPH.Controls.Add(rdList)
Any ideas why phAnswers is not updated with the new tempPH placeholder?
Cheers
OnItemDataBound may be too late to add controls. Try it in OnItemCreated and see if that helps. It's a quick test - just change your repeater event declaration like this:
OnItemCreated="R1_ItemDataBound"
If this idea doesn't help, you can easily switch it back.
Edit - I just noticed something. To populate a RadioButtonList, you should use ListItems, like this:
ListItem item - new ListItem("your text", "your value");
rdList.Items.Add(item);
This is probably why your RadioButtonList did not appear, but lone radio buttons worked.
Try using a Panel instead of a PlaceHolder

Setting LinkButton Title in ASP.Net Wizard Sidebar Template

Playing around with customizing the appearance of the Wizard control in ASP.Net, and I've found out how to disable the sidebar buttons using the SideBarTemplate and catching the OnItemDataBound event. All pretty easy. What I want to do now is to modify the text of the rendered LinkButton to prefix the step name with something like ">>" for the current step.
So, in my ItemDataBound event handler for the SideBarList, I have the following code:
Dim stepCurrent As WizardStep = e.Item.DataItem
Dim linkCurrent As LinkButton = e.Item.FindControl("SideBarButton")
If Not stepCurrent Is Nothing Then
Trace.Write("SideBar", "Current Step = " & stepCurrent.Wizard.ActiveStep.Name)
Trace.Write("Sidebar", "Link Button = " & linkCurrent.Text)
linkCurrent.Enabled = False
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
linkCurrent.Text.Insert(0, ">> ")
End If
End If
However, what I find is the trace output is showing an empty string for the lunkbutton text, but the style changes work.
Am I trying to set the text in the wrong place?
Thanks
I did not find any way to change "SideBarButton" text property that is why I added
another link button control in SelectedItemTemplate to DataList and set visible="fasle" in SideBarButton. SelectedItemTemplate will be used to render item in sidebar for current wizard step.
<ItemTemplate>
<asp:LinkButton ID="SideBarButton" runat="server"/>
</ItemTemplate>
<SelectedItemTemplate>
<asp:LinkButton ID="ActiveSideBarButton" runat="server">
<asp:LinkButton Visible="false" ID="SideBarButton"unat="server"/>
</SelectedItemTemplate>
In OnItemDataBound event do something like
Dim stepCurrent As WizardStep = e.Item.DataItem
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
Dim linkCurrent As LinkButton = e.Item.FindControl("ActiveSideBarButton")
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
LinkCurrent.Text = stepCurrent.Title;
linkCurrent.Text.Insert(0, ">> ")
End If
SideBarButton will not be rendered because of visible="false" and only ActiveSideBarButton for current step will be rendered with parameters you need.

Resources