Submitting to JavaScript first and then ASP.NET?

333 Views Asked by At

I'm creating a User Control for a quick form. This need needs to store data in two places: one is a custom built (by someone else) database that uses JavaScript to submit the data, and the other is a CRM that uses .NET APIs to submit data.

My form currently submit the JavaScript side without problem, but I cannot get any .NET code to run.

An example of my code is:

nps.ascx

<html>
<head>
<script type='text/javascript'>
function doSubmit() {
  SubmitJavaScriptBasedData('title','stuff',$['[id$=comment]').val());
  $('[id$=comment].css('visibility','hidden');
  return true;
}
</script>
</head>
<body>
<asp:RadioButton ID="RB1" GroupName="Buttons" runat="server" /><br />
<asp:RadioButton ID="RB2" GroupName="Buttons" runat="server" /><br />
<asp:RadioButton ID="RB3" GroupName="Buttons" runat="server" /><br />
<asp:RadioButton ID="RB4" GroupName="Buttons" runat="server" /><br />
<p>
<asp:TextBox id="comment" runat="server" TextMode="MultiLine"></asp:TextBox>
<p>
<asp:Button id="submitBtn" runat="server" Text="Go!" OnClientClick="doSubmit();" OnClick="submitComment_Click"></asp:Button>
<p>
<asp:Label id="lblTest" runat="server"></asp:Label>
</body>
</html>

nps.ascx.cs:

...
protected void submitComment_Click(object sender, EventArgs e)
{
    lblTest.Text = "Hello World!";
}
...

When I click my button, I get the expected JavaScript things happening: my remote database is updated and the 'comment' textbox disappears. However, no .NET code is executed at all.

Can anyone help me understand why I'm not getting .NET stuff executed?

P.S. I re-wrote this code from memory and as an example - including all the (I think) relevant pieces) - so there may be mistakes during typing. I think the underlying issue, however, is specifically to do with submissions.

2

There are 2 best solutions below

1
On

Try this set of code.

<asp:Button id="submitBtn" runat="server" Text="Go!" OnClientClick="javascript:return doSubmit();" OnClick="submitComment_Click"></asp:Button>
1
On

Well, try with this (it's absolutely not a good solution, but it's only to try if it works)

<asp:Button ClientIdMode="Static" id="submitBtn" runat="server" Text="Go!" OnClientClick="doSubmit(); __doPostBack('submitBtn','');" OnClick="submitComment_Click"></asp:Button>