We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Selected row

I am trying to use the content of a selected row in a function on the .cs, but I can't find a way. Can you please help me?
I have a grid and a button, and when I press the button, I want to had a record to a table different from the one that feeds the grid.

            <ej:grid runat='server' ID="sgEmCurso" DataSourceCachingMode="None" MinWidth="0" AllowPaging="True" BackColor="Red" BorderColor="Black" ForeColor="White">
                <ClientSideEvents RowSelected="rowSelected" />
                <Columns>
                    <ej:Column Field="IDProjecto" Visible="False">
                    </ej:Column>
                    <ej:Column Field="NomeProjecto" HeaderText="Nome Projecto">
                    </ej:Column>
                    <ej:Column Field="NomeArtigo" HeaderText="Nome Artigo">
                    </ej:Column>
                    <ej:Column Field="DataAbertura" Format="{0:dd/MM/yyyy}" HeaderText="Data Abertura">
                    </ej:Column>
                    <ej:Column Field="DescricaoTipoProjecto" HeaderText="Tipo Projecto">
                    </ej:Column>
                </Columns>
                <PageSettings Template=""></PageSettings>
                <ScrollSettings EnableTouchScroll="False"></ScrollSettings>
        </ej:grid>


        protected void sbtButton_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e)
        {
            var grid = sgEmCurso;

        }


5 Replies

SR Sellappandi Ramu Syncfusion Team January 11, 2016 07:23 AM UTC

Hi Manuel,

Thanks for contacting Syncfusion support.

While using button sever click event, we can get the button model in server side can’t get the grid model. So we need to pass the selected records before trigger the server side click event. We suggest you to use Ajax method in clientsideonclick event to get the selected records in server side.

Please refer to the code example and sample,

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <ej:button id="ButtonNormal" runat="server" type="Button" clientsideonclick="Click" size="Normal" text="Click"></ej:button>

    <ej:grid id="FlatGrid" runat="server" allowpaging="True">

        <Columns>

               <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="Right" Width="90">                  

               </ej:Column>

               <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="90">                  

               </ej:Column>              

               <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="110"></ej:Column>

               <ej:Column Field="ShipCity" HeaderText="ShipCity" Width="90" />

            </Columns>

         <PageSettings Template="" ></PageSettings>

    </ej:grid>

    <script type="text/javascript">

        function Click(element) {

            var gridObj = $("#<%=FlatGrid.ClientID%>").ejGrid("instance");

            if (gridObj.getSelectedRecords().length != 0) {

                var customerID = gridObj.getSelectedRecords()[0];

                $.ajax({

                    url: "/Default.aspx/Data",

                    type: "POST",

                    contentType: "application/json; charset=utf-8",

                    dataType: "json",

                    data: JSON.stringify({ data: customerID }),

                    success: function (data) {

                        alert(data.d);

                    }

                })

            }

            else

                alert("No record selected")

        }
    </script>
[Aspx.cs]

[System.Web.Services.WebMethod]

        public static object Data(Orders data)

        {

            //perform operation

            return "";// return the data
        }


Sample: http://www.syncfusion.com/downloads/support/forum/121627/ze/Sample_121627680167393

Refer to the online help documentation for getSelectedRecords(),

Document: http://help.syncfusion.com/js/api/ejgrid#methods:getselectedrecords


Screen shot to get the selected records in server side.



Regards,
Sellappandi R


MS Manuel Santos January 11, 2016 09:32 AM UTC

As you might see in the code I sent, I have an hiden cell, which is the first one. Is it possible to use it's content?


MF Mohammed Farook J Syncfusion Team January 12, 2016 09:18 AM UTC

Hi Manuel,

The getSelectedRecords() method of the Grid returns all the data including the hidden columns. Please find the code example.

<ej:grid id="FlatGrid" runat="server" allowpaging="True">

        <Columns>

               <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="Right" Width="90">                  

               </ej:Column>

               <ej:Column Field="CustomerID" Visible="false" HeaderText="Customer ID" Width="90">                  

               </ej:Column>             

                   . . .

    </ej:grid>

    <script type="text/javascript">

        function Click(element) {

            var gridObj = $("#<%=FlatGrid.ClientID%>").ejGrid("instance");

            if (gridObj.getSelectedRecords().length != 0) {

                var customerID = gridObj.getSelectedRecords()[0];

                $.ajax({

                    url: "/Default.aspx/Data",

                    type: "POST",

                    contentType: "application/json; charset=utf-8",

                    dataType: "json",

                    data: JSON.stringify({ data: customerID }),

                    success: function (data) {

                        alert(data.d);

                    }

                })

            }

            else

                alert("No record selected")

        }
    </script>

[Aspx.cs]

        [System.Web.Services.WebMethod]

        public static object Data(Orders data)

        {

            //perform operation

            return "";// return the data
        }



For your convenience we have prepared a sample that can be downloaded from the following  link:
Sample: http://www.syncfusion.com/downloads/support/directtrac/149498/ze/Sample1827275800

Refer to the  screen shot to get the selected records in server side:




Please let us know if you have any queries.
Regards,
J.Mohammed Farook


MS Manuel Santos January 16, 2016 02:40 AM UTC

Isn't there any other way? I need to use in the c# function the value of some textboxes and I don't find a way to get those values.


MF Mohammed Farook J Syncfusion Team January 18, 2016 01:46 PM UTC

Hi Manuel,

Based on your request we have created a sample and it can be downloaded from the following link:

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample2109893463.zip


Please find the code snippet.

[aspx]

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

   <asp:TextBox id="cus_input" runat="server"></asp:TextBox>

    <ej:button id="ButtonNormal" runat="server" type="Button" clientsideonclick="Click" size="Normal" text="Click"></ej:button>

    <ej:grid id="FlatGrid" runat="server" allowpaging="True">

        <Columns>

               . . .

            </Columns>

         . . .

    </ej:grid>

    <script type="text/javascript">

        function Click(element) {

            var gridObj = $("#<%=FlatGrid.ClientID%>").ejGrid("instance");

            if (gridObj.getSelectedRecords().length != 0) {

               

                var customerID = gridObj.getSelectedRecords()[0];

               

                customerID.textValue = $('#<%= cus_input.ClientID %>').val();

                $.ajax({

                    url: "/Default.aspx/Data",

                    type: "POST",

                    contentType: "application/json; charset=utf-8",

                    dataType: "json",

                    data: JSON.stringify({ data: customerID }),

                    success: function (data) {

                        alert(data.d);

                    }

                })

            }

            else

               alert("No record selected")

        }

    </script>
</asp:Content>

[aspx.cs]

   public static object Data(Object data)

        {

            //perform operation

            return "";// return the data
        }



In above sample we create “asp text box” in this sample.  We have append the textbox value to getSelectedRecord() array. Please find the code snippet.


                var customerID = gridObj.getSelectedRecords()[0];

               

                customerID.textValue = $('#<%= cus_input.ClientID %>').val();





Please refers the screen shot:


Regards,
J.Mohammed Farook




Loader.
Up arrow icon