I have an asp button (the code is below). I have the event handler working, but I want to pass a string (which is the result of a javascript function) into the event handler function. Is there a way to do that?
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" Text="Run" />
Normally the way you would do this is to have you javascript modify an input control so that the value will be posted back with the
Buttonclick. If you want the user to visually not be able see the data that your javascript is modifying to post back then you can write it to aHiddenField.You can use the
OnClientClickto setup the javascript to run before the postback happens to ensure your modified value is put into the input control. Eg:So you just need to swap the call to
YourFunctionToAddStringToControlwith your modified javascript function that is writing the value to aHiddenFieldor another input control. Then update your server side click handler to access the field.Side Note: When using javascript to write to server side controls such as
HiddenField, don’t forget to use<%= yourHiddenField.ClientID %>to get the correct control name.