Dynamically setting the "editoption" 'value' field for a select box in jqgrid with AJAX call

Posted by imomins on September 21, 2011 at 11:05 AM

<script type="text/javascript">

 

         $(function() {

             var data1 = '';

             var tmp = 'Select:Select Course;';

                 $.ajax({

                 type: "POST",

                 url: "AddAEP.aspx/GetCourses",

                 data: {},

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

                 dataType: "json",

                 success: function(msg) {

                     data1 = msg.d.split(";");

                     for (var i = 0; i <= data1.length - 1; i++) {

                         tmp = tmp + data1[i] + ";";

                       

                     }

                     tmp = tmp.substring(0, tmp.length - 1);

                    

                 }

             });

             $("#InstructorGrid").jqGrid({

                 url: '../Handler/InstructorHandler.ashx',

                 datatype: 'json',

                 height: 300,

                 width: 1000,

                 colNames: ['InstructorID', 'Date', 'CourseName', 'CourseNumber', 'Instructor', 'Comments', 'Actions'],

                 colModel: [

                        { name: 'InstructorID', index: 'InstructorID', width: 100, sortable: false, hidden: true },

                        { name: 'Date', width: 100, sortable: false, editable: true, editrules: { required: true, date: true, datefmt: 'd-M-Y'} },

                        { name: 'CourseName', width: 150, editable: true, editrules: { required: false }, edittype: "select", formatter: "select", editoptions: { value: tmp} },

                        { name: 'CourseNumber', width: 100, editable: true, editrules: { required: false }, edittype: "select", editoptions: { dataUrl: "../Handler/Select.txt"} },

                        { name: 'Instructor', width: 100, editable: true, editrules: { required: false }, edittype: "select", editoptions: { value: "UserHandler.ashx/GetAllUserName1"} },

                        { name: 'Comments', width: 250, editable: true, edittype: "textarea", editoptions: { rows: "5", cols: "30"} },

                        { name: 'Actions', width: 100 }

                        ],

 

 

                 rowNum: 25,

                 rowList: [25, 35, 45],

                 pager: '#InstructorGridPager',

                 //sortname: 'StudentID',

                 viewrecords: true,

                 mtype: "GET",

                 editurl: '../Handler/InstructorHandler.ashx',

                 gridview: true,

                 sortorder: 'asc',

                 caption: 'Instructor',

                 //loadComplete: function(data) {

                 // alert("safdewrfe");

                 //$("#InstructorGrid").setColProp('CourseName', { editoptions: { value: "A:A"} });

                 //},

 

                 col: {

                     caption: "Show/Hide Columns",

                     bSubmit: "Submit",

                     bCancel: "Cancel"

                 }

             });

 

             $("#InstructorGrid").jqGrid('navGrid', '#InstructorGridPager', { edit: true, add: true, del: true });

 

             options = { autosearch: true };

             $("#InstructorGrid").filterToolbar(options);

 

         });

      

    </script>



WebMethod:

[WebMethod ]

    public static string GetCourses()

    {

        string Users = "";

        string connectionString = SQLHelper.getConnetionString();

        using (SqlConnection connection = new SqlConnection(connectionString))

        {

            using (SqlCommand command = new SqlCommand())

            {

 

                command.Connection = connection;

                //command.CommandText = "User_GETAllUser";

                command.CommandText = "SELECT CourseTitle FROM CourseCatalog";

 

                //command.CommandType = CommandType.StoredProcedure;

                connection.Open();

                using (SqlDataReader dataReader = command.ExecuteReader())

                {

                    string user;

                    while (dataReader.Read())

                    {

 

                        user = Convert.ToString(dataReader["CourseTitle"]);

                        Users += user + ":" + user + ";";

 

                    }

                    Users = Users.Substring(0, Users.Length - 1);

                }

 

            }

 

            return Users;

        }


 


Categories: JQUERY, ASP.NET

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

1 Comment

Reply imomins
11:14 AM on September 21, 2011 
Practical