I want to collect action response using JS in ASP.NET C#. So on an ASP.NET Button click I am doing following
Button on ASPX page
<asp:Button ID="btn_add" runat="server" CssClass="button btn-entry"
Text="Add" OnClick="btn_add_Click"
Inside btn_add_Click , I have
bool continue_ = isEmpty_InputForm(input_form_index);
if (!continue_)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"onclick",
"<script type=\"text/javascript\">"
+ " var action_res = document.createElement(\"INPUT\");"
+ " action_res.type = \"hidden\";"
+ " action_res.name = \"action_res\";"
+ " action_res.value = null;"
+ " if (confirm('Are you sure?')) {"
+ " action_res.value = 'Yes';"
+ " }"
+ " document.forms[0].appendChild(action_res);"
//+ "confirm('Are you really sure?');"
+ " </script>",
false);
}
Then to collect the value of action_res
JS var I am doing following
string action_res = Request.Form["action_res"];
The page gives me the confirm popup just fine, but action_res
value is always null
!
I am new to JS, can someone help me spotting the error please.
Think about it this way - if the posted page is generating that script, the server code is not waiting for the information from that confirmation dialog. It's always going to be null because the server doesn't know about
var action_res
; it wasn't establish before post back. Maybe handle the confirmation on the client side with JavaScript, set a hidden input up on your page (input id="confirmation_res" type="hidden" runat="server" />
) and callstring action_res = action_res.Value;
in your C# code?