Then in the UpdateDataPrintStatus page, using the following code to get the parameter:
if (!IsPostBack)
{
string IDNumber = Request.QueryString["ID"].ToString();
//insert data into database.
}
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
[System.Web.Services.WebMethod]
public staticstring MyTestMethod(string name)
{
string str = string.Format("{0} button is click!", name);
return str;
}
Note, if the WebMethod is static, we cannot use Request.Form method, we could transfer data via the parameters.
Best regards,
Dillion
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
389 Points
965 Posts
How do I insert a database record from an onclick event in JavaScript?
Jun 22, 2017 11:42 AM|cms9651|LINK
Hi all, I need your appreciated help.
How do I insert database record (MySQL) in C# when JavaScript onclick event is triggered on the HTML link ?
My code below I don't have error but the record not inserted.
On the browser in HTML view of asp classic page the value of :
is correct :
Thank you in advance for any help.
Asp classic and javascript page :
UpdateDataPrintStatus.aspx.cs
Cheers,
Chevy Mark Sunderland
All-Star
35169 Points
9930 Posts
Moderator
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 22, 2017 01:54 PM|bbcompent1|LINK
Is the web control you are using have AutoPostback = "True" set? If not, that's why most likely.
All-Star
45479 Points
7008 Posts
Microsoft
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 23, 2017 05:52 AM|Zhi Lv - MSFT|LINK
Hi cms9651,
I suggest you could try to use the following code to redirect to the UpdateDataPrintStatus page:
Then in the UpdateDataPrintStatus page, using the following code to get the parameter:
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
389 Points
965 Posts
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 28, 2017 09:17 AM|cms9651|LINK
Thank you for replies.
I have tried with Ajax and jquery without success.
UpdateDataPrintStatus.aspx.cs
Cheers,
Chevy Mark Sunderland
Member
260 Points
71 Posts
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 28, 2017 06:18 PM|sakthivel0531|LINK
You should NOT write the Post method code in Page Load event. You need to create a web method for this.
UpdateDataPrintStatus.aspx.cs
Member
389 Points
965 Posts
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 29, 2017 10:42 AM|cms9651|LINK
Hi, thank ou for reply really appreciated.
Unfortunately, the records are not added, but on HTML webpage index.asp the value of data-id is correct :
And if try with this vaue in Ajax the record is not added :
index.asp
UpdateDataPrintStatus.aspx.cs
Cheers,
Chevy Mark Sunderland
All-Star
45479 Points
7008 Posts
Microsoft
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 30, 2017 06:12 AM|Zhi Lv - MSFT|LINK
Hi cms9651,
Please check your code, as far as I know, when we use JQuery Ajax to call the WebMethod, the method should be static.
You could refer to the following code:
$("#btnCallWP").click(function () { $.ajax({ type: "POST", url: "JQueryWebMethod.aspx/MyTestMethod", data: '{name: "' + "AAA" + '"}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert(response.d); }, failure: function (response) { alert(response.d); } }); return false; });
Code in WebMethod:
[System.Web.Services.WebMethod] public static string MyTestMethod(string name) { string str = string.Format("{0} button is click!", name); return str; }
Note, if the WebMethod is static, we cannot use Request.Form method, we could transfer data via the parameters.
Best regards,
Dillion
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
389 Points
965 Posts
Re: How do I insert a database record from an onclick event in JavaScript?
Jun 30, 2017 08:46 AM|cms9651|LINK
Thank you, I have try your code but I don't have change in aspx,cs page, I'm sorry.
Cheers,
Chevy Mark Sunderland