Hi
I have below code. OnClientClick Javascript gets executed . After that i want to execute below code
C#
BALBooks bALBooks = new BALBooks();
BALStudents bALStudents = new BALStudents();
StudentDetail Result = bALStudents.GetStudentDetailByLoginID(bALStudents.GetStudentIDByMobile(hdfMobile.Value));
ltrlHead.Text = Result.Name;
ltrlHead.Text = ltrlHead.Text + " : " + bALBooks.GetBookTitle(Convert.ToInt32(hdfBookId.Value));
BindData(Convert.ToInt32(hdfBookId.Value), Result.LoginCode);C#
protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType != DataControlRowType.DataRow)
return;
e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
for (int i = 8; i < e.Row.Cells.Count - 1; i++)
{
string cellValue = e.Row.Cells[i].Text;
var row = (DataRowView)e.Row.DataItem;
var currentPerson = row[0].ToString();
var currentDate = row.DataView.Table.Columns[i];
var commandArgument = currentPerson + ":" + currentDate;
LinkButton button = new LinkButton();
button.Text = cellValue;
button.CommandName = "Select";
button.CommandArgument = commandArgument;
button.OnClientClick = "GetModuleData(this)";
if (cellValue.ToUpper().Trim() == "A")
{
button.CssClass = "badge badge-danger";
}
}
}
}JavaScript
function GetModuleData(e) {
var currentRow = $(e).closest("tr");
var rp;
// rp = currentRow.find("td:eq(0)").text();
var table = document.getElementById("grdPlanning"), rIndex, cIndex, track;
// table rows
for (var i = 1; i 0) {
Mobile = $('table tr th:eq(' + (cIndex) + ')').text();
BookId = currentRow.find("td:eq(0)").text();
var str = Mobile;
const result = str.split(/[()]/);
var res1 = result[0];
var res2 = result[1];
document.getElementById('hdfBookId').value = BookId;
document.getElementById('hdfMobile').value = res2;
};
};
}
}
};Thanks

Rajkiran SwainRank #165 · 14.4K reputationPosted May 16, 2023, 4:27 AM EDT
To achieve this, you can modify the
GetModuleDatafunction in the JavaScript code to execute an AJAX request or redirect to another page where the C# code can be executed. Here's an example using AJAX:function GetModuleData(e) { // Existing code ... // Execute AJAX request to execute C# code $.ajax({ type: "POST", url: "YourPage.aspx/ExecuteCSharpCode", data: JSON.stringify({ bookId: BookId, mobile: res2 }), contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { // Handle the response if needed console.log("C# code executed successfully."); }, error: function(xhr, textStatus, errorThrown) { // Handle the error if needed console.log("Error executing C# code:", errorThrown); } }); }In your ASP.NET code-behind (C#), you need to create a web method to handle the AJAX request:
using System.Web.Services; // ... [WebMethod] public static void ExecuteCSharpCode(int bookId, string mobile) { BALBooks bALBooks = new BALBooks(); BALStudents bALStudents = new BALStudents(); StudentDetail Result = bALStudents.GetStudentDetailByLoginID(bALStudents.GetStudentIDByMobile(mobile)); // Update the necessary controls or perform other actions // ... BindData(bookId, Result.LoginCode); }Make sure to replace "YourPage.aspx" with the actual page where your code resides. Also, adjust the AJAX URL accordingly.
Note: If you're using ASP.NET Web Forms, make sure to enable Page Methods by adding
EnablePageMethods="true"to your<%@ Page %>directive.This way, when the
GetModuleDatafunction is executed, it will trigger the AJAX request toRamco RamcoRank #462 · 5.4K reputationPosted May 16, 2023, 4:50 AM EDT
Hi
It is giving error - Error parsing attribute 'enablepagemethods': Type 'LMS.Coordinator.AddRecommendedBooks' does not have a public property named 'enablepagemethods'.
function GetModuleData(e) { var currentRow = $(e).closest("tr"); var rp; // rp = currentRow.find("td:eq(0)").text(); var table = document.getElementById("grdPlanning"), rIndex, cIndex, track; // table rows for (var i = 1; i 0) { Mobile = $('table tr th:eq(' + (cIndex) + ')').text(); BookId = currentRow.find("td:eq(0)").text(); var str = Mobile; const result = str.split(/[()]/); var res1 = result[0]; var res2 = result[1]; alert(res1); alert(res2); document.getElementById('hdfBookId').value = BookId; document.getElementById('hdfMobile').value = res2; }; }; } } $.ajax({ type: "POST", url: "AddRecommendedBooks.aspx/getPopUp", /* *//*data: JSON.stringify({ bookId: BookId, mobile: res2 }),*/ contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { // Handle the response if needed console.log("C# code executed successfully."); }, error: function (xhr, textStatus, errorThrown) { // Handle the error if needed console.log("Error executing C# code:", errorThrown); } }); };Thanks