编辑和添加功能在jqgrid中不起作用的功能

时间:2016-02-27 12:17:26

标签: c# sql-server jqgrid

在存储过程的帮助下执行CRUD功能的类

public class InsertUpdateDeleteBLL
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
    SqlCommand com = new SqlCommand();

    public string InsertUpdateDeleteMethod(object param)
    {
        string returnValue = "";
        try
        {
            Dictionary<string,> editData = param as Dictionary<string,>;
            com.CommandText = "InsertUpdateDeleteSP";
            com.CommandType = CommandType.StoredProcedure;
            if (editData["oper"].ToString() == "del")
            {
                com.Parameters.AddWithValue("@UserID", editData["UserID"] == DBNull.Value ? 0 : editData["UserID"]);
                com.Parameters.AddWithValue("@Actiontype", editData["oper"].ToString());
            }
            else
            {
                com.Parameters.AddWithValue("@UserID", editData["UserID"] == DBNull.Value ? 0 : editData["UserID"]);
                com.Parameters.AddWithValue("@FirstName", editData["FirstName"].ToString());
                com.Parameters.AddWithValue("@LastName", editData["LastName"].ToString());
                com.Parameters.AddWithValue("@IsActive", editData["IsActive"].ToString());
                com.Parameters.AddWithValue("@Department", editData["Department"].ToString());
                com.Parameters.AddWithValue("@Email", editData["Email"].ToString());
                com.Parameters.AddWithValue("@IsSuperAdmin", editData["IsSuperAdmin"].ToString());
                com.Parameters.AddWithValue("@JobRole", editData["JobRole"].ToString());
                com.Parameters.AddWithValue("@UserName", editData["UserName"].ToString());
            }
            com.Connection = conn;
            conn.Open();

            int m = com.ExecuteNonQuery();
            if (m > 0)
            {
                if (editData["oper"].ToString() == "add")
                {
                    returnValue = "Record Added Successfully";
                }
                else if (editData["oper"].ToString() == "edit")
                {
                   returnValue = "Record Updated Successfully"; 
                }

                else if (editData["oper"].ToString() == "del")
                {
                    returnValue = "Record Deleted Successfully"; 
                }
                else
                { 
                    throw new Exception("there is problem");

                }
            }
        }
        catch (Exception ex)
        {
            returnValue = ex.Message.ToString();
        }
        finally
        {
            conn.Close();
        }

        return returnValue;
    }

//WebMethod

[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string EditUpdateDelete(object data)
{
    Helper.InsertUpdateDeleteBLL insertUpdateDelete = new Helper.InsertUpdateDeleteBLL();
    string returnMessage = insertUpdateDelete.InsertUpdateDeleteMethod(data);

    return returnMessage; 
}


<script type="text/javascript">
    $.ajax({
        type: "POST",
        contentType: "application/json",
        data: "{}",
        url: "MyTestPage.aspx/getData",
        dataType: "json",
        success: function (data) {
            data = data.d;
            $("#list").jqGrid({
                datatype: "local",
                colNames: ['UserID', 'First Name', 'Last Name', 'Is Active', 'Department', 'Email', 'Is Super Admin', "Job Role", 'Username'],
                colModel: [
                { name: 'UserID', index: 'UserID', width: 50, stype: 'text' },
                { name: 'FirstName', index: 'FirstName', width: 130, stype: 'text', sortable: true, editable: true },
                { name: 'LastName', index: 'LastName', width: 130, editable: true },
                { name: 'IsActive', index: 'IsActive', width: 60, editable: false },
                { name: 'Department', index: 'Department', width: 80, align: "centre", sortable: true, editable: true },
                { name: 'Email', index: 'Email', width: 150, align: "centre", editable: true },
                { name: 'IsSuperAdmin', index: 'IsSuperAdmin', width: 150, align: "middle", editable: true },
                { name: 'JobRole', index: 'JobRole', width: 150, sortable: true, editable: true },
                { name: 'Username', index: 'Username', width: 100, sortable: true, editable: true }
                ],
                data: JSON.parse(data),
                rowno: 10,
                loadonce: true,
                multiselect: true,
                rowlist: [5, 10, 20],
                pager: '#pager',
                viewrecords: true,
                gridview: true,
                sortorder: 'asc',
                toppager: true,
                editurl: 'MyTestPage.aspx/EditUpdateDelete',
                cloneToTop: true,
                async: false,
                altrows: true,
                autowidth: false,
                hoverrows: true,
                height: 200,
                // rownumbers: true,
                caption: "User Data"
            });

            $('#list').jqGrid('navGrid', '#pager',
            {
                edit: true,
                add: true,
                del: true,
                search: true,
                searchtext: "Search",
                addtext: "Add",
                edittext: "Edit",
                deltext: "Delete",
                cloneToTop: true
            },
            {
                //update
                recreateForm: true,
                reloadAfterSubmit: false,
                width: 500,
                closeAfterEdit: true,
                ajaxEditOptions: { contentType: "application/json" },
                serializeEditData: function (postData) {
                    var postdata = { 'data': postData };
                    return JSON.stringify(postdata);
                }
            },
            {
                // add function
                recreateForm: true,
                beforeShowForm: function (form) {
                    $('#tr_UserID', form).hide();
                },
                width: 500,
                reloadAfterSubmit: false,
                closeAfterAdd: false,
                ajaxEditOptions: { contentType: "application/json" },
                //onclickSubmit: function (eparams) { alert('Hi');},
                serializeEditData: function (postData) {
                    var postdata = { 'data': postData };
                    return JSON.stringify(postdata);
                }
            },
            {
                ajaxDelOptions: { contentType: "application/json" },
                reloadAfterSubmit: false,
                onclickSubmit: function (eparams) {
                    var retarr = {};
                    var sr = $("#list").getGridParam('selrow');
                    rowdata = $("#list").getRowData(sr);
                    retarr = { UserID: rowdata.UserID };

                    return retarr;
                },
                serializeDelData: function (data) {
                    var postData = { 'data': data };
                    return JSON.stringify(postData);
                }
            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            debugger;
        }
    });
</script>

0 个答案:

没有答案