Reputation: 11
I have created checkboxes dynamically from list of checkboxes and i saved this list into a session and want to get this List of checkboxes from session in checkbox's onChange event. Here is the code.
public static List <CheckBox> chklist = new List <CheckBox> ();
void arrinit() {
for (int i = 0; i < 31; i++) {
//initializing list of checkboxes
chklist.Add(new CheckBox());
}
}
void show() {
for (int i = 0; i < 30; i++) {
TableCell cell4 = new TableCell();
tRow.Cells.Add(cell4);
((IParserAccessor) cell4).AddParsedSubObject(chklist[i]);
chklist[i].ID = "cbx_" + i.ToString();
string a = "processChechBox('" + "ctl00_ContentPlaceHolder1_" + chklist[i].ID + "'); return false;";
chklist[i].Attributes.Add("onChange", a);
chklist[i].Attributes.Add("runat", "server");
}
Session["chk"] = chklist;
}
function processChechBox(id) {
//here is the javascript function for checkbox onChange event
debugger;
var containerRef = document.getElementById(id);
var data = document.getElementById(id);
data.value = '1';
data.checked = true;
var a = '<%= Session["chk"]%>';
}
var a = '<%= Session["chk"]%>';
this line is returning System.Collections.Generic.List1[System.CheckBox]
instead of list
processChechBox(id)
this function is called on every check box checked.
Upvotes: 0
Views: 1713
Reputation: 192
Try with below approach and check what you got in console log.
function processChechBox(id) {
//here is the javascript function for checkbox onChange event
debugger;
var containerRef = document.getElementById(id);
var data = document.getElementById(id);
data.value = '1';
data.checked = true;
var list = <%= new JavaScriptSerializer().Serialize(Session["chk"]) %>;
console.log(list);
}
Upvotes: 0
Reputation: 2254
This line
<%= Session["chk"]%>
Will write out the equivalent of
Session["chk"].ToString()
which obviously is not what you want here. What do what to accomplish with the 'var a' used here?
My guess is you really want something like this
<% var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); %>
var jsVariable = <%= serializer.Serialize(((List<CheckBox>)Session["chk"]).ToArray()) %>;
Credit to: Pass C# ASP.NET array to Javascript array
Upvotes: 0