0

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.

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

I copied and paste your code and its giving me exception: {"Exception has been thrown by the target of an invocation."} and the inner exception is: {"This operation requires IIS integrated pipeline mode."}
If you change the app pool to use integrated that should go away, not sure why its required.
0

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);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.