how i do perform click event using c# in webBrowser - c#

How I do perform click event using c# in webBrowser.
This command is not working in c#, I don't know why.
webBrowser1.Document.InvokeScript("document.getElementsByName(\\"submitAddressButton\\")[0].click()");
But,
When I tried in web console. seem that command is work.
document.getElementsByName("submitAddressButton")[0].click();
I use getElementByName in c#, because there is no ID in HTML element target.
Below is fragmented of HTML :
<form name="addressSelectForm" action="/online_customers/page/manageaddress/site_qualification/
6d5b51984c1b4a73aaf722f01474a256/select_address?submitToken=0980629f4dd64f1d9e72b05e16281d9b" method="post">
<span style="display:none">LOCXXXXX51</span><input type="submit" class="link-button" name="submitAddressButton"
value="LOCXXXXX51">
<input type="hidden" name="fsaId" value="">
</form>
Thank
Jigu

HtmlElementCollection d0cument = homeBrowser.Document.GetElementsByTagName("button");
foreach (HtmlElement link in d0cument)
{
String class2 = link.InnerText;
if (class2 == "Login")
{
link.InvokeMember("click");
logincheck.Enabled = true;
}
}
i use like this because homeBrowser is have a lot of button

Related

how to put a bind-value and an onchange in the same input checkbox?

hello community I have a problem putting a bind-value and an onchange shows me the following error:
The attribute 'onchange' is used two or more times for this element. Attributes must be unique (case-insensitive). The attribute 'onchange' is used by the '#bind' directive attribute.
this is my input checkbox:
<div class="form-group col-md-2">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1"
#bind-value="#ProveedorEstadoCarrito.Cotizacion.Aceptada"
#onchange="e => CheckChanged(e)">
<label class="custom-control-label" for="customSwitch1">Aceptada</label>
</div>
</div>
this is the event:
private Boolean Aceptada = false;
private async Task CheckChanged(ChangeEventArgs ev)
{
Aceptada = (Boolean)ev.Value;
ProveedorEstadoCarrito.Cotizacion.Aceptada = Aceptada;
if (Aceptada == true)
{
var httpResponse = await repositorio.Put("api/Cotizacion", ProveedorEstadoCarrito.Cotizacion);
if (httpResponse.Error)
{
await mostrarMensajes.MostrarMensajeError(await httpResponse.GetBody());
}
else
{
navigationManager.NavigateTo("/formulario-cotizacion");
}
}
}
I want the checkbox to be activated with the bind if it was clicked
First off, you usually don't bind to the value attribute. It remains fixed, and when present, and within a form element, it is passed as form data to the server.
What you want is the checked attribute, like the code snippet below demonstrates:
<input type="checkbox" checked="#selected"
#onchange="#((args) => selected = (bool) args.Value)" />
#code {
private bool selected;
}
The above code show how to bind to a check box element. As you can see, the above code creates a two-way data binding, from a variable to the element, and from the element to the variable. The value attribute is involved. The same usage is applicable to the radion button element. Unlike other input elements, both employ the checked attribute, not the value attribute.
You may also use this variation:
<input type="checkbox" #bind="selected" />
which is equivalent to the code above: a checked attribute + onchange event, but the version above can let you solve your issue. Here's how your code may look like:
<input type="checkbox" class="custom-control-input" id="customSwitch1" checked="#ProveedorEstadoCarrito.Cotizacion.Aceptada" #onchange="CheckChanged">
And
private async Task CheckChanged(ChangeEventArgs ev)
{
Aceptada = (Boolean)ev.Value;
ProveedorEstadoCarrito.Cotizacion.Aceptada = Aceptada;
if (Aceptada == true)
{
Hope this helps...
You can't. But you can set checked=ProveedorEstadoCarrito.Cotizacion.Aceptada to update the state of the checkbox, and for the #onchange event do #onchange=CheckChanged and in that method you can set ProveedorEstadoCarrito.Cotizacion.Aceptada = (bool) ev.Value;

ASP .NET MVC Bind to multiple model properties from single element

I would like to bind html data-* attribute to separate property in my model. How to do that?
As you can see here my button is binding to Operation property and I would like to bind data-* to property Data_RemoveAt.
public enum LinkListOperation
{
AddOne,
RemoveOne,
RemoveAll,
Submit,
RemoveAt
}
public class StepThree_Notification_TemplateEmailViewModel
{
public LinkListOperation Operation { get; set; }
[DisplayName("data-removeat")]
public int Data_RemoveAt { get; set; }
}
#using (var form = Html.BeginForm("AcceptTask", "Task", FormMethod.Post))
{
<div>Linki:</div>
for(int i = 0; i < Model.Links.Count; ++i)
{
<div>
#Html.TextBoxFor(f => f.Links[i], new { Name = string.Format("Model.Links[{0}]", i) })
<button value="RemoveAt" type="submit" name="Model.LinkOperation" data-removeat="#i">Remove</button>
</div>
}
<button value="AddOne" type="submit" name="Model.LinkOperation">MORE</button>
<button value="RemoveOne" type="submit" name="Model.LinkOperation">LESS</button>
<button value="RemoveAll" type="submit" name="Model.LinkOperation">REMOVE ALL</button>
<button value="Submit" type="submit" name="Model.Operation">OK</button>
}
If you are not using ajax, what you can do is to wrap your submit button inside a form tag and set the values you want to sent as form fields. You may use hidden fields for that.
for(int i = 0; i < Model.Links.Count; ++i)
{
<div>
#Html.TextBoxFor(f => f.Links[i],
new { Name = string.Format("Model.Links[{0}]", i) })
#using(Html.BeginForm("Remove","Home"))
{
<input type="hidden" name="Operation" value="#Model.Operation" />
<input type="hidden" name="RemoveIndex" value="#i" />
<button value="RemoveAt" type="submit" name="Model.Operation">Remove</button>
}
</div>
}
Assuming your action method looks like this
public ActionResult Remove(string Operation,int RemoveIndex)
{
// to do : return something with the passed in values.
}
If you already have an outer form tag, This approach won't be ideal as nested forms are not a good idea. You may consider using javascript-ajax code to read the data attribute value and send it to server.
You do not need to add the form tag like what i mentioned above. Keep your markup as it is except, we will add a css class to the submit button which we will use later as the jQuery selector.
<button value="RemoveAt" class='removeBtn' data-removeat="#i"
type="submit" name="Model.Operation">Remove</button>
And add this javscript to listen to the click event on the button and read the data attribute value and then make an ajax post call. Assuming you have jQuery library loaded to your page,
$(function(){
$(".removeBtn").click(function(e){
e.preventDefault();
var _this=$(this);
var removeIndex=_this.data("removeat");
var op=_this.attr("name");
var url = "#Url.Action("Remove","Home")";
url=url+"?Operation="+op+"&RemoveIndex="+removeIndex;
$.post(url,function(res){
//do something when a response comes back, may be update the UI/reload the page ?
//window.location.href=window.location.href;
});
});
});

How getAttribute("onclick")

For example I have this html code:
<a href="#" onclick="return Index.submit_login('server_pl80');">
<span class="world_button_active">Świat 80</span>
</a>
And I need to get attribute of onclick, because I may get more links and I must find difference between them. For my opinion get attribute of onclick is only one way.
But if I GetAttribute("onclick") from HTMLElement it will return System.__ComObject.
Is there any idea how read onclick value from webBrowser?
I have only this:
HtmlElement selected_div = #webBrowser1.Document.GetElementById("div_id").GetElementsByTagName("div")[0];
HtmlElement a = selected_div.GetElementsByTagName("a")[0];
string rightLink = a.GetAttribute("href");
string onclickLink = a.GetAttribute("onclick"); // return "System.__ComObject" string
if (rightLink == "http://www.example.com/#")
a.InvokeMember("click", null);
"onclick" or "onClick" in getAttribute(); doesn't make a difference
This code works but click on first link on the list of servers.. I need choose the server and links have difference only in onclick attribute.

C# Access tags properties by HtmlElement

There is an html like:
<div id="instance" style="color:red;display:block; .... bila bila">
<h2>some text</h2>
</div>
I wanna access style of div via this code;
foreach (HtmlElement link in webBrowser1.Document.GetElementsByTagName("div"))
{
if (link.GetAttribute("id").ToString() == "instance")
{
MessageBox.Show(link.innerhtml);
}
}
But link.innerhtml gives me the inside of div tag, not div's own. Output text of Messag.Box is:
<h2>some text</h2>
I also tried this too:
MessageBox.Show(link.GetAttribute("style"));
but it didnt work.
How to access div properties via same div's id?
You should probably use something like this:
MessageBox.Show(link.OuterHtml);

Click on element in Webbrowser

I have about 10 of these tags under each other:
<a class="answer" href="#">This is one</a>
<a class="answer" href="#">This is Two</a>
<a class="answer" href="#">This is Three</a>
Now I need my webbrowser in C# to click on <a class="answer" href="#">This is Two</a>
How would I do this? Only got text and a tag with a class. No value this time.
Thanks
If you want it just for that "This is Two" link I believe it would go something like this:
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("This is Two"))
link.InvokeMember("Click");
}

Categories

Resources