Click here to Skip to main content
16,005,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to send Java script Array values to C# code behind file


I have Check Boxes in Dev Express gridview and and now on client click event I want to
take selected check box values in java script array. and then on button click I want
to send that values to code behind using json ajax call.

But It is giving Error Message.

What I have tried:

ASPX Code For Gridview With Check Box

<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" KeyFieldName="AccId"
    ClientInstanceName="grid">
    <Columns>
        <dx:GridViewCommandColumn ShowSelectCheckbox="true"></dx:GridViewCommandColumn>
        <%-- <dx:GridViewDataTextColumn>
            <DataItemTemplate>
                <dx:ASPxCheckBox ID="cbCheck" runat="server" Visible="true" AutoPostBack="true"></dx:ASPxCheckBox>
            </DataItemTemplate>
        </dx:GridViewDataTextColumn>--%>
        <dx:GridViewDataTextColumn FieldName="AccId" ReadOnly="True" VisibleIndex="1">
            <EditFormSettings Visible="False" />
        </dx:GridViewDataTextColumn>
        <dx:GridViewDataTextColumn FieldName="AccessName" VisibleIndex="0">
        </dx:GridViewDataTextColumn>
    </Columns>
    <ClientSideEvents SelectionChanged="grid_SelectionChanged" />
</dx:ASPxGridView>


Then Bellow this button control as

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>"
           SelectCommand="GetUserAccess" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
       <input id="Button1" type="button" value="GetData" onclick="myfun()" />


Java Script Function in Head Section as follows

<script type="text/JavaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        // alert('c');
        var myval = [];
        function grid_SelectionChanged(s, e) {
            alert("Click");
            s.GetSelectedFieldValues("AccId", GetSelectedFieldValuesCallback);
        }
        function GetSelectedFieldValuesCallback(values) {
           alert(values);
            try {
                myval.push(values);
            }
            finally { }
            
        }

        function myfun() {
            $.ajax({
                type: "POST",
                url: "WebForm1.aspx/MyMethod", // the method we are calling
                contentType: "application/json; charset=utf-8",
                //data: { Param1: myval },
                data: "{ Param1: '" + myval + "'}",
                dataType: "json",
                success: function (result) {
                    alert('Yay! It worked!');
                },
                error: function (result) {
                    alert('Oh no :(');
                }
            });

        }
</script>


code behind file Method(.cs files) as

[WebMethod]
       public static void MyMethod(string []Param1)
       {
           try
           {
               //Do here server event
           }
           catch (Exception)
           {
               throw;
           }
       }


but it is giving Error as
Oh no


plese help me to solve this
Thanks in advance
Posted
Updated 18-Dec-17 16:29pm

1 solution

try

   var obj = { Param1: myval } 

         $.ajax({
             type: "POST",
             url: "WebForm1.aspx/MyMethod", // the method we are calling
             contentType: "application/json; charset=utf-8", 
             data: JSON.stringify(obj),
             dataType: "json",
             success: function (result) {
                 alert('Yay! It worked!');
             },
             error: function (result) {
                 alert('Oh no :(');
             }
         });
 
Share this answer
 
v2

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