I am stuck with an issue that puzzled me. Basically i have a custom file *.ascx that is a user control i am writing.
By now it's written as follow
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="...." %>
<div class="btn-group" role="group">
<button type="button" style="height:35px;" runat="server" onserverclick ="Delete_ServerClick" class="btn btn-danger">
<i class="bi bi-trash-fill"></i>
</button>
</div>
In the server side code i have written the following code :
protected void Delete_ServerClick(object sender, EventArgs e)
{
// ---> BREAK POINT HERE FIRST INSTRUCTION <---
//deletion logic
}
In my page i have many instances of such user control and the first thing i note is that the delete button does not have any ID, here the code as it's rendered in the browser (each instance is equal to others) :
<div class="btn-group" role="group">
<button onclick="__doPostBack('ctl00','')" type="button" style="height:35px;" class="btn btn-danger"><i class="bi bi-trash-fill"></i></button>
</div>
When i click on the button, postback is fired, page reload, but the breackpoint in the code behind does not fire. I think the issue is the fact that each button come without ID (even if i put "AutoID"), but it's not clear to me what's wrong here.
Well, be it a plain jane asp.net button, or a simple button?
In BOTH cases, you still ALWAYS BUT ALWAYS want to add a "id" to that control. There no reason to think of omitting the "id" in such cases.
So, for buttons say due to wanting bootstrap icons?
Then this:
<button id="cmdSave" runat="server" class="btn myshadow" type="button"
onserverclick="cmdSave_Click">
<span aria-hidden="true" class="glyphicon glyphicon-floppy-saved">Save</span>
</button>
<button id="cmdCancel" runat="server" class="btn myshadow" style="margin-left: 15px"
type="button"
onclick="MyClose();return false">
<span aria-hidden="true" class="glyphicon glyphicon-arrow-left">Back/Cancel</span>
</button>
<button id="cmdDelete" runat="server" class="btn myshadow" style="margin-left: 15px"
type="button"
onserverclick="cmdDelete_ServerClick"
onclick="if (!confirm('Delete Record?')) {return false};">
<span aria-hidden="true" class="glyphicon glyphicon-trash">Delete</span>
</button>
Only real issue to note?
the standard of the client side click that returns true (or false) AGAIN works the same as a standard asp.net button. But there are 2 noteworthy differences.
Note the 2 events used:
onserverclick="cmdDelete_ServerClick"
onclick="if (!confirm('Delete Record?')) {return false};"
So, compared to a standard asp.net button you have this:
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick=""
OnClientClick=""
/>
Also note that you can (with both buttons) always generate the click event code stub by simple typing in
OnClick= (for asp.net button)
or
onserverclick= (for button)
In both cases, then intel-sense will kick in, and "offer" to create the code stub. (but in both cases, we ALWAYS assume a "id" for the control has be set/used/created like always.
So you get this effect:
the next REALLY BIG important FYI?
While for a asp.net button, as noted, above also works, and as noted, you ALSO have both events. However, for the asp.net button, you can say go:
<asp:Button ID="Button2" runat="server" Text="Button"
OnClick="Button2_Click"
OnClientClick="return confirm('really delete');"
/>
So, if you don't hit ok to confirm, then the server side code stub will not run.
However, WHEN you use the "non" asp button, then BOTH the client side and server side code "generated" behind the scenes is COMBINED!!!!
That means, if the "client" side js code you put in the onclick event "exists", then the server side click code will NEVER run.
So, you have to re-write the above simple "return false" like this:
onclick="if (!confirm('Delete Record?')) {return false};"
So, if you don't return false, think of that "js" expression as having to continue for the 2nd part of the button (the server side click event) to run.
However, if you not using the true/false ability of js to "control" or determine if the server side click runs, then the above does not apply to you.
And if you say drop a button (either kind) into a grid view, repeater etc.?
Then you are Still 100% free to add the click events using the above intel-sense, and in fact for buttons dropped into a grid, you can't double click on the button in the designers to generate the click stub for code behind, and thus you HAVE to use the above "mark-up" example and let inte-sense pop up that context menu and choose "create new event"/
So, your buttons? Yes, they can work, really work much the same as a asp.net button, but in all cases, such buttons need a "id". In fact, even without any server side code, the JavaScript standard is that any such controls should have a "id" assigned to them. The designer does not add the "id" for you, but in most cases one will change/edit the "id" to something more meaningful then the default, and thus in both cases, you tend to wind up having to type in that id by hand anyway.
More FYI:
While the above buttons above look like this:
Do be careful, since due to a lawsuit and ownership issue in regards to those glyph icons?
Versions after bootstrap 4 don't include the glyph icons, and thus you have to grab a set from some place else, or consider say fontawsome, or some such.
In other words, if you decide to "update" your bootstrap version, you find all of a sudden, those icons and the "classes" that define them are no longer included in newer versions of bootstrap.
One more FYI:
i strong but VERY strong suggest that you adopt the habit of adding type="button" when using the standard button. While in "most" cases, they work, and work the SAME as a asp.net button?
in some cases, I think update panels, and other cases? if you do NOT add the type="button", they don't fire the server side code. I don't know the details as to when/why/where/how/what causes this, but it is a wide spread reported issue. So, do keep this in mind.
So, those buttons I posted above?
Note how I added type="button" to them.
As noted, in most cases they just work, but often they might not, so, wrap a string around your finger for this issue - I often attempted to debug a page, only to realize the issue was leaving out the type="button".
Other this the above issue (of using js code to conditional control the post-back, and type="button", both button choices from what I can tell work the same anyway (assuming the runat="server" tag).
So, typing in markup (and there is no button control to drag from the tool box?). They are not generated buttons, but are in fact markup typed by YOU, and thus you need to add/have/use a "ID" for such controls, and you should adopt this for most non asp.net controls (not just buttons), let alone the built in controls.
I want to achieve clicking a HTML button via reading its Value or innerText in C# Selenium, none of the methods for click is seem to work, no idea why?
I was wondering if this is possible if so please advise my code is below.
Html below
<div class="a-button-stack">
<span class="a-declarative" data-action="dp-pre-atc-declarative" data-dp-pre-atc-declarative="{}" id="atc-declarative">
<span id="submit.add-to-cart-ubb" class="a-button a-spacing-small a-button-primary a-button-icon">
<span class="a-button-inner">
<i class="a-icon a-icon-cart"></i><input id="add-to-cart-button-ubb" name="submit.add-to-cart-ubb" title="Add to Shopping Basket" data-hover="Select <b>__dims__</b> from the left<br> to add to Basket" class="a-button-input" type="submit"
value="Add to Basket" aria-labelledby="submit.add-to-cart-ubb-announce">
<span id="submit.add-to-cart-ubb-announce" class="a-button-text" aria-hidden="true">Add to Basket</span></span></span>
</span>
</div>
My code in C# is the following:
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//*[contains(text(),'Add to Basket')]")).Click();
driver.FindElement(By.XPath("//button[contains(text(), 'Add to Basket')]")).Click();
//input[#value='Add to Basket']
There is no button tag but you can grab the input tag by it's data attribute value like so.
You are trying to use an element of a button but a button element does not exist.
The add to cart button in the html is:
//input[#id='add-to-cart-button-ubb']
I got a form with a radio button (button with value 3) that I need to select and a submit button that need to be clicked. The problem is I don't know how to select the radio buttons or click the button since it doesn't have a name or id.
Here is the HTML code:
<input type="radio" name="chosen" value="3" id="a3">
<input type="radio" name="chosen" value="2" id="a2">
<input type="radio" name="chosen" value="1" id="a1">
<input value="Next" type="submit">
Here is the code I've tried for the button (I'm trying to select the button with value 3:
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("chosen")[2].InvokeMember("click");
but that code won't work and I don't know how to click the button to submit and go on. I need help selecting the radio button and click the submit button. I don't have access to change any of the HTML code.
I'm not quite sure what you meant by "use C# to click". Because basically, the element is displayed and interact in browsers (Chrome, FireFox). And C# is for processing http requests in server (IIS).
So to select/click some html element, you can use jQuery such as
$('input[value="3"]').click();
I have an tag within my listview to direct the user to the profile page
<a class="btn btn-sm btn-default" href="profile.aspx?ID=<%# Eval("ownerID") %>"></a>
The profile page works as I can load it myself. However when I hover over the tag it shows the the correct link that it'll be directed to, but when I click on it, it just refreshes the current page, I've used the tag the same way in different pages and it works but for some reason it does not work here.
What could be the reason why?
I think you need to try this anchor tag, HREF should be in single quote.
<a class="btn btn-sm btn-default" href='profile.aspx?ID=<%# Eval("ownerID")%>'>For Test</a>
Is it possible to add HTML input buttons to asp.net triggers, as I have a message box it works perfectly for a gridview which is in update panel,
but when I go to a different page of gridview, message box displays but buttons stops working, I don't know how to debug it, please help.
this is the button,
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
and can I add it to,
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
OR should I not ?
with runat server tag you use html controls in c# script.
i.e
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" />
should be
<input type="button" id="Button2" value="Cancel" cssclass="rightButton" runat="server" />
and the when you double click on it(assuming VS as IDE), it will make a new click event in c# snippet. Besides its good practice to use direct html tags when complex functions are not needed, it saves time to display the page, as an asp component first translates itself in the html and then goes to browser; while this approach saves time of translation.
Regards
Yes you can! to see errors ,first remove UpdatePanel from your code and then test elements without it,in this status if any error occurs will be shown, after test you can add it again.