get html element by id vb.net - html

I am trying to change the background color of a div when clicked. The div is inside a link button which is the sender in this function:
Public Sub ClickDiv(sender As Object, e As EventArgs)
Dim lnkbtn As LinkButton = TryCast(sender, LinkButton)
End sub
How can i get the div inside of this link button (the div has command argument as id). What I've tried so far (i am getting div=Nothing):
Dim div As HtmlControl = TryCast(lnkbtn.FindControl(lnkbtn.CommandArgument), HtmlControl)
or
Dim div As HtmlControl = TryCast(Page.FindControl(lnkbtn.CommandArgument), HtmlControl)
HTML in source code:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$ctl21','')" style="text-decoration:none;">
<div id="x11" class="col-md-1" runat="server">11</div>
</a>
Creation of Link button and div:
Dim div As HtmlGenericControl = New HtmlGenericControl()
div.Attributes.Add("id", "y" & i)
div.Attributes.Add("class", "col-md-2")
div.Attributes.Add("runat", "server")
Dim lnkbtn As LinkButton = New LinkButton
AddHandler lnkbtn.Click, AddressOf Me.ClickDiv
lnkbtn.CommandArgument = "y" & i
lnkbtn.Controls.Add(div)
divrepeater.Controls.Add(lnkbtn)

Related

Read multiple web classes, add them to listview

Hey I have following problem
I need to get specific values from website and more than one,
here's an example of website code
<div class="content">
<div class="all-items">
<div class="item1">
Example Item
</div>
<div class="itemsize">
" 103 "
<span> cm <span>
</div>
there more "item1" classes under the first with same name which I need to add in listview
until there is no "item1" class more.
I tried following but its not throwing an error or anything...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("className") = "content" Then
For Each Element0 As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element0.GetAttribute("className") = "all-items" Then
For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
If Element1.GetAttribute("className") = "item" Then
For Each Element2 As HtmlElement In Element.GetElementsByTagName("a")
If Element2.GetAttribute("className") = "href" Then
Dim vLink As String = Element2.InnerText
For Each Element3 As HtmlElement In Element.GetElementsByTagName("a")
If Element3.GetAttribute("className") = "Example Item" Then
Dim vTitle As String = Element3.InnerText
For Each Element4 As HtmlElement In Element.GetElementsByTagName("div")
If Element4.GetAttribute("className") = "itemsize" Then
Dim vSize As String = Element4.InnerText
For Each Element5 As HtmlElement In Element.GetElementsByTagName("span")
If Element5.GetAttribute("className") = "span" Then
Dim vUnit As String = Element5.InnerText
With lvList.Items.Add(vTitle)
.SubItems(0).Text = (vLink)
.SubItems(1).Text = (vSize + " " + vUnit)
End With
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End If
Next
End Sub
its messy af but I should work theoretically except the size one
and I'm not sure to if I get all "item1" classes with that
I have literally no more idea at this point especially I failing
to retrieve just one value.
any suggestions or help?

changing css background from sql database of particular div

I have a div where it's property set runat="server" with its ID. I'm trying to put dynamic background image for that particular div from MySQL database where it's path url mentioned in row. Now all working fine but I don't know how to give style affect to that particular div only. Currently I defined only on div which results to get background image in all div on that page. How can I define that div class or ID in my code?
Private Sub coverContent_Init(sender As Object, e As EventArgs) Handles coverContent.Init
Try
Dim css As New HtmlGenericControl()
css.TagName = "style"
css.Attributes.Add("type", "text/css")
Dim imageURL As String = String.Empty
Dim var3 As String
var3 = Request.QueryString("hospitalID")
Dim str As String = "Select hospitalID, coverImage from hospitals where hospitalID='" + var3 + "';"
con.Open()
Dim cmd As New MySqlCommand(str, con)
Dim da As New MySqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
con.Close()
If dt.Rows.Count > 0 Then
generalID.Text = dt.Rows(0)("hospitalID").ToString
imageURL = dt.Rows(0)("coverImage").ToString
End If
con.Close()
css.InnerHtml = (Convert.ToString("div{background-image: url(") & imageURL) + ");}"
Page.Header.Controls.Add(css)
MyBase.OnInit(e)
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
Very simple where you have mentioned div there you need to specify class of div where you want to show background image. Like below
css.InnerHtml = (Convert.ToString(".divClassName{background-image: url(") & imageURL) + ");}"

VB.NET/GetElementByClass how to div class click?

