disable href on content page from Master page in vb.net - vb.net

Just as the title says. I have code that disables all controls that runat=server. Its as follows
Dim c As Control
For Each c In pc
If c.HasControls Then DisableAllControls(c.Controls)
If c.GetType.ToString.ToLower.IndexOf("webcontrols.dropdownlist") > -1 Then
DirectCast(c, DropDownList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.textbox") > -1 Then
DirectCast(c, TextBox).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobuttonlist") > -1 Then
DirectCast(c, RadioButtonList).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.radiobutton") > -1 Then
DirectCast(c, RadioButton).Enabled = False
ElseIf c.GetType.ToString.ToLower.IndexOf("webcontrols.button") > -1 Then
DirectCast(c, Button).Enabled = False
End If
Next
But I have a couple of href's in there that i want to disable also. I know they dont runat server, so how can i catch these?

You can add runat="server":
<a runat="server" href="..." ></a>
It is the only way to maintain controls using server side code.

You can add runat="server" to HTML controls too. Without it you won't be able to access the control on the server side and you won't be able to disable it.

That code looks invalid (If c.HasControls Then DisableAllControls(c.Controls) has no matching End If) but perhaps VB.NET has added inline If syntax like that without me realising.
Anyway, as far as disabling all runat="server" controls, you should be able to just do this:
For Each c as WebControl In pc.OfType(Of WebControl)()
' Put your recursive call here as before
c.Enabled = False
Next
Now, to "disable" those other elements, you can either add runat="server" to them (perhaps not even possible if you have generated HTML), or you can use some JavaScript. I'm going to assume by disable you mean hide in the case of <a> tags?
jQuery makes this easy, with a sample script being something like:
$(document).load(function() {
$('a').hide();
});
or:
/* hides all a tags under an element with class="someClass" */
$(document).load(function() {
$('.someClass a').hide();
});
You could then have your code render this script, using something like this in your Page:
Dim script as String = "" /* your javascript here */
Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "HideTagsScript", script)

Related

Using VBA, how do I input a value to a <input> text box on a webpage that uses Knockout JS?

I'm trying to populate a form on a webpage that uses Knockout JS. When I insert a value into a particular text box it appears to have taken the value but when I submit the form it retains it's previous value. With a bit of digging around I've discovered that Knockout JS uses data-binding to update the underlying value. When I insert a value into the input text box the Knockout JS is not being triggered and therefore not updating the underlying value. This post is for something similar but with a check box and dropdown box rather than a text box. But I can't figure out how to get it working with a text box.
The actual website I'm trying update requires a login, however I think this page on the Knockout JS Site http://knockoutjs.com/examples/helloWorld.html will suffice for testing purposes.
Basically what I need is, using VBA, to enter a FirstName and LastName in the 2 text boxes (where it currently says 'Planet' and 'Earth'). You should see the 2 input values appear immediately below where it says 'Hello Planet Earth'. Typing it manually into the text boxes works fine. But I just can't get it to work in VBA.
Here' what I have so far...
Sub KnockoutTest()
Dim IE As New InternetExplorer
Dim hDoc As HTMLDocument
IE.Visible = True
'Load webpage and loop until loading is complete.
IE.Navigate "http://knockoutjs.com/examples/helloWorld.html"
Do While (IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE)
DoEvents
Loop
Set hDoc = IE.Document
Set firstName = hDoc.getElementsByClassName("liveExample").Item(0).all.Item(1)
Set lastName = hDoc.getElementsByClassName("liveExample").Item(0).all.Item(3)
firstName.Click
firstName.Value = "Joe"
lastName.Click
lastName.Value = "Bloggs"
'Close ie object
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Anyone got any ideas on how I get the knockout events to fire?
The actual html from the text box on the page I'm trying to update is this....
<input type="text" ondrop="return false;" onpaste="return false;" data-bind="numericValue: Markup, visible: IsCellEditable(), selected: IsCellEditable(), event: { blur: blurZoneMarkup, keypress: function (data, event) { return $parent.isDecimal(data, event, hdnIsDiscount) }, keydown: function (data, event) { $(event.target).data('tabcode', event.which || event.keyCode); { return true; } } }" style="width: 90%; display: none;">
Any help or advice would be much appreciated. I've been trying to solve this for 3 days now!
Basically, you would need to handle such automated inputs as 'HTMLevents' and trigger them. An updated snippet from your original code:
With hDoc
Set evt = hDoc.createEvent("HTMLEvents")
evt.initEvent "change", True, False
firstName.Value = "Joe"
firstName.dispatchEvent evt
End With
Tested this code and works fine for the website that link that you have provided.
I have done tons of website automations with VBA and the most robust approach is to create one event, specific to each field that you would want to fill, assign a value to the field, and dispatch the event that you have created.
For further information, refer to the documentation on MSDN https://msdn.microsoft.com/en-us/ie/ff975247(v=vs.94)

Selecting an item from a dropdown doesnt trigger any action with VB.net

I'm trying to automate loading data using vb.net, but there comes a point where I select values of various combobox. Seeing the code of the page I find that the combobox has a format similar to this(its just an example):
<select id="BirthMonth" name="BirthMonth">
<option value="">Month</option>
<option value="01" >January</option>
<option value="02" >February</option>
<option value="03" >March</option>
<option value="04" >April</option>
fails to work with WebBrowser1.Document.GetElementById ("Name combo") SetAttribute ("OPTION", "March"), searching the Internet I found a solution where inter is a parameter that contains the value.:
Public Sub selector(ByVal inter)
Dim option_ As HtmlElementCollection
option_ = WebBrowser1.Document.GetElementsByTagName("option")
For Each option__ As HtmlElement In option_
If option__.InnerHtml = inter Then
option__.SetAttribute("selected", "True")
End If
Next
End Sub
which if you choose value, but the page does not take it, so it does not refresh the other Combobox.
Anyone know how to do to select the value, so the page can trigger the action?
PS: must be in the name of the field by value.
Thank you very much
Bye!
PS: I solved the problem just adding this lines after the selection:
WebBrowser1.AllowNavigation = True
WebBrowser1.Document.Forms(0).InvokeMember("submit")
Its solved just adding this lines after the selection:
WebBrowser1.AllowNavigation = True
WebBrowser1.Document.Forms(0).InvokeMember("submit")

write text inside div element use webbrowser vb.net

Need to write in element .
This is the HTML code
< div id="t" class="message-tools__textarea js-scroller" contenteditable="true" data-placeholder="Write a message (Enter to send)">XXXXXX< /div>
My question is how to write text like XXXXXX ?????
Add this:
<div id="t" runat="server" ...
And then in your codebehind:
t.InnerText = "XXXXXXX" ' or t.InnerHTML if you're adding HTML code).
So, if the text to be replaced is fixed and has only 1 instance within the HTML code, it should be rather simple:
Dim OrigCode, ModifiedCode as string
OrigCode = GetGoogleCodeFromURL ' get the code
ModifiedCode = OrigCode.Replace("XXXXXXX","ZZZZZZ")
Dim MyHTML as string = "<head>...</head><body><h1>Hello World!</h1><p> </p>" & _
ModifiedCode & "<p> </p><p>That's it.</p>"
But usually, things are a bit more complicated, so I'm not sure, if you expressed your wish precisely. Also, if the code is big and action is recursive, it might be better to break it into parts and handle only relevant part of it, due to performance issues.

Disable ASP Button if Variable is null

I'm sure this is simple, I need to disable a button on my html page if a variable doesn't have a value from inside the code.
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
I've found several options for disabling the button if there is no text for form validation but I haven't found a way to actually connect the VB code with my ASPX page...
My thought was:
'Logic for button enable
If (username.Length < 0) Then eastOpen_Click.Enabled = False
Else eastOpen_Click.Enabled = True
But with that I get an argument not specified error.
Based on this from your sample:
<asp:Linkbutton ID="eastMinute" runat="server" CssClass="btn btn-default" Text="Open Once" Width="95%" />
Your button name is eastMinute, so your code should be:
'Logic for button enable
If (username.Length < 0) Then eastMinute.Enabled = False
Else eastMinute.Enabled = True
The eastOpen_Click sounds like an event handler; that is a function and therefore does not have an Enabled property.
You can also write it in one line like:
eastMinute.Enabled = (username.Length < 0)
What I ended up having to do for this to work properly was do the logic for if the value was passed or not and then add an attribute manually to my button using Attributes.Add().
'Logic for button enable
If (username Is Nothing) Then eastOpen.Attributes.Add("disabled", "disabled") Else

Getting HRESULT: 0x800A01B6 Exception while getting elements from InternetExplorerMedium website

I'm trying to make some code on VB.net that opens a website and logs in, and after that runs a report. Everything has been working fine when I try to get any of the from the website, but the ones that have this instruction within the OnClick property = "return oamSubmitForm('inputParamView:paramForm','inputParamView:paramForm:_idJsp106');"
Basically, if you see the following code, you see that I click on some of the CheckBoxes and it works just fine, but when I retrieve the button, it doesn't type an input, it actually has the link of the website when I use a whatc while debugging.
This is my code (I skipped the login section):
Private Sub open Page()
ieb = New SHDocVw.InternetExplorerMedium()
ieb.Navigate("http://example.example/qptheme2/pages/index.faces")
ieb.visible = True
ieb.Silent = True
While Not (ieb.ReadyState = WebBrowserReadyState.Complete)
Application.DoEvents()
End While
If v.checked = False Then
v.Click()
End If
v = ie.Document.GetElementById("inputParamView:paramForm:inputParametertuesday")
If v.checked = False Then
v.Click()
End If
v = ie.Document.GetElementById("inputParamView:paramForm:_idJsp106")
v.Click() '<-- IT FAILS HERE Exception HRESULT: 0x800A01B6
If I check the watch it shows mshtml.HTMLAnchorElementClass {http://example.example/qpreport/savedpages/savedReports.faces#}
if I check the source code from the page, this is the element that I'm trying to get:
<a id="inputParamView:paramForm:_idJsp106" onclick="return
oamSubmitForm('inputParamView:paramForm','inputParamView:paramForm:_idJsp106');" href="#"
I don't know if it has anything to do with the property OnClick.
I will appreciate any of your help trying to solve this issue.
I found the solution by myself. I just had to instance the element I was retrieving as mshtml.HTMLAnchorElementClass
after that, I was able to click on it.
Dim l As mshtml.HTMLAnchorElementClass = ieb.Document.GetElementById("inputParamView:paramForm:_idJsp106")
l.click()