.net - identify whether a button has a class when clicked - asp.net

I have an asp button which I am using in 2 different places (appending to a new place and adding a class using jquery under certain conditions).
I need to slightly alter the function that runs when this button is clicked depending on whether or not this button has a given class. Is this possible??
So something like this...
if myASPButton hasclass("xyz") then
...
end if
i'm using vb.net for what it's worth.

It's not possible, clicking a button causes a postback, the only information posted back is form data. Modifications to the HTML client-side wont be reflected severside.
You will need to make a hidden field and populate the value of the hidden field and use that for checking severside.

Related

How Does ASP.NET Knows Which Button Caused a PostBack?

I'm using ASP.NET 4 to create a page with elements that are used in a jQuery UI Sortable plugin.
All of these elements contain a button (implemented as a submit button) with the name SubmitButton. All but one of these buttons are hidden in the page's Load event. But if I drag the element to another position, and then submit the page using that button, ASP.NET gets confused.
ASP.NET thinks I've clicked a different button, one that wasn't even visible on the page, but is associated with content at the position where the clicked button was before the move. (If I don't move the element, it works fine.)
I can't seem to determine how this is happening. As I understand it, ASP.NET knows which submit button caused a postback because the button's name and value is included in the postback data. (__EVENTTARGET plays no role here and is empty.) So how can it think a different button submitted the page?
How could ASP.NET get confused about which button submitted the page?
(Sorry, it's not possible to put this page on a public website.)
I worked through this issue and here's what I found.
First of all, buttons by default are rendered as <input> tags with type = submit. The postback mechanism in this case has nothing to do with ASP.NET. The postback data includes, among other things, the name of the submit button that caused the postback along with the button's text (in the form name=text).
But ASP.NET button names, by default, include the names of all parent controls. When those controls are unnamed, they are given an ID like ctl00, ctl01, ctl02, etc. The result is that all my buttons are guaranteed to have a unique name.
The problem is when these buttons and parent controls are all created dynamically. On the postback, these controls are reconstructed in the load event but in the new order. Because the order affects the name (ctl01 vs ctl02), this means my submit button has a different name than it did when the page was originally rendered.
The result is that ASP.NET sees the wrong name associated with the submit button used to trigger the postback.

GridView with a custom button

Specifically, I am using DevExpress 10.1 for their APSxGridView. I have a grid with a command column. I plan on using two of the standards: new and edit. I would like to create a custom button that will take a value from within the current row and redirect to another page using that value as part of the query string.
I have seen some suggestions to create a hyperlink column to do this, but I would like to have all of these commands in a single column. I can see solutions for this if I weren't constrained by leaving all buttons within a single column. Is this something that maybe I could assign during HtmlDataCellPrepared?
Thanks.
If it's anything like the standard .net GridView I would create a template field. In a template field you can put whatever you want in there. I have done this when I need an asp.net control (a button, hyperlink, etc) instead of the standard grid buttons and commands. You can then access controls and the datasource during the OnRowDataBound event handler and customize the button during run time if need be.

Asp.NET AJAX control update field on form

I have a form with several text fields, a couple of drop down lists, and a custom asp.net control.
The requirement I have is that when the values of certain fields ( some are in the main form, some are inside the control ) the user will be alerted that the settings will not take place unless they also restart the processing and will have the option to reset it. If they exercise this option by pressing on the restart button, we execute an additional restart() method call on the server side on top of everything else.
In order to create this alert system I need to know when these required fields have changed. For the fields in the main form/page I store the original values in hidden fields when the page gets created (possible because the page is static). Then when the submit button is pressed I check the current values against the original values stored. If any of them differ I will alert the user.
For the fields in the custom control, I created a boolean property that indicates whether changes in the fields of interest took place. This control is highly dynamic contains a variable number of lists, some of them with over 100 items that can be selected or deselected. So this boolean property is able to identify if the changes I am looking for took place.
The problem is I need to get the value of this property when the user clicks on Submit, otherwise it will not contain the right value. But at the same time I want to avoid a postback. I do not want the whole page to reload. I want to get that value asynchronously somehow.
Since the whole page is stateless the question is how do I accomplish this? How do I accomplish the call to the property? And after I make the call and I get the result where do I store it so it's accessible from javascript code on the client side.
You can try putting a timer control in your ajax panel. Then on a set interval, you will evaluate the boolean property in your code behind.
Alternatively you can check the hidden field "changed" event using JavaScript or jQuery
http://api.jquery.com/change/

How do I trigger a PostBack with arguments from Javascript?

This is what I'm doing: Using jquery, I'm popping up a "form" over the page that lets the user search for branches of the company and select the one they want. When the form pops up, they can type in a textbox, and it will do AJAX requests back to the server to return the top n results for what they've entered, and those results will be put into a list for them. I want the user to be able to select one by clicking a link that says "select" or something, and at that point I want it to do a PostBack have the Branch Selector control that this is in change it's SelectedBranch property to the newly selected branch. I've got this all working right now with a hard coded list of LinkButtons, but how do I do the same thing with a dynamic list of links inserted with jquery?
Look at the HTML that gets emitted for your hard coded LinkButtons. You'll see that each one calls the JavaScript __doPostBack function when clicked. I believe this function takes two arguments: a control ID and an extra command argument you can use for your own purposes.
I would suggest adding a single control to the page whose only job is handling events for the dynamic links. Then, when you are creating the links with jquery, make each one call __doPostBack, passing the event handling control's ID for the first argument and some other string for the second argument that identifies which link was clicked. In the Click event for the handling control, look at the second argument value and do what you need to do.
The short answer is... you don't.
ASP.NET relies on the Viewstate for the current state of the controls, including items in a DropDownList or similar control. Dynamically updating a list on the client will not modify the viewstate, so will not be available on the back end.
The general workaround for this is to just add a hidden field which updates/stores the current selection via js on the client side. Then read it from this field on the backend rather than List.SelectedValue.

Best way to implement mutliple Default Buttons on a ASP.NET Webform

What is the best way to implement mutliple Default Buttons on a ASP.NET Webform?
I have what I think is a pretty standard page. There is a login area with user/pass field and a login button. Then elsewhere on the same page there is a single search field with a search button.
asp:Panel has a property named DefaultButton. You just need to encapsulate your markup portions with appropriate panels and set the default buttons for each.
Capture the enter key press for each area of the screen and then fire the corresponding button's click even.
Use a helper function like this one to tie the textboxes to their associated buttons.

Resources