C# Click html submit button with no id - c#

I have a page in my C# webbrowser that contains
<input type="submit" value="Sign out">
Since it does not have an id, I am unable to use the webbrowser's htmldocument to get it by an id and invoke its click. How would I click it using htmldocument now?

You can give it your own ID you know, it's doesn't have to be a server control for that. Just do:
<input type="submit" ID="MySubmitButton1" value="Sign out">
You mention you want to call something when it is clicked, alternatively just add the onclick event manually:
<input type="submit" onclick="CallMyFunction()" value="Sign out">
Pass 'this' into it if you want to pass the input control into the function:
<input type="submit" onclick="CallMyFunction(this)" value="Sign out">
You probably don't want the submit button to post anything when you call your function as well, so you probably want something like:
<input type="submit" onclick="return CallMyFunction()" value="Sign out">
function CallMyFunction()
{
bool IsFormValid = false;
// Check if form is valid
return IsFormValid;
}

Assuming you don't have control over the HTML page and therefore cannot add an ID to the element, you could use the HtmlDocument.GetElementsByTagName to get all input elements, and then filter that collection by the type and value attributes. Something like:
var firstMatchingSubmit = (from input in myDocument.GetElementsByTagName("input")
where input.GetAttribute("type") == "submit" &&
input.GetAttribute("value") == "Sign out"
select input).FirstOrDefault();
if (firstMatchingSubmit != null)
{
firstMatchingSubmit.RaiseEvent("click");
}
Note that this approach is not appropriate if there are multiple matching elements (only the first one will be clicked).

What about this:
webbrowser1.Document.Forms[0].InvokeMember("submit");

How to handle page with multiple html submit button with no id? How to identify the submit button from 2nd row.Multiple html buttons

Related

How to disable angularjs from capturing my form submit for the second submit button

Thanks for checking.
I am building a web application using angularjs. i have two buttons one will do a Ajax post to server using angularjs and the other will to a normal html post to server. the reason i want the second one to do a direct post to server is that i want to generate an excel document on server and return it tp user to download on response.
please how can i disable angularjs from capturing my form submit for the secont submit button
I'd say that the simplest way is to change the button that shouldn't post the form to not be a form button, but another element that doesn't have that default behaviour. That is, change this:
<form>
<input type="text">
<input type="submit" ng-click="doStuff()" value="Ajaxy things">
<input type="submit" value="Real post">
</form>
to:
<form>
<input type="text">
<a ng-click="doStuff()">Ajaxy things</a>
<input type="submit" value="Real post">
</form>
And style the a to look like a button.

Multiple "OnSubmit" in Form

On the current website I am working on we have a Html Form that wraps everything and is used by the global search to submit and search when the user presses enter in the "global search" text box.
The problem we are now having is that we have a application form which has its own text boxes within this Html Form and when you press enter it does a onSubmit for the Global Search.
So in short I was wondering if there was any way to change certain specific text boxes to do a different onSubmit than the global search.
Many Thanks,
Vincent Kong
Instead of using type="submit" buttons use regular buttons (type="button") then call javascript to do what you want.
you can use HTML5 formaction Attribute:
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit"><br>
<input type="submit" formaction="demo_admin.asp" value="Submit as admin">
</form>
Although this quickly becomes messy, you can set the CommandName attributes on each of your buttons and handle them appropriately (based on the command value of the clicked button) during the postback.

How do I call the click function of an HTML button using the class name of the control or its value but not the ID?

I'm creating a WPF application that opens a WebBrowser instance and get the HTML code ...
I was calling the elements by ID for example:
if the HTML is:
<input type="text" name="company" id="company" value=""/>
the C# code to add value to this element will be:
wb.Document.GetElementById("company").SetAttribute("value", String.Format("{0}", Company));
where wb is an instance of the WebBrowser Class
or if i have a button and need to click the Click event:
<input type="submit" id="button1" />
the C# code will be:
wb.Document.GetElementById("button1").InvokeMember("click");
now I have a problem is that I need to call the click function of a button that doesn't have an ID but just have a class name and value
like the one below:
<input class="submit" type="submit" value="register"/>
how can i do this?
You have to use GetElementByTagName("INPUT") and then find your HtmlElement by its GetAttribute("class") method to invoke the click event.
var elems = wb.Document.GetElementByTagName("INPUT");
foreach(HtmlElement elem in elems){
if(elem.GetAttribute("class") == "submit"){
elem.InvokeMember("click");
}
}

Cancel button in form

I have a cancel button in a form:
#using (Html.BeginForm("ConfirmBid","Auction"))
{
some stuff ...
<input type="image" src="../../Content/css/img/btn-submit.png" class="btn-form" />
<input type="image" src="../../Content/css/img/btn-cancel.png" class="btn-form" />
}
The issue is I want this button to go to a particular view when I click on it. How do I do this?
Either you can convert the Cancel button as an anchor tag with #Html.ActionLink helper method and apply a css class which makes the link to looks like a button and then in the controller action for that link, you can return the specific view.
#Html.ActionLink("Cancel","Index","Products",null, new { #class="clsButtonFake"})
or
Use 2 submit buttons in the form. One for real submit and one for the cancel. and in your controller action, check which button called the action method.
You can read more about it here in this answer.
Lot of the answers worked in either of the browsers, chrome or ie but not all.
This worked in all -
<input type="button" value="Cancel" onclick="location.href='#Url.Action("Index","Home")';"/>
This is my button HTML:
<button type="button"
class="btn btn-inverse"
id="cancel"
onclick="window.history.back()">
<i class="icon-remove icon-large"></i>
<br />#Localization.Cancel
</button>
Then to customize the onclick attribute in some views I do this:
<script>
$(document).ready(function ()
{
$("#cancel").
attr("onClick",
"document.location.href='#Html.Raw(Url.Action("Index", "Standard",
new { ManualId = Model.ManualId, ChapterId = Model.ChapterId }))'");
});
</script>
Or a styled submit button:
<input type="submit" value="Save Form" name="Save" class="submit-button btn-form" />
Then Javascript for cancel button:
<input type="button" onclick="document.location.href('Home/Index')" value="Cancel" class="cancel-button btn-form" />
// Note: This avoids any of the validation that may happen in the model that
// normally gets triggered with a submit
So with Shyju's appraoch, you use the built in MVC ActionLink helper. Doing this, you'll need to have any images or icons done through css. However, this is much more cachable, especially if you use base64 strings for your images in css.
I like Adauto's approach because it gives you much more control of the markup. MVC Html Helpers are nice, but they still seem to have their heads in the WebForms mindset of "don't worry about it, we'll take care of it for you".
The one thing I would add is Url.Content.
<img src="#Url.Content("~/Content/css/img/btn-submit.png" class="btn-form" />
It's never really a good idea to make your views have to know the location of content relative to it's location.
<a href="/Auction/[ActionName]">
<input type="image" src="#Url.Content("~/Content/css/img/btn-cancel.png")" class="btn-form" />
</a>
if you want to preserve its look as a button, you could do something like this:
<a href="/Auction/[ActionName]">
<input type="button" value="Cancel">
</a>
where [ActionName] is the name of the action that will return your desired view.
<img src="../../Content/css/img/btn-submit.png" class="btn-form" />
I ended up making a helper so I could reuse the cancel button. I added a js confirm in case people click the cancel button by accident after filling in the form.
#helper FormCancelButton(string cancelUrl)
{
<button type="button" class="btn" onclick="if (confirm('Cancel changes?')) location.href = '#cancelUrl';">Cancel</button>
}
I then call it like so:
#FormCancelButton(Url.Action("Index", "User" ))
If you are really keen you could try and detect the dirty state of the form too and only show the confirm dialog if the form had been changed.
<asp:Button runat="server" class="btn btn-danger"
CausesValidation="false" onclick="Cancel_Click" Text="Cancel"/>
protected void Cancel_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx");
}

Does ASP.NET MVC swallow submit values?

I have a single form in ASP.NET MVC (v1) that has 2 input buttons. Each submit button has to be contained within this single form and I need to know which one the user pressed.
I know about the trick to check the FormCollection values which will be returned based on the button pressed. For example, if I have and and the user clicks Button2, I should be able to say Request.Form["Button2"] != null and that will evaluate to true in which case I know that the user clicked that button.
However, this is not working for me. The values of all my buttons is null as non of them are contained within the Request.Form values. Is there a bug in ASP.NET MVC which swallows these values?
Here is my form code:
<% using (Html.BeginForm()) {%>
<% Html.RenderPartial( "EditAreaControl", Model ); %>
<div class="form-layout-command-container">
<div class="form-layout-command-area-alpha"><button type="submit" name="submit1" value="Save">Save</button></div>
<div class="form-layout-command-area-alpha"><button type="submit" name="submit2" value="SaveAndCopy">Save and Create Copy</button></div>
<div class="form-layout-command-area-beta"><%= Html.ActionLink("Cancel", "list") %></div>
</div>
<% } %>
Here is my controller code:
[AcceptVerbs( HttpVerbs.Post )]
public ActionResult Add(FormCollection values )
{
if (values["submit1"] != null)
// always false
if (values["submit2"] != null)
// always false as well
}
From w3schools:
Important: If you use the button element in an HTML form, different browsers will submit different values. Internet Explorer will submit the text between the and tags, while other browsers will submit the content of the value attribute. Use the input element to create buttons in an HTML form.
It seems that this is not standardized. You should stick to
<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="Cancel" />
I would use inputs of type submit instead of buttons. Non-inputs may not passed back in a form post or at least can be passed back inconsistently. Note that they can have the same name with different values so that you can use the same parameter for any button that submits the form.
<input type="submit" name="submitButton" value="Save" />
<input type="submit" name="submitButton" value="SaveAndCopy" />
public ActionResult Save( string submitButton, ... )
{
if (submitButton == "Save")
{
...
}
else if (submitButton == "SaveAndCopy")
{
...
}
....
}
Using Firebug, I found that the submit buttons were not being sent in the response and because of that, there isn't much I can do on the MVC side. I decided to use a client side hack to populate a hidden input field on the client side which would be passed to the controller values.
I changed the input buttons to be:
<input type="submit" value="Save" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="submit" value="Save and Copy" onclick="actions.copyValues($(this), $('#submitAction'));" />
<input type="hidden" id="submitAction" name="submitAction" />
The jquery script simply copies the values:
Actions.prototype.copyValues = function(from, to) {
$(to).val($(from).val());
};
The controller action then looks for the hidden input values:
var request = HttpContext.Request;
return request.Form["submitAction"];
This solves the issue from above but I realize it is not that clean.
Put them in two different forms and you will know which one submitted based on which action was called on the controller.

Categories

Resources