简体   繁体   中英

how can I define a javascript array per every gridview row?

I have a gridview and I want to pass user permissions per gridview row to client side using javascript array , I write below code for that in gridview row data bind event:

permissions+= "var permissions = ["; 
lbtnSendToApprove.Enabled = CheckPermission(document, project, user, null, eProjectAdminPermission.EditDocument);
                if (!lbtnSendToApprove.Enabled)
                {
                    lbtnSendToApprove.OnClientClick = string.Empty;
                    permissions += "Base64.encode('0' ) ,";
                }
                else
                    permissions += "Base64.encode('1' ) ,";

                lbtnReleaseDocument.Enabled = true;
                permissions += "Base64.encode('1' ) ,";

                lbtnAddSubversion.Enabled = CheckPermission(document, project, user, null, eProjectAdminPermission.EditDocument);
                if (!lbtnAddSubversion.Enabled)
                {
                    lbtnAddSubversion.OnClientClick = string.Empty;
                    permissions += "Base64.encode('0' ) ,";
                }
                else
                    permissions += "Base64.encode('1' ) ,";

                bool attachmentPermission = CheckPermission(document, project, user, null, eProjectAdminPermission.DetermineAttachment);
                if (!attachmentPermission)
                {
                    permissions += "Base64.encode('0' ) ];";
                }
                else
                    permissions += "Base64.encode('1' ) ];";
ScriptManager.RegisterClientScriptBlock(e.Row, e.Row.GetType(), "ContextMenuPermission", permissions , true);

but this code only keep last row values.how can I have this array per row? and how can I read from this array base on gridview row index?

you can try taking the user permissions from your data source, would be easier than trying to take permission from the gridview, and then you send the customer with the same "RegisterClientScriptBlock"

Or you can take them on the same client-side selector styles applied to the gridview, suppose "users" you can call your gridview Might do this with jquery

$ ('# users input tr [id ^ = permission]') 

and with that and would get all the rows of gridview and you could manipulate with a for loop ..

What data source using a dataset?

Ok I solve my problem with below way:

first I define an array in javascript : (a two dimenssion array):

var permissions=[];

then I change My rowDataBound Code to this:

permissions += "permissions.push(["; 
lbtnSendToApprove.Enabled = CheckPermission(document, project, user, null, eProjectAdminPermission.EditDocument);
                if (!lbtnSendToApprove.Enabled)
                {
                    lbtnSendToApprove.OnClientClick = string.Empty;
                    permissions += "Base64.encode('0' ) ,";
                }
                else
                    permissions += "Base64.encode('1' ) ,";

                lbtnReleaseDocument.Enabled = true;
                permissions += "Base64.encode('1' ) ,";

                lbtnAddSubversion.Enabled = CheckPermission(document, project, user, null, eProjectAdminPermission.EditDocument);
                if (!lbtnAddSubversion.Enabled)
                {
                    lbtnAddSubversion.OnClientClick = string.Empty;
                    permissions += "Base64.encode('0' ) ,";
                }
                else
                    permissions += "Base64.encode('1' ) ,";

                bool attachmentPermission = CheckPermission(document, project, user, null, eProjectAdminPermission.DetermineAttachment);
                if (!attachmentPermission)
                {
                    permissions += "Base64.encode('0' ) ]);";
                }
                else
                    permissions += "Base64.encode('1' ) ]);";
ScriptManager.RegisterStartupScript(upDocuments, upDocuments.GetType(),"ContextMenuPermission"+e.Row.RowIndex.ToString(), permissions , true);//set permissions 

in deed I add the row permission's array for all rows. now I can get every row permissions base on rowIndex of gridview. for example permissions[0] is refer to first row permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM