Passing additional arguments into the OnClick event handler of a LinkButton using Javascript - asp.net

I have a ASP.NET Website, where, in a GridView item template, automatically populated by a LinqDataSource, there is a LinkButton defined as follows:
<asp:LinkButton ID="RemoveLinkButton" runat="server" CommandName="Remove"
CommandArgument='<%# DataBinder.GetPropertyValue(GetDataItem(), "Id")%>'
OnCommand="removeVeto_OnClick"
OnClientClick='return confirm("Are you sure?");'
Text="Remove Entry" />
This works fine. Whenever the Button is Clicked, a confirmation dialog is displayed.
What I am trying to do now, is to allow the user to enter a reason for the removal, and pass this on the the OnClick event handler. How would I do this?
I tried OnClientClick='return prompt("Enter your reason!");', but, of course, that did not work =)

Personally, I would stash the reason into a hidden field.
It would work something along these lines: your OnClientClick method would take the return value of a JS method, which does the prompt, and then places the result of the prompt into the hidden field.

You can also look into calling __doPostBack from your client-side code instead of using the OnClick postback. Then you can capture the reason and pass it server-side.

Related

ASP.Net textbox OnTextChanged firing even though validation is invalid

Here's a simple version of my code:
<asp:TextBox ID="textBox" runat="server" AutoPostBack="true" OnTextChanged="textChanged" CausesValidation="true" ValidationGroup="valGroup"></asp:TextBox>
<asp:RegularExpressionValidator ID="regEx" runat="server" ControlToValidate="textBox" ValidationGroup="valGroup" ErrorMessage="Not a valid number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSaveClick" ValidationGroup="valGroup" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancelClick" CausesValidation="false" />
It's a text box with a regular expression validator that's supposed to restrict the input to be only positive real numbers. When the text changes and it passes validation a textChanged() function is called.
If the text is invalid, textChanged() won't fire, and the save button won't work as well.
This works as intended.
I've also got a cancel button, and it's supposed to just clear the text box (and some other things server side). It should be able post back to the server whether or not the text box is valid, hence the CausesValidation="false" tag.
My problem is that when I enter something invalid into the text box, and then click cancel it actually executes textChanged() before btnCancelClick(). Even though it isn't valid.
This is a huge problem because textChanged() uses the text box text as a number, so the page throws an exception.
I have no idea what's causing textChanged() to be called. For one thing, the cancel button doesn't even call that function. And the regular expression validator should prevent the text box from calling it.
Edit: I've tried making some changes and experimenting.
function cancel()
{
document.getElementById('<%=textBox.ClientID%>').value = "";
}
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="cancel()" CausesValidation="false" />
I switched the cancel button to call a JavaScript method to try to clear the text box. The results are kind of interesting.
When an invalid string is entered as the first thing, then the cancel button works fine. It clears the text box without textChanged() being called.
If I type in a valid number, textChanged() is called as I expect. But when I click the cancel button it does call cancel(), and immediately after that it calls textChanged() again, but now the text box is empty, leading to an exception.
A similar thing happens if I enter something valid, and then make it invalid. textChanged() is called for the valid input. When the incorrect input is added, the validator prevents textChanged() from being called. But when I click cancel it goes from cancel() to textChanged().
I was thinking that maybe it was because validators don't persist through postback, but with the JavaScript function cancel() the button can't cause the page to postback. textChanged() has to be what's causing it, but I have no idea how.
Edit 2: I'm not going to mark this solved, since it's still a huge problem in my opinion, but I have found a workaround.
<script>
function cancel() {
document.getElementById('<%=textBox.ClientID%>').value = "";
return false;
}
</script>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="return cancel();" CausesValidation="false" />
This doesn't allow the button to cause a postback under any circumstances. It's fixing something that shouldn't be there in the first place, but there it is.
This helps with the button click, but not with other things. I've got a GridView with an OnSelectedIndexChanged function server-side. It also causes the page to post back with the textChanged() function. I could try to create a JavaScript version of the GridView function and fix it like the cancel button, but at this stage I'll probably remove the OnTextChanged event handler and put the textChanged() function and put it behind a button.
add causesvalidation="true" works for me on the textbox control.
So close... What's needed is simply setting a dedicated name to the ValidationGroup name on the Cancel button. This will cause it to be processed out of cycle with the rest of the validation. Not setting it effectively puts it in the "default" validation group which behaves (as you've seen), a bit oddly.
To allow the Cancel button to be exempt from the rest of the form validation, the rule I generally apply is [ValidationGroupName]Cancel to ensure it's triggered outside that block of validation. In this case, setting the Cancel button to something like;
<asp:Button ID="btnCancel" runat="server" Text="Cancel" ValidationGroup="valGroupCancel" OnClick="btnCancelClick" />
Should give you your expected behaviour.

I want iif conditiomn in my radio button

I want to put if condition in my radio button.
I have wrote this code but it does not give me result as I want. you will get idea from my code what actual I need to do. I have loaded user control two time in a single page so I want to call Java Script in page according to control.
<asp:RadioButton runat="server" GroupName="Pricing" class="2deditable iscreatedbydealer isinprivatelabel" onClick='<%this.ID=="ucPricing_Details_Sale"? "setSalePopupRetailPrice();":"setClearancePopupRetailPrice();"%>'
ID="rbManual" />
I think you can dynamic add the onclick event for the control in page load event of server side.
e.g
if("ucPricing_Details_Sale".Equals(this.ID))
{
this.rbManual.Attributes.Add("onclick", "setSalePopupRetailPrice();");
}
else
{
this.rbManual.Attributes.Add("onclick", "setClearancePopupRetailPrice();");
}
The on OnClick event will run on server side. Try using the OnClientClick event instead.
Edit: Deleted my last statement. Mistook me about the context of the call.
Also added code sample:
<asp:RadioButton runat="server"
OnClientClick='<%this.ID=="ucPricing_Details_Sale"?[...]"%>'
ID="rbManual" />

Where to write JavaScript to Validate updating record in GridView?

I am using *GridView1_RowCommand* event method to Insert, Update record. I want to use JavaScript clientside validation to validate record in update mode. Where can I write JavaScript to make it fire when clicked on the update button?
If you are familiar (know-how of) with the JavaScript language, you can write JavaScript in .aspx (markup) or external JavaScript file particularly. If not than use ASP.NET validation controls. Note that to validate user input you must have to convert GridView fields to "TemplateFields".
I would use a TemplateField, and put the update button in the EditItemTemplate. Assign the correct CommandName to link up to the RowCommand event, and use OnClientClick to trigger validation.
<EditItemTemplate>
<asp:Button ID="btnUpdate" runat="server" CommandName="Update" OnClientClick="validateData(this);" />
</EditItemTemplate>

asp.net textarea postback before submit button

I have an ASP.NET form with a few controls and a submit button at the bottom, all inside an update panel:
<asp:UpdatePanel runat="server" ID="upContent">
<ContentTemplate>
<asp:TextBox runat="server" ID="tbxMyTextBox" AutoPostBack="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return doStuff()" OnClick="btnSubmit_Click" />
</ContentTemplate>
</asp:UpdatePanel>
If I write something in the TextBox and click 'submit' immediately (without clicking out of the TextBox first), the changes are not recorded (as seen in the server-side event handler). However, if I write something in the TextBox, and then change focus to another control, an AutoPostback happens through the UpdatePanel and then clicking 'submit' does recognize these changes. How can I force this content to update right as I click the submit button, while still running both the clientside and serverside events attached to it? Thanks!
Is is possible that your text-box gets cleared due to some on-submit/on-click event handler? I will suggest you do use some tool such as Fiddler to inspect the POST data in request. Or you can put a break-point at your server side code and inspect the request data. Look for particularly Request.Form[tbxMyTextBox.UniqueID] - i.e. look for presence of value for your text-box's name (name property at client side which corresponds to UniqueID property at server side). If the request contains the value typed in the text-box then something is happening on server side but good news is you can always retrieve the value from Request object. If the value is not present in the Request object then it means that client side code is clearing the value before the submit.
If an onclick method returns false, it cancels the action that the button would normally perform. As your button would normally submit your form, but it appears not to be submitting, your client side javascript code in doStuff is probably returning false.

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources