get element id with vb - html

I have the following code in an aspx page:
<div id="objectList" style="overflow: auto; width:100px; display:block;
position:absolute;top:0px;left:0px;z-index:100;">
<div id="object8" class="object" title="">
<br>object8</div>
<div id="object2" class="objectSelect" title="">
<br>object2</div>
</div>
I am attempting to find the ID of the object that is selected, in this case object2. I am trying to do it in the codebehind with vb.net but I'm not sure how. Any help would be appreciated.

Add runat="server" to all of the <div> elements you wish to find out if they are selected or not, like this:
<div id="object8" class="object" title="" runat="server">
<div id="object2" class="objectSelect" title="" runat="server">
Now in code-behind you can loop through all of the <div> elements in the page and check the class attribute value, like this:
For Each item As Control In Me.Controls
' We have to look at all HtmlGenericControl, because
' there is no .NET control type for DIV
Dim theDiv As System.Web.UI.HtmlControls.HtmlGenericControl = TryCast(item, System.Web.UI.HtmlControls.HtmlGenericControl)
' Make sure the cast worked before we try to use the DIV
If theDiv IsNot Nothing Then
' Is the class name equal to objectSelect?
If theDiv.Attributes("class") = "objectSelect" Then
' Yes, this DIV is selected, do something here
End If
End If
Next

Related

How to get the Text of a Div Class and Place inside a Label?

I'm trying to get the "valor" that is inside the site div from Poloniex to put inside a Label every 1 second using Timer after load site completely.
My Completely Code:
https://pastebin.com/QrcuWnxf
Div:
<div class="info">valor</div>
I have found that on the page there are several "info" classes...
This code worked, but it brought another result than expected ...
Dim theElementCollection As HtmlElementCollection
theElementCollection = WebBrowser1.Document.GetElementsByTagName("Div")
For Each curElement As HtmlElement In theElementCollection
If curElement.OuterHtml.Contains("info") Then
Variable1 = (curElement.GetAttribute("InnerText"))
End If
Next
Label1.Text = Variable1
The result was the value of this Div that also has the "info" class.
<div class="msg"><div class="info">OMG/BTC and OMG/ETH OmiseGO markets added</div>
Change the second try:
For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")
If Element.GetAttribute("className") = "lastPrice" Then
For Each Element1 As HtmlElement In Element.GetElementsByTagName("div")
If Element1.GetAttribute("className") = "info" Then
Dim Variable1 as String = Element1.InnerText
End If
Next
End If
Next
Yes, you have to use className for class attribute.
This code is verified against the following HTML: This also have more than 1 info class.
<html>
<body>
<div class="firstPrice">
<div class="name">First Price</div>
<div class="info">11650.00</div>
</div>
<div class="lastPrice">
<div class="name">Last Price</div>
<div class="info">11650.00</div>
</div>
</body>
</html>

How to extract innerText of "Who was your first employer? into Excel from the given HTML

<div class="ContainerFieldControlHoriz" style="width: 400px;">
<label title="Who was your first employer?" id="dnn_ctr1854_ChallengeQuestion_ChallengeQuestionCmnTextBox_ControlLabel">Who was your first employer?</label>
</div>
My code was
Set htmlsecurityquestion = htmldoc.getElementById("dnn_ctr1854_ChallengeQuestion_ChallengeQuestionCmnTextBox_ControlLabel")
Range("A1").Value = htmlsecurityquestion.innerText
but it is not working
You access this value through attributes collection, not through innerText. Your second line should be:
Range("A1").Value = htmlsecurityquestion.Attributes["title"].Value;

Image Src url extraction using ms access vba ie navigation

I am using MS Access code for VBA IE navigation. I want to extract image link value from below mentioned html code but unable to extract image link value.
HTML code is given below:
<div class="product-image-vp-sub">
<div class="js-media-zoom-icons hide-content">
<div class="zoom product-zoom product-zoom-in js-zoom-in wmicon wmicon-zoom"></div>
</div>
<img itemprop="image" src="http://ll-us-i5.wal.co/dfw/dce07b8c-3eca/k2-_f47f1f48-69bb-4277-9009-6f7d3a63697a.v2.jpg" class="product-image js-product-image js-product-primary-image" data-asset-id="2A5BB9FEFACA4EA09290025ED003ACAE" data-zoom-image="" alt="...And The Earth Did Not Swallow Him">
</div>
VBA code is given below.
Set html = ie.Document
my_data1 = html.getElementsByClassName("product-image-vp-sub")
For Each Item In my_data1
href1 = Item.getElementsByTagName("img")(0)
href2 = href1.src
Next

Convert String into Div Object in vb.net/asp

I have a bit of a difficult problem which I can't seem to find out how to solve.
Basically, I have a couple tables on my database which, identify, by client, which divs IDs the client has access to, by using the tab
So I have a table which identifies the divs by their ID, by using the table index:
id | id_div
0 | D0
1 | D1
(and so on..)
And then another one which has only the clients ID and the divs (identified by the "id" field) he has access to:
client_id | div_id
29 | 0
29 | 1
(and so on..)
Then I'm cross referencing which divs should be visible and which should not.
The problem is I am getting the divs id as a string and in order to be able to tell in code-behind to set the visibility to false I need to reference the div in itself..
A sample:
<dx:TabPage Name="tabServico" Text="<%$ Resources:InterfaceGenerica, lblServico %>">
<ContentCollection>
<dx:ContentControl>
<div class="conteudo_pagina_tab">
<asp:HiddenField ID="hidID" runat="server" Value="0" EnableViewState="true" />
<asp:HiddenField ID="hidIdCliente" runat="server" Value="0" EnableViewState="true"/>
<div id="D0" runat="server">
<div class="cols coluna1">
<asp:Literal ID="litClientes" runat="server" Text="<%$ Resources:InterfaceGenerica, lblCliente %>"></asp:Literal>
</div>
<div class="cols coluna2-4">
<dx:ASPxComboBox ID="cboClientes" runat="server" HelpText="" ValueField="id_cliente" TextField="nome_completo" SelectedValue="" Width="100%" AutoPostBack="true"></dx:ASPxComboBox>
</div>
</div>
<clear></clear>
<div id="D1" runat="server">
<div class="cols coluna1">
<asp:Literal ID="litTipoOperacao" runat="server" Text="<%$ Resources:InterfaceGenerica, lblOperacao %>"></asp:Literal>
</div>
<div class="cols coluna2-4">
<dx:ASPxComboBox ID="cboTipoOperacao" runat="server" Width="100%" HelpText="" ValueField="id_operacoes" TextField="nome" SelectedValue="" AutoPostBack="true">
</dx:ASPxComboBox>
</div>
</div>
<clear></clear>
<div id="D2" runat="server">
<div class="cols coluna1">
<asp:Literal ID="litTipoServs" runat="server" Text="<%$ Resources:InterfaceGenerica, lblTipoServico %>"></asp:Literal>
</div>
<div class="cols coluna2-4">
<dx:ASPxComboBox ID="cboTipoServs" runat="server" HelpText="" ValueField="id_tipo_servs" TextField="nome" SelectedValue="" AutoPostBack="true" Width="100%"></dx:ASPxComboBox>
</div>
</div>
<div id="D3" runat="server">
<div class="cols coluna5">
<asp:Literal ID="litSubTipoServs" runat="server" Text="<%$ Resources:InterfaceGenerica, lblSubtipoServico %>"></asp:Literal>
</div>
<div class="cols coluna6-8">
<dx:ASPxComboBox ID="cboSubTipoServs" runat="server" HelpText="" ValueField="id_tipo_subtipos" TextField="nome" SelectedValue=""></dx:ASPxComboBox>
</div>
</div>
And in code behind I have:
Dim cross As New Hashtable()
Dim divsCliente() As String
Dim lstDivs As List(Of campos_agd_form)
lstDivs = campos_agd_form_mapper.CarregarDivs()
If lstDivs IsNot Nothing Then
For Each i In lstDivs
cross.Add(i.id, i.id_div)
Next
End If
Dim lstDivsCliente As List(Of clientes_campos_agd)
lstDivsCliente = clientes_campos_agd_mapper.CarregarCamposCliente(guser.id)
If lstDivsCliente IsNot Nothing Then
divsCliente = (lstDivsCliente.Item(0).id_campos_enum).Split(",")
End If
'Dim divsCliente() As Integer = Convert.ToInt32((lstDivsCliente.id_divs).Split(","))
For Each item In cross
For Each i In divsCliente
If item.Key = Convert.ToInt32(i) Then
Dim div As System.Web.UI.HtmlControls.HtmlGenericControl
div = TryCast(item.Value, System.Web.UI.HtmlControls.HtmlGenericControl)
div.Visible = False
End If
Next
Next
As I was already expecting I can't convert a string into a HtmlObject so what I need to do is to find an object by it's id (the string), without having to go through the parent-object (basically, search the whole document, like one would do with javascript with a getElementById)
How can this be accomplished?
The framework I'm using is .NET 4.0
I Recommend the following approach.
You need to know which information to show to each user, so you might want to store this in session for example (Global.asax):
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fetch from DB
Session("Rights") = {"MyID1", "MyID3"}
End Sub
Then create a base user control that checks from the session if it's id is in the list of the rights the user has. If not, the control will automatically hide it self:
Imports System.Linq
Public MustInherit Class MyBaseControl
Inherits System.Web.UI.UserControl
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
If Page.IsPostBack Then Return
Dim rights As String() = CType(Session("Rights"), String())
If Not rights.Any((Function(s) s = Me.ID)) Then Me.Visible = False
End Sub
End Class
Then create x number of content controls that inherit from this base control. These controls can have totally different content, but consider making as few as possible, since your D0, D1 etc seem to have almost same content. So just customize the control to handle different texts and values:
Public Class MyControl1
Inherits MyBaseControl
End Class
Then on the page you will have as many of these controls as needed:
<div>
<uc1:MyControl1 ID="MyID1" runat="server" />
<uc2:MyControl2 ID="MyID2" runat="server" />
<uc3:MyControl3 ID="MyID3" runat="server" />
</div>
Hope this helps.
So, I ended up doing things a little differently.
Basically I'm using a ClientScriptManager, and constructing an array with the elements to hide. (Which is then passed to the client side).
So the function now looks like this:
Private Sub ManipulaFormCliente()
Dim cross As New Hashtable()
Dim divsCliente() As String = New String() {}
Dim aux() As String = New String() {}
Dim cs As ClientScriptManager = Page.ClientScript
Dim lstDivs As List(Of campos_agd_form)
lstDivs = campos_agd_form_mapper.CarregarDivs()
If lstDivs IsNot Nothing Then
For Each i In lstDivs
cross.Add(i.id, i.id_div)
Next
End If
Dim lstDivsCliente As List(Of clientes_campos_agd)
lstDivsCliente = clientes_campos_agd_mapper.CarregarCamposCliente(" id_cliente = " & Convert.ToInt32(hidIdCliente.Value))
If lstDivsCliente IsNot Nothing Then
If lstDivsCliente.Count <> 0 Then
divsCliente = (lstDivsCliente.Item(0).id_campos_enum).Split(",")
End If
End If
For Each item In cross
For Each i In divsCliente
If item.Key = Convert.ToInt32(i) Then
cs.RegisterArrayDeclaration("divsCliente", "'" & item.Value & "'")
End If
Next
Next
End Sub
Then, on the client side I made a function which runs once the window has loaded, and uses the array constructed on code-behind to apply a css "display: none" on the divs whose IDs get passed on the array.
The code is the following:
window.onload = function hideFields() {
if (divsCliente.length > 0) {
for (var i = 0; i < divsCliente.length; i++) {
document.getElementById(divsCliente[i]).style.display = 'none';
}
}
}
This implements the behaviour desired: Whenever there's a postback (and respective load) this function is run, hiding the required divs/fields.
As a final touch, I had to add the 'clientidmode = "static"' attribute to the divs, in order to get the getElementById() function to work properly (according to the data in the DB)
I hope this helps anyone in need of a similar solution.

Trying to show or hide a DIV with vb.net code

Following example stated in
How can i do an if statement inside a repeater
I'm trying to show or hide a div in html page that works with vb.net code, but no success.
<div runat="server" visible='<%# Container.DataItem.cod_dni = "10211721")%>' >
Hello
</div>
Got error ID:
BC30944
Syntax error in cast operator; two arguments separated by comma are required.
Also I tried
<div runat="server" visible='<%# Container.DataItem("cod_dni") = "10211721")%>' >
Hello
</div>
and got same error ID.
You probably just need to cast the Container.DataItem.. Assuming cod_dni is a string, try this:
<div runat="server" visible='<%# CStr(Container.DataItem("cod_dni")) = "10211721"%>' >
Hello
</div>
Also,
there's an extra ) in your statement at the end, so I took it out.
Try this:
visible='<%# IIf(Eval("cod_dni").ToString() = "10211721",true,false) %>'