从后端代码向Javascript传递值时的变量命名

时间:2018-09-06 07:00:52

标签: javascript c#

我正在将doctype变量的值从我的C#代码传递给javascript。

现在我无法根据输入内容找到任何文件。

命名约定必须准确吗?

编辑:添加了当前版本,其中DocType硬编码在onclick JavaScript页面中。


变量: {em> ascx.cs

中的DOC_TYPE ascx

中的

DocType

AllGroup_UserControl.ascx.cs:

public partial class AllGroup_UserControl : UserControl
{
    ProductProvider provider = new ProductProvider();
    TBL_USER_PROFILEProvider uprovider = new TBL_USER_PROFILEProvider();
    DocumentProvider dprovider = new DocumentProvider();

    int DOC_TYPE;
    // Document Types
    const int G1_DOC_TYPE = 1;
    const int G2_DOC_TYPE = 2;
    const int G3_DOC_TYPE = 3;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string userName = SPContext.Current.Web.CurrentUser.Name;
            TBL_USER_PROFILE p = uprovider.GetUser(userName);
            if (p != null)
            {
                List<string> G1List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group 1");
                List<string> G2List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group 2");
                List<string> G3List = uprovider.GetAccessByModuleName(p.UserProfileID, "Group 3");

                if (G1List.Count != 0)
                {
                    DOC_TYPE = G1_DOC_TYPE;
                }
                else if (G2List.Count != 0)
                {
                    DOC_TYPE = G2_DOC_TYPE;
                }
                else if (G3List.Count != 0)
                {
                    DOC_TYPE = G3_DOC_TYPE;
                }
                else
                {
                    Response.Redirect("/SitePages/AccessDeny.aspx");
                }

                Page.DataBind();
            }
        }
    }

    public int DocType
    {
        get
        {
            return DOC_TYPE;
        }
    }

//rest of the code

AllGroup_UserControl.ascx:

<a href="#" runat="server" onclick="openDialog('/SitePages/FileDownload.aspx?DocType=<%# DocType %>&ItemNo=<%#Eval("StoreItemNo")%>&CustomerID=<%#Eval("CustomerID")%>')">

添加:

使用docType硬编码的工作代码:

Group1_UserControl.ascx.cs :(文档类型为硬编码)

public partial class Group1_UserControl : UserControl
{

    ProductProvider provider = new ProductProvider();
    TBL_USER_PROFILEProvider uprovider = TBL_USER_PROFILEProvider();
    DocumentProvider dprovider = new DocumentProvider();
    int docType = 100;

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            {
                string userName = SPContext.Current.Web.CurrentUser.Name;
                TBL_USER_PROFILE p = uprovider.GetUser(userName);
                if (p != null)
                {
                    List<string> alist = uprovider.GetAccessByModuleName(p.UserProfileID, "Group 1");
                    if (alist.Count == 0)
                        Response.Redirect("/SitePages/AccessDeny.aspx");
                }
            }
        }
    }

Group1_UserControl.ascx :(文档类型为硬编码)

<a href="#" onclick="openDialog('/SitePages/FileDownload.aspx?DocType=100&ItemNo=<%#Eval("StoreItemNo")%>&CustomerID=<%#Eval("CustomerID")%>')">

1 个答案:

答案 0 :(得分:0)

您必须为此使用AJAX。然后,您可以将值从后端传递到JavaScript。 下面是一个示例,

客户端脚本

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/YourFunctionHere",
        data: '{name: "' + DocType + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>

Web方法

    [System.Web.Services.WebMethod]
public static string YourFunctionHere(string name)
{
    return "return value here";
}