Adding linebreak in loop - asp.net

I use the following code to add smoe fileupload controls on my page. I'd like a line break between each one, but am having trouble getting it to work.
For i As Integer = 1 To 2
Dim fileupload As FileUpload = New FileUpload
fileupload.ID = "FileUp" & i
placeUpload.Controls.Add(fileupload)
Next
Thanks

For i As Integer = 1 To 2
Dim fileupload As FileUpload = New FileUpload
fileupload.ID = "FileUp" & i
placeUpload.Controls.Add(fileupload)
placeUpload.Controls.Add( new LiteralControl("<br />") )
Next

Related

Which a better way to print in asp.net vb.net?

I want to print a gridview and a labels in header and label in footer ( after the gridview populated )
to use CrystalReports or window.print
or anything else that make my goal achived ( specialy something that easy to use )
Easiest way is to export the datagrid, download and print from Excel.
Dim dg As System.Web.UI.WebControls.DataGrid 'For example if your grid was called dg
Dim reportTitle As String = "Add A Title - Since DataGrid Doesn't Have One"
'export to excel
context.Response.Buffer = True
context.Response.ClearContent()
context.Response.ClearHeaders()
context.Response.ContentType = "application/vnd.ms-excel"
EnableViewState = True
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw) '<-- built-in Method of all System.Web.UI.Control objects
Context.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" & reportTitle & "</font></center></b>")
Context.Response.Write(tw.ToString())
Context.Response.Flush()
Context.Response.Close()
Context.Response.End()
For more info, you can check the Microsoft docs https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.control.rendercontrol

How to get the value on the textbox in server vb.net

