html get elements dropdownlist vbnet - asp.net

i am trying to collect all the items of an html dropdownlist and put them in my combobx in vbet but i am getting an error that index was out of range.I have checked the id of the dropdownlist but it is correct.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim I As Integer = 0
Dim NAME As String
NAME = WebBrowser1.document.GetElementById("ID").GetAttribute("pt1:r1:0:soc2::content").GetAttribute("value").ElementAt(I)
ComboBox1.Items.Add(NAME)
MessageBox.Show(NAME)
End Sub
Any help would be appreciated thanks.

i wrote this code now and everything works perfect.
hope it helps:
For Each elem In WebBrowser1.Document.GetElementById("pt1:r1:0:soc2::content").Children
ComboBox1.Items.Add(WebBrowser1.Document.Body.AppendChild(elem).InnerText)
MessageBox.Show(WebBrowser1.Document.Body.AppendChild(elem).InnerText)
Next
Jonathan

Related

VB.NET Webform DetailsView - Loop Through Updating field names

I have this code below in VB.NET and it works fine, which is looping through updating input elements clean it with HtmlEncode
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles data_DetailsView1.ItemUpdating
For i As Integer = 0 To e.NewValues.Count - 1
If e.NewValues(i) IsNot Nothing Then
e.NewValues(i) = Server.HtmlEncode(e.NewValues(i).ToString())
End If
Next
End Sub
The above code loop through every inputs in the DetailsView. I want to skip two inputs (textboxes) name "Desc1" and "Desc2". I couldn't figure out how to get the input name from the element. Please help me correct the below code:
Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles data_DetailsView1.ItemUpdating
For i As Integer = 0 To e.NewValues.Count - 1
If e.NewValues(i) IsNot Nothing Then
If e.Name <> "Desc1" OR e.Name <> "Desc2" Then 'this is not working, but just the pseudo code.
e.NewValues(i) = Server.HtmlEncode(e.NewValues(i).ToString())
End If
End If
Next
End Sub
Thanks in advance,

VB.NET display label text only on default.aspx page only

Have a message that currently displays on every page of our website. I'd like to change this so that it only appears on the homepage which is default.aspx. Please let me know what I can try. the below code lives in the code behind of the master page of the site. We use vb.net but if someone can write it in C# I can convert it.
Previous code that displays label text on every page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End Using
End Sub
Code I have written that isn't working, what am I missing?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using ds As DataSet = SystemDataObject.SystemDataGetMessage()
Dim appPath As String = Request.PhysicalApplicationPath
If appPath = "/Default.aspx" Then
lblSystemMessage.Text = ds.Tables(0).Rows(0)("SystemMessage_sd").ToString
End If
End Using
End Sub
Ended up wrapping the control in a panel with ID of Message and using the following code to display only if on the homepage.
If Page.Title = "Home" Then
Message.Visible = True
Else
Message.Visible = False
End If

creating event control in vb.net. Multiple controls with the same ID '1' were found

Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class

Why is this insert code inserting data twice on button clicks?

The following code inserts same data two time in database table, but I want it to insert only one item when button is clicked.
What is the problem in this code?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If RadUpload1.UploadedFiles.Count >= 0 Then
Dim file As UploadedFile = RadUpload1.UploadedFiles(0)
SqlDataSource1.InsertParameters("photo").DefaultValue = "./upload/" & file.GetName()
'SqlDataSource1.InsertParameters("name").DefaultValue = TextBox1.Text
SqlDataSource1.Insert()
ListView1.DataBind()
End If
'UpdateProgressContext()
End Sub
You must be using both OnClick="Button1_Click" in your markup and Handles Button1.Click in your procedure.
Try removing one of them.

VB.NET CheckboxList doubles when button is clicked. Why?

Every time I click the Delete Button, the check marks double in quantity:
Public Class About
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
For count As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(count).Selected Then
File.Delete("C:\Images\" & CheckBoxList1.Items(count).ToString)
End If
Next
End Sub
End Class
The checkboxlist isn't refreshed so that the checkbox that I deleted is removed from the checkboxlist. How do I accomplish that? Thanks!
On a button click, Page_Load is ran again, so the code that adds the checkboxes is ran a second time.
Add a check for Page.IsPostBack, and only add the checkboxes if it is not a postback.
If Not Page.IsPostBack Then
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If
(I hope syntax is right... Not used to VB)
the whole content of the Page_Load event handler has to be executed only once in your case, so rewrite it like this:
If Not IsPostBack Then
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If

Resources