VBScript to add and delete row dynamcially - asp.net

I would like to add a new row to my HTML table and delete the row dynamically using VBScript. Iam new to this can anyone guide me how to do this.

Note that this will be Internet Explorer specific. Might not work on other browsers/
Sub AddRow()
Dim objTable : Set objTable = window.document.getElementById("tableid")
Dim objRow : Set objRow = objTable.insertRow()
Dim intCount, objCell
For intCount = 0 To 2
Set objCell = objRow.insertCell()
objCell.innerHTML = "Content for cell")
Next
End Sub
For delete use
window.document.getElementById("tableid").deleteRow(oRow.rowIndex);

Related

How to find the value of a dropdownlist using FindControl?

I am working on a website with VB.NET and ASP.NET. I currently have recurring DropDownLists for the user to provide input.
The design is recurring. These DropDownLists get their values from a database table, Everything with the Web interface is working except for writing these recurring values to the database - that is just to give you some background.
I have set the ID's of each DropDownList like so:
FrequencyList.ID = String.Concat("FreqList", DBReader(0))
That is in a loop while reading the DatabaseReader.
This is what I'm having issues with (please note I simplified the code down to make it easier to read:
Dim i As Integer
DBCommand = New SqlCommand()
DBCommand.Connection = DBConnection
DBCommand.CommandType = Data.CommandType.StoredProcedure
DBCommand.CommandText = "StoredProcedureName"
DBConnection.Open()
For i = 1 To AspectTableLength
Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("#nFrequencyID", SqlDbType.Int)
ParamFrequencyID.Value = FindControl("FreqList" & Convert.ToString(i))
ParamFrequencyID.Direction = ParameterDirection.Input
Next
The FindControl("FreqList" & Convert.ToString(i)) variable is incorrect because it does not access the value - and adding .SelectedItem.Value does not work.
I got help from a developer.
Dim MyControls As ControlCollection = Panel.Controls
Dim Number As Integer 'this is the same as "DBReader(0)"
For Each MyControl As Control In MyControls
If MyControl.ID Is Nothing Then
Else
If MyControl.ID.StartsWith("Span") Then
Number = Replace(MyControl.ID, "Span", "")
Dim Freq As DropDownList = PanelMain.FindControl(“FreqList” & Number)
Dim ParamFrequencyID As SqlParameter = DBCommand.Parameters.Add("#nFrequencyID", SqlDbType.Int)
ParamFrequencyID.Value = Freq.SelectedIndex
ParamFrequencyID.Direction = ParameterDirection.Input
DBCommand.ExecuteNonQuery()
DBCommand.Parameters.Clear()
End If
End If
Next
DBConnection.Close()

Sorting a datatable

I have an asp.net listbox on my page, that I want to sort.
E.g: listbox(lstChooseFields)
MY VB code:
Private Sub LoadColumns()
If drpScreen.SelectedIndex > -1 Then
Dim daLookup As New LookupTableAdapters.LookupTableAdapter
Dim dtLookup As New System.Data.DataTable
dtLookup = daLookup.GetNestedControlValues("EM", "ExcelExport.aspx", "drpScreen", "drpScreen", drpScreen.SelectedValue)
lstChooseFields.DataSource = dtLookup
lstChooseFields.DataValueField = "LKP_ControlValue"
lstChooseFields.DataTextField = "LKP_ControlText"
lstChooseFields.DataBind()
'Dispose
daLookup.Dispose()
End If
End Sub
I have done some searching and found something like 'dtLookup.DefaultView.Sort = "LKP_ControlText" that I can use but it does not work. I don't want to sort in my database only on this page, this listbox (lstChooseFields).
So if you want to sort for the datatable then you have tried dtLookup.DefaultView.Sort = "LKP_ControlText" but you have missed out one piece of word there dtLookup.DefaultView.Sort = "LKP_ControlText asc". This generally works if LKP_ControlText is a column name in the datatable
You must use the word ascthere. which i couldnt find in your question.
After getting the dataTable sorted you can copy the same structure to another datatable if u want by using
Dim dtDuplicateLookup As New DataTable()
dtDuplicateLookup = dtLookup.DefaultView.ToTable(True)
and then access dtDuplicateLookup for further use if you want to retain the previous datatable as it is.
Use the .sorted property for the list box as shown here.
lstChooseFields.Sorted = True
UPDATE: your method was right but just tune it this way. I think you missed the DESC part. The second expression tells it how to sort the dataview.
Dim dataView As New DataView(table)
dataView.Sort = " column_name DESC" //DESC or ASC as per requirement
Dim dataTable AS DataTable = dataView.ToTable()
Else try this. After sorting a defaultview, generally you have to loopback through it
foreach(DataRowView r in table.DefaultView)
{
//... here you get the rows in sorted order
//insert the listbox items one by one here using listbox.add or listbox.insert commands
}

Radcombobox inside Radgrid FormTemplate

G'day,
I have a RadComboBox control inside of a RadGrid that is displayed when the InitInsert action occurs. I'm using Entity Framework as a datasource & the results contained within this are correct. My problem is that when I use findcontrol it returns nothing.
If e.CommandName = "InitInsert" Then
RadGrid1.MasterTableView.InsertItemDisplay = Telerik.Web.UI.GridInsertItemDisplay.Bottom
Dim query = From myTable In dbEntity.myTables Select myTable.Name, myTable.ID
Dim mineCompBox = CType(e.Item.FindControl("mineCompBox"), RadComboBox)
mineCompBox.DataSource = mineCompQuery
mineCompRadBox.DataTextField = "Name"
mineCompRadBox.DataValueField = "Id"
mineCompRadBox.DataBind()</code>
I'm having trouble finding any answers that reference FormTemplate without it being an edit form. What am I missing? :-(
Thanks.
I don't have my computer in front of me to test it but I'm pretty sure that either:
1) You are not looking at the right controls collection
2) or the RadComboBox is not create yet or has been created but the ID doesn't match
so you cannot find it
Again not 100% sure. Maybe this can help you its a complete example: http://beecy.net/post/2009/01/07/telerik-radgrid-formtemplate-codebehind.aspx (maybe check you markup against this one)
My problem was solved by using an ItemCreated command. An example can be found here:
http://www.telerik.com/community/forums/aspnet-ajax/grid/find-controls-when-using-editcommand.aspx
The code for my situation was:
Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
Dim dbEntity As WebsiteEntities = New WebsiteEntities
Dim myQuery = From myTable In myTables Select myTable.Name, myTable.ID
Dim EditFormItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
Dim myCombobox As RadComboBox = DirectCast(EditFormItem.FindControl("radDropBox"), RadComboBox)
myCombobox.DataSource = myQuery
myCombobox.DataTextField = "Name"
myCombobox.DataValueField = "ID"
myCombobox.DataBind()
End If
End Sub

Can't find file

I'm working on a custom menu system in asp.net that populates a horizontal menu on the fly based on which menu item is selected from the website's main menu.
This 2nd menu is populated from a custom XML file in the website's root directory.
(See http://loganyoung.wordpress.com/2010/06/03/asp-net-horizontal-submenu-from-xml/ for details).
At the time I'd written that post, it did work, but my development environment has changed and now I'm getting an error saying that the XML file can't be found.
Here's my code:
Imports System.Xml
Partial Class Site
Inherits System.Web.UI.MasterPage
Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick
Select Case e.Item.Value.ToString
Case "Team"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/TeamMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
Case "Investments"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
Case "Social Responsibility"
Dim doc As New XmlDocument
doc.Load("~/Submenus.xml")
Dim NameNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/name")
Dim URLNodes As XmlNodeList = doc.SelectNodes("/InvestmentsMenu/item/url")
If NameNodes.Count = URLNodes.Count Then
For i As Integer = 0 To NameNodes.Count - 1
Dim m As New MenuItem
m.Text = NameNodes.Item(i).FirstChild.InnerText
m.NavigateUrl = URLNodes.Item(i).FirstChild.InnerText
Menu2.Items.Add(m)
Next
End If
End Select
End Sub
End Class
And here's the error I'm getting:
Could not find a part of the path 'c:\windows\system32\inetsrc\~\Submenus.xml'.
Menu2 is just a completely empty <asp:Menu> control directly under the main menu on the page.
Can someone tell me what I'm doing wrong please?
Thanks in advance.
XmlDocument.Load is expecting a file path where you are providing a virtual path. Try changing it to this:
doc.Load(Page.MapPath("~/Submenus.xml"))
If you use doc.Load("~/Submenus.xml") this xml must be in your project. Otherwise you have to use server.mappath.

Grouping rows in ASP.Net ListView

I am new to ASP.NET. I am trying to display my sql results using a list view. I am using the example for grouping my results by a data field from the 4GuysFromRolla.com website. However, I find the way of grouping the items by a data field to be a bit clumsy. Is there a better way to do it?
Thanks.
Nested ListView - http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
I have never used a ListView, but I did do grouping in a GridView. You can try porting this over to a ListView if you want:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim tblGrid As Table = Me.GridView1.Controls(0)
Dim strLastCat As String = "#"
Dim row As GridViewRow
For Each row In GridView1.Rows
Dim intRealIndex As Integer = tblGrid.Rows.GetRowIndex(row)
Dim strCat As String = Me.GridView1.DataKeys(row.RowIndex).Value
If strLastCat <> strCat Then
Dim rowHeader As New GridViewRow(intRealIndex, intRealIndex, DataControlRowType.Separator, DataControlRowState.Normal)
Dim newCell As New TableCell
newCell.ColumnSpan = Me.GridView1.Columns.Count
newCell.BackColor = System.Drawing.Color.FromArgb(61, 138, 20)
newCell.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255)
newCell.Font.Bold = True
newCell.Font.Size = New FontUnit(FontSize.Larger)
newCell.Text = strCat
rowHeader.Cells.Add(newCell)
tblGrid.Controls.AddAt(intRealIndex, rowHeader)
strLastCat = strCat
End If
Next
MyBase.Render(writer)
End Sub
The code creates headers each category. The final version can be viewed here: http://www.truedietreviews.com/diet-reviews/

Resources