I have created a textBox in the server side(vb.net) like this:
Dim r As New TableRow
Dim c As New TableCell
Dim txt As New TextBox
For i = 1 To 10
c = New TableCell
txt = New TextBox
txt.ID = "recev" & i
txt.ClientIDMode = UI.ClientIDMode.Static
Next
This create me all the textBox in my page.
Now the user need to enter numbers in the textBox and press on OK button.
My problem is that I don't know how to get the numbers.
If I do in the html <form> so I can do this:
For i = 1 To 10
txt = "recev" & i & ""
Request.Form(txt)
Next
and it's work. But I can't use <form> because of other reasons.
Can I use something else to get the data the user insert?
Thanks!
For example, you could use Javascript, in this way:
Event 'click' of your button: javascript:process_stuff();
Your javascript, would be something like this:
function process_stuff(){
var your_values = [];
for (var indice = 1; indice <= 10 ; indice++) {
if ($("#recev" + indice).length){
your_values[indice ] = $("#recev" + indice).val();
}
PageMethods.PM_receipt_info(your_values);
}
Server side:
<WebMethod()> _
Public Shared Sub PM_receipt_info(aValues)
for each sValue as String in aValues debug.print(sValue ) next
Where aValues is an array with the textboxes values'.
Another try: if you need a postback, can do it JS __doPostBack ('',params), passing that values sepparated with character , etc.

programmatically add dynamic AJAX controls to an ASP page

Is it possible to programmatically add dynamic AJAX controls to an ASP page?
Now I create Textbox, Listbox dynamically in Page_Init Event. I have a Hover Menu that only attaches to the first ListBox.
I need to add it to the loop that creates the other controls so that it will be available on each List box.
Here is some of my code...
Dim num As Integer = Session("lineNums") ' Determines how many text boxes need to be created based on carriage returns from textblock.
Dim MainContent As ContentPlaceHolder = CType(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Dim ph As PlaceHolder = DirectCast(MainContent.FindControl("PlaceHolder1"), PlaceHolder)
For I = 1 To num
Dim txtD As New TextBox
txtD.ID = "txtDEdit" & I.ToString("D2")
ph.Controls.Add(txtD)
Dim litCtrlD As New Literal
litCtrlD.ID = "litCtrlD" & I.ToString("D2")
litCtrlD.Text = "<br />"
ph.Controls.Add(litCtrlD)
txtD.Text = "ENGLISH"
Dim txtA As New TextBox
txtA.ID = "txtAEdit" & I.ToString("D2")
ph.Controls.Add(txtA)
Dim litCtrla As New Literal
litCtrla.ID = "litCtrla" & I.ToString("D2")
litCtrla.Text = "<br />"
ph.Controls.Add(litCtrla)
Dim txtE As New TextBox
txtE.ID = "txtEEdit" & I.ToString("D2")
ph.Controls.Add(txtE)
Dim litCtrlE As New Literal
litCtrlE.ID = "litCtrlE" & I.ToString("D2")
litCtrlE.Text = "<br />"
ph.Controls.Add(litCtrlE)
txtE.Text = "TRANSLATION"
Dim txtB As New TextBox
txtB.ID = "txtBEdit" & I.ToString("D2")
ph.Controls.Add(txtB)
AddHandler txtB.TextChanged, AddressOf txtBChanged
Dim litCtrl As New Literal
litCtrl.ID = "litCtrl" & I.ToString("D2")
ph.Controls.Add(litCtrl)
Dim lstF As New ListBox
lstF.ID = "lstFEdit" & I.ToString("D2")
ph.Controls.Add(lstF)
Dim litCtrlF As New Literal
litCtrlF.ID = "litCtrlF" & I.ToString("D2")
ph.Controls.Add(litCtrlF)
Dim lstG As New ListBox
lstG.ID = "lstGEdit" & I.ToString("D2")
ph.Controls.Add(lstG)
AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged
Dim litCtrlG As New Literal
litCtrlG.ID = "litCtrlG" & I.ToString("D2")
ph.Controls.Add(litCtrlG)
Next I
I need to add the Hover Menu somewhere in this code area.
Dim lstG As New ListBox
lstG.ID = "lstGEdit" & I.ToString("D2")
ph.Controls.Add(lstG)
AddHandler lstG.SelectedIndexChanged, AddressOf lstGChanged
Dim litCtrlG As New Literal
litCtrlG.ID = "litCtrlG" & I.ToString("D2")
ph.Controls.Add(litCtrlG)
Thanks forany help on this one.
EDIT: I have added the following code to the Page_Init Event to try and build the Hover menus but am getting an error as described below....
' ************************************
' Creating Hover
Dim hoverMenuExt As New AjaxControlToolkit.HoverMenuExtender
hoverMenuExt.ID = "hovExtEdit" & I.ToString("D2") **
ph.Controls.Add(hoverMenuExt)
Dim litHoverMenuExt As New Literal
litHoverMenuExt.ID = "litHoverMenuExt" & I.ToString("D2")
ph.Controls.Add(litHoverMenuExt)
** Line with error: Variable "hoverMenuExt.ID" is used before it has been assigned a value. A null reference exception could result at runtime.
Any ideas on this part?
FYI, this is a great link that shows how to dynamically add standard controls.
Click here.
This provides an in-depth review of the dynamic control too elaborate to detail here.

How to generate asp.net textbox accoring to the digit displays in label text?

If in the label the default text is 10 then ten textbox controls will be generated in webform ...
Can anybody did this in VB.NET ?
Look at add control.
You simply use a loop based on the counter in the label then you say myWebForm.Controls.Add(txtBox); where txtBox is:
Dim txtBox as TextBox
Sure here is some code:
Dim count as Integer
count = CType(Me.myLabel.Text, Integer)
For i as Integer=0 to count-1
txtBox = new TextBox()
txtBox.ID = "txt" & i.ToString()
myForm.Controls.Add(txtBox)
Next i

How do I use id's of HTML elements in another sub when they are created from server-side code?

I have a table that I'm creating in code behind with the final column containing a HTML checkbox with runat="server".
The code I'm using to do this is:
Do While reader.HasRows
Do While reader.Read
Dim tNewRow As New HtmlTableRow
Dim cellSKU, cellDESCR, cellDept1, cellDeptGrp1, cellDeptRng1, cellStand, cellBoard, cellSelect As New HtmlTableCell
cellSKU.InnerHtml = "<a href='edit.aspx?edit=" & reader("SKUN") & "'>" & reader("SKUN") & "</a>"
cellDESCR.InnerText = reader("DESCR")
cellDept1.InnerText = reader("DPT1")
cellDeptGrp1.InnerText = reader("GRP1")
cellDeptRng1.InnerText = reader("RNG1")
cellBoard.InnerText = reader("BOARD")
cellStand.InnerText = reader("STAND")
cellSelect.InnerHtml = "<input type='checkbox' id='selectedSKU' value='" & reader("SKUN") & "' runat='server' />"
tNewRow.Cells.Add(cellSKU)
tNewRow.Cells.Add(cellDESCR)
tNewRow.Cells.Add(cellDept1)
tNewRow.Cells.Add(cellDeptGrp1)
tNewRow.Cells.Add(cellDeptRng1)
tNewRow.Cells.Add(cellStand)
tNewRow.Cells.Add(cellBoard)
tNewRow.Cells.Add(cellSelect)
tNewRow.Style.Add("border", "solid 1px #cccccc")
skusTable.Rows.Add(tNewRow)
Loop
reader.NextResult()
Loop
But I want to use the checkbox in another sub such as:
Protected Sub editSkus_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles editSkus.Click
End Sub
Though I can't use selectedSKU.whatever as the checkbox doesn't exist on the page until the other section of code is run.
Is there a way I can get around this or another way of doing things?
Thanks in advance for any help.
It's nasty, but here goes...
Dim skusTable As New HtmlTable
Dim i As Integer
Do While reader.HasRows
Do While reader.Read
i = i + 1
Dim tNewRow As New HtmlTableRow
tNewRow.ID = "MyRow" & i
Dim cellSKU, cellDESCR, cellDept1, cellDeptGrp1, cellDeptRng1, cellStand, cellBoard, cellSelect As New HtmlTableCell
cellSKU.InnerHtml = "<a href='edit.aspx?edit=" & reader("SKUN") & "'>" & reader("SKUN") & "</a>"
cellDESCR.InnerText = reader("DESCR")
cellDept1.InnerText = reader("DPT1")
cellDeptGrp1.InnerText = reader("GRP1")
cellDeptRng1.InnerText = reader("RNG1")
cellBoard.InnerText = reader("BOARD")
cellStand.InnerText = reader("STAND")
'Create the checkbox as a webcontrol and add it to the table cell
Dim checkBox As New HtmlControls.HtmlInputCheckBox
checkBox.Value = reader("SKUN")
checkBox.ID = "selectedSKU"
cellSelect.ID = "SelectedCell"
cellSelect.Controls.Add(New WebControls.CheckBox)
tNewRow.Cells.Add(cellSKU)
tNewRow.Cells.Add(cellDESCR)
tNewRow.Cells.Add(cellDept1)
tNewRow.Cells.Add(cellDeptGrp1)
tNewRow.Cells.Add(cellDeptRng1)
tNewRow.Cells.Add(cellStand)
tNewRow.Cells.Add(cellBoard)
tNewRow.Cells.Add(cellSelect)
tNewRow.Style.Add("border", "solid 1px #cccccc")
skusTable.Rows.Add(tNewRow)
Loop
reader.NextResult()
Loop
Now all you need do is use FindControls to get to the element...
Dim myRow As HtmlControls.HtmlTableRow = skusTable.FindControl("MyRow" & i)
'Probably be good to check the object exists first....
If Not myRow Is Nothing Then
Dim myCell As HtmlControls.HtmlTableCell = myRow.FindControl("SelectedCell")
If Not myCell Is Nothing Then
Dim myCheckbox As HtmlControls.HtmlInputCheckBox = myCell.FindControl("selectedSKU")
If Not myCheckbox Is Nothing Then
'Got it! now is it selected?
Return myCheckbox.Checked
End If
End If
End If
The runat="Server" attribute is parsed by the ASP.NET process and indicates that it should create some kind of control object. At the point your code is running this parsing has already been done. ASP.NET does not parse content added to the InnerHTML property to look for additional runat attributes or any other ASP.NET specific markup, InnerHTML is expected to be strictly standard HTML that is to be sent to the server.
You could create an instance of the CheckBox ASP.NET control and add it to the cellSelect controls collection, which is the equivalent to runat="Server". However that doesn't really help because you want to bind an event to this control but on post back this control will no longer exist.
Is there a reason you are not using DataGrid to acheive this UI?

Resources