如何为每个gridview行定义一个javascript数组

本文关键字:一个 数组 javascript gridview 定义 | 更新日期: 2023-09-27 18:19:42

我有一个gridview,我想使用javascript数组将每个gridview行的用户权限传递给客户端,我在gridview行数据绑定事件中为此写了以下代码:

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);

但这段代码只保留最后一行的值。我怎么能每行都有这个数组?我如何根据gridview行索引从这个数组中读取?

如何为每个gridview行定义一个javascript数组

您可以尝试从数据源获取用户权限,这比尝试从gridview获取权限更容易,然后向客户发送相同的"RegisterClientScriptBlock"

或者,你可以将它们采用与网格视图相同的客户端选择器样式,假设你可以调用网格视图的"用户"可以使用jquery 来实现这一点

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

这样就可以得到gridview的所有行,你可以用for循环进行操作。。

使用数据集的数据源是什么?

好的,我用以下方式解决了我的问题:

首先,我用javascript定义了一个数组:(一个二维数组):

var permissions=[];

然后我将我的rowDataBound代码更改为:

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 

实际上,我为所有行添加了行权限的数组。现在我可以根据gridview的rowIndex获得每一行的权限。例如,permissions[0]是指第一行权限。