</div></div></div><div class="f u" id="m_more_item"><span>Diğerlerini Gör</span></div></div></div></div></div></div></div></body></html>
DOCUMENT
CODE:
Dim h1 As HtmlElementCollection = Nothing
h1 = W.Document.GetElementsByTagName("div")
For Each curElement As HtmlElement In h1
If InStr(curElement.GetAttribute("classname").ToString, "f u") Then
curElement.InvokeMember("Click")
BUT code does not work HELP ME ?
Off my Example: VB.Net - select a class using GetElementByClass and then click the item programmatically
The problem is your trying to click the DIV instead of trying to click the A HREF tag. You will want to do that instead.
Since there is no "class" or anything on that element, then you could do something like...
Dim h1 As HtmlElementCollection = Nothing
h1 = W.Document.GetElementsByTagName("div")
For Each h1Element as HtmlElement in h1
Dim NameStr As String = h1Element.GetAttribute("href")
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
h1Element.InvokeMember("Click")
Dim theElementCollection As HtmlElementCollection = Nothing
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("id") = "m_more_item" And Element.OuterHtml.Contains("Diğerlerini Gör") Then
Element.InvokeMember("click")
End If
Next

Find generated HTML Control and assign value to it

Hello i generate HTML input text controls with a string builder and put it inside a div from Code Behind.
Then i also need to assign values to these generated input & update database if values change by the user.
The problem is Code Behind can't find the generated from String builder HTML Input-text Controls
You can see code example below:
Public Class WebForm1
Inherits System.Web.UI.Page
Private Sub WebForm1_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Dim strB As New StringBuilder
For i = 0 To 5
strB.Append("<input type=""text"" value="""" runat=""server"" id=position_" & i & "/>")
Next
wraper.InnerHtml = strB.ToString
strB.Clear()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i = 0 To 5
Dim a As HtmlInputText
a = Me.Page.FindControl("position_" & i)
a.Value = "test"
Next
End Sub
End Class
That's because you're not creating controls, you're creating strings without a context.
You might want to look at these questions:
ASP: runat=server for dynamic control
Dynamically Created Controls losing data after postback

ListBox with html element

Can anyone offer me some advice? I currently have a listbox I am using, in the listbox there is a list of images from any website. they are grabbed from the website via this method
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
For Each CurElement As HtmlElement In PageElements
imagestxt.Items.Add(imagestxt.Text & CurElement.GetAttribute("src") & Environment.NewLine)
Next
Timer1.Enabled = True
End Sub
I then use the picture control method to get the image and display it.
pic1.Image = New Bitmap(New MemoryStream(New WebClient().DownloadData(imagestxtimagestxt.SelectedItem.ToString))).SelectedItem.ToString)))
This method pulls the images and title from the HTML.
Private Function StrHTML12() As Boolean
Dim htmlDocument As HtmlDocument = WebBrowser1.Document
ListBox1.Items.Clear()
For Each element As HtmlElement In htmlDocument.All
ListBox1.Items.Add(element.TagName)
If element.TagName.ToUpper = "IMG" Then
imgtags.Items.Add(element.OuterHtml.ToString)
End If
If element.TagName.ToUpper = "TITLE" Then
titletags.Items.Add(element.OuterHtml.ToString)
Timer1.Enabled = False
End If
Next
End Function
This is a counting method to count how many empty alt="" or empty img alt='' there are on the page.
Basically what i am looking to do is;
Have a program that can check the image, look at the alt='' or img alt='' if on the website the dev hasn't put anything in the alt tag i want the image to show in a picture box and i want the alt tag either next to it or underneith it or something. but i have no idea how.
counter = InStr(counter + 1, strHTML, "<img alt=''")
counter = InStr(counter + 1, strHTML, "alt=''")
counter = InStr(counter + 1, strHTML, "alt=""")
The above seems really slow and messy. is there a better way of doing it?
I do not have VB installed so I have not been able to test the code. I'm also not familiar with the datagridview component so have not attempted to integrate my code with it.
The code below should get you the title of the page, and loop through all the img tags that do not have (or have empty) alt-text
HtmlElement.GetAttribute(sAttr) returns the value of the attribute or an empty string.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim Title As String
Dim ImSrc As String
Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
// This line might need to be adjusted, see below
Title = PageElements.GetElementsByTagName("title")(0).InnerText
For Each CurElement As HtmlElement In PageElements
If CurElement.GetAttribute("alt") = "" Then
// CurElement does not have alt-text
ImSrc = CurElement.GetAttribute("src") // This Image has no Alt Text
Else
// CurElement has alt-text
End If
Next
Timer1.Enabled = True
End Sub
The line that gets the title might need to be changed as I'm unsure how collections can be accessed. You want the first (hopefully only) element returned from the GetElementsByTagName function.