Click here to Skip to main content
14,870,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a dynamic HTML table using StringBuilder. But when I am trying to pass it's cell value to jQuery Ajax call, I am not getting the correct values. How will I get the cell values after focus out?

Here's the exact code snippet:
Dim table as new StringBuilder()
For each item in strategy

table.AppendLine(String.Format("<td>{0}</td>",item.id);

table.AppendLine("<td>{0}</td>",item.price);

table.AppendLine("<td contenteditable=""true"" onfocusout ="SaveVal({0},{1})"">{1}</td>",item.id, item.cellvalue );

Next

Return table.tostring ();


SaveVal Jquery method in .aspx file:

function SaveVal(id,cellvalue){
$.ajax({
type:"POST",
url:"Maintenance.aspx/SaveVal",
contentType:"application/json;charset=utf-8",
dataType:"json",
data: JSON.stringify({
ids: id,
val:cellvalue
}),
async:false,
success:function(response){
alert("Value Saved");
}
});
}




I want to get this content editable cell value after focus out. SaveVal(id,cellvalue) function is defined in jQuery Ajax call and I want to pass these 2 parameters id and cellvalue which is the final value of the cell- if there is no edit then-existing value and if we edit and type new value then the new value will be passed.

What I have tried:

Please see code snippet that I’ve tried so far.
Posted
Updated 15-Mar-21 22:59pm

1 solution

Quote:
VB.NET
table.AppendLine("<td contenteditable=""true"" onfocusout=""SaveVal({0},{1})"">{1}</td>", item.id, item.cellvalue)
You haven't quoted the values you're passing to your function. Unless both id and cellvalue are numbers, you will either get a syntax error, or they will be treated as references to undeclared variables.

You will also need to encode the values properly to avoid persisted cross-site scripting vulnerabilities and other errors. For ASP.NET (WebForms), use a combination of System.Web.HttpUtility.HtmlAttributeEncode and System.Web.HttpUtility.JavaScriptStringEncode for the attribute, since you need to encode for both contexts, and System.Web.HttpUtility.HtmlEncode for the cell value.
VB.NET
table.AppendLine("<td contenteditable=""true"" onfocusout=""SaveVal({0},{1})"">{2}</td>",
    HttpUtility.HtmlAttributeEncode(HttpUtility.JavaScriptStringEncode(item.id, true)),
    HttpUtility.HtmlAttributeEncode(HttpUtility.JavaScriptStringEncode(item.cellvalue, true)),
    HttpUtility.HtmlEncode(item.cellvalue));
   
Comments
Maciej Los 16-Mar-21 5:34am
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900