-1

当用户单击网格上的编辑按钮时,我将站点名称和订单 ID(隐藏值)传递给 javascript 函数。我收到一个站点名称的“未终止的字符串常量错误”。我不知道那有什么问题。请看下面的图片。在此处输入图像描述

protected void gvCktMap_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (Session["TaskID"] != null)
            {
                int intTskID = Convert.ToInt32(Session["TaskID"]);
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("class", "taskListRow");
                }
                CheckBox chkBoxOne = (CheckBox)e.Row.FindControl("chkSelect");
                Label lblREJ = (Label)e.Row.FindControl("lblREJ");
                // Label lblDesc = (Label)e.Row.FindControl("lblDesc");
                Label lblStatus = (Label)e.Row.FindControl("lblTaskStatus");
                Label lblDaysUntillPTD = (Label)e.Row.FindControl("lblDaysUntilPTD");
                Label lblPastDue = (Label)e.Row.FindControl("lblPastDue");
                if (lblStatus != null)
                {
                    if (lblStatus.Text.Trim().ToLower() == "complete" || lblStatus.Text.Trim().ToLower() == "not_required")
                        chkBoxOne.Enabled = false;
                }
                //Do Not move this code...We are selecting orderids based on checkbox enable or not....//Babu//
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton lnkSiteName = (LinkButton)e.Row.FindControl("lnkSiteName");
                    Label lblServiceOrderNumber = (Label)e.Row.FindControl("lblServiceOrderNumber");
                    LinkButton lnkCMNumber = (LinkButton)e.Row.FindControl("lnkCORECMNumber");
                    LinkButton lnkSiteCMTicketNumber = (LinkButton)e.Row.FindControl("lnkSiteCMTicketNumber");
                    LinkButton lnkSMNumber = (LinkButton)e.Row.FindControl("lnkSMTicketNumber");
                    LinkButton lnkSiteSMTicketNumber = (LinkButton)e.Row.FindControl("lnkSiteSMTicketNumber");
                    Label lblorderId = (Label)e.Row.FindControl("lblorderId");
                    Label lblsId = (Label)e.Row.FindControl("lblsId");
                    Label lblsiteId = (Label)e.Row.FindControl("lblsiteId");
                    Label lbllob = (Label)e.Row.FindControl("lblLOB");
                    Label lblclarifyAccountNumber = (Label)e.Row.FindControl("lblclarifyAccountNumber");
                    Label lblTaskStatus = (Label)e.Row.FindControl("lblTaskStatus");
                    LinkButton lnkCustomerName = (LinkButton)e.Row.FindControl("lnkCustomerName");
                    LinkButton lnkProjectName = (LinkButton)e.Row.FindControl("lnkProjectName");
                    LinkButton lnkRoeID = (LinkButton)e.Row.FindControl("lnkPreSaleAssociation");
                    ImageButton detailsbutton = (ImageButton)e.Row.FindControl("btnDetails");
                    LinkButton lnkpendingdependencies = (LinkButton)e.Row.FindControl("lnkPendingDependencies");
                    LinkButton lnknotes = (LinkButton)e.Row.FindControl("lnkNotes");
                    LinkButton lnkuserpendingdependencies = (LinkButton)e.Row.FindControl("lnkUserPendingDependences");
                    LinkButton lnkPendJeopCount = (LinkButton)e.Row.FindControl("lnkPendJeopCount");
                    LinkButton lnkPreSaleAssociation = (LinkButton)e.Row.FindControl("lnkPreSaleAssocation");
                    LinkButton lnktaskowner = (LinkButton)e.Row.FindControl("lnkTaskOwner");
                    LinkButton lnkConfigs = (LinkButton)e.Row.FindControl("lnkConfigFiles?");
                    LinkButton lnkCircuits = (LinkButton)e.Row.FindControl("lnkCircuits");
                    LinkButton lnkOptics = (LinkButton)e.Row.FindControl("lnkOptics");
                    ImageButton btnMixedorder = (ImageButton)e.Row.FindControl("btnMixedOrd");
                    ImageButton edit_button = (ImageButton)e.Row.FindControl("edit_button");
                    if (edit_button != null)
                        edit_button.OnClientClick = "StoreOrderIDAndLOB('" + lblorderId.Text + "','" + lbllob.Text + "')";

                    if (lnkConfigs != null)
                        lnkConfigs.OnClientClick = "StoreConfigs('" + lblsId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";

                    if (lnktaskowner != null)
                        lnktaskowner.OnClientClick = "StoreTaskOwner('" + lnkSiteName.Text.Replace("'", "&apos") + "','" + lnktaskowner.Text.Replace("'", "&apos") + "','" + lblorderId.Text + "','" + lbllob.Text + "','" + lblTaskStatus.Text + "')";

                    if (lnkSiteName != null)
                        lnkSiteName.OnClientClick = "StoreLnkSiteName('" + lblorderId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";

                    if (detailsbutton != null)
                        detailsbutton.OnClientClick = "Storedetails('" + lnkSiteName.Text.Replace("'", "&apos") + "')";

                    if (lnkCMNumber != null)
                        lnkCMNumber.OnClientClick = "StoreCMNumber('" + lnkCMNumber.Text + "', '" + lblorderId.Text + "')";

                    if (lnkSiteCMTicketNumber != null)
                        lnkSiteCMTicketNumber.OnClientClick = "StoreSiteCMTicketNumber('" + lnkSiteCMTicketNumber.Text + "', '" + lblorderId.Text + "')";

                    if (lnkSMNumber != null)
                        lnkSMNumber.OnClientClick = "StorelnkSMNumber('" + lnkSMNumber.Text + "', '" + lblorderId.Text + "')";

                    if (lnkSiteSMTicketNumber != null)
                    {
                        lnkSiteSMTicketNumber.OnClientClick = "StorelnkSiteSMTicketNumber('" + lnkSiteSMTicketNumber.Text + "', '" + lblorderId.Text + "')";
                    }
                    if (lnkpendingdependencies != null)
                    {
                        lnkpendingdependencies.OnClientClick = "StoreOrderId('" + lblorderId.Text + "')";
                    }
                    if (lnknotes != null)
                    {
                        lnknotes.OnClientClick = "StoreNotes('" + lblorderId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkuserpendingdependencies != null)
                    {
                        lnkuserpendingdependencies.OnClientClick = "StoreUserpendingdependencies('" + lblorderId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkPendJeopCount != null)
                    {
                        lnkPendJeopCount.OnClientClick = "StorePendJeopCount('" + lblorderId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkProjectName != null)
                    {
                        lnkProjectName.OnClientClick = "Storeprojectname('" + lnkProjectName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkCustomerName != null)
                    {
                        lnkCustomerName.OnClientClick = "StoreCustomername('" + lnkCustomerName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkPreSaleAssociation != null)
                    {
                        lnkPreSaleAssociation.OnClientClick = "StorePreSaleAssociation('" + lblorderId.Text.Trim() + "','" + lblsId.Text.Trim() + "','" + lblTaskStatus.Text.Trim() + "')";
                    }
                    if (lnkCircuits != null)
                    {
                        lnkCircuits.OnClientClick = "StoreCircuitsInfo('" + lblorderId.Text + "','" + lblsiteId.Text + "','" + lbllob.Text + "','" + lblclarifyAccountNumber.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";
                    }
                    if (lnkOptics != null)
                    {
                        lnkOptics.OnClientClick = "StoreOpticsInfo('" + lblsId.Text + "','" + lnkSiteName.Text.Replace("'", "&apos") + "')";
                    }
                    if (btnMixedorder != null)
                    {
                        btnMixedorder.OnClientClick = "StoreMixedorder('" + lblServiceOrderNumber.Text + "')";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            log4net.Config.XmlConfigurator.Configure();
            log.Warn("Logging:" + ex);
        }
    }

如何解决此错误?

function StoreOrderIDAndLOB(sender) {
//get handle to parent row:
var oRow = sender;
while (oRow && oRow.tagName.toLowerCase() !== "tr")
    oRow = oRow.parentNode;

//something might be wrong, sanity check:
if (!oRow)
    return;

//build array of all possible elements we have to look into:
var elements = [];
var arrSpans = oRow.getElementsByTagName("span");
for (var i = 0; i < arrSpans.length; i++)
    elements.push(arrSpans[i]);

//site name is the sender, grab its contents:
var siteName = "";
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    if (element.id.indexOf("lnkSiteName") >= 0) {
        siteName = element.innerHTML;
        break;
    }
}
siteName = sender.innerHTML;

//find the order ID by looking for matching element:
var orderId = "";
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    if (element.id.indexOf("lblorderId") >= 0) {
        orderId = element.innerHTML;
        break;
    }
}

//here you can use the values same way you use in the original code...
alert("site name: " + siteName + " and order ID is " + orderId);

}

4

2 回答 2

0

一个值包含双引号就足够了,整个事情都会中断,因为在这种情况下输出本身会被破坏。

不要将原始值发送给 JavaScript 函数,而是让函数找到它们需要的值。我将举一个函数的例子,你可以在所有其他函数中应用相同的逻辑,并拥有干净的代码而没有引号风险。

JavaScript:

function StoreLnkSiteName(sender) {
    //get handle to parent row:
    var oRow = sender;
    while (oRow && oRow.tagName.toLowerCase() !== "tr")
        oRow = oRow.parentNode;

    //something might be wrong, sanity check:
    if (!oRow)
        return;

    //build array of all possible elements we have to look into:
    var elements = [];
    var arrSpans = oRow.getElementsByTagName("span");
    for (var i = 0; i < arrSpans.length; i++)
        elements.push(arrSpans[i]);

    //site name is the sender, grab its contents:
    var siteName = sender.innerHTML;

    //find the order ID by looking for matching element:
    var orderId = "";
    for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        if (element.id.indexOf("lblorderId") >= 0) {
            orderId = element.innerHTML;
            break;
        }
    }

    //here you can use the values same way you use in the original code...
    alert("site name: " + siteName + " and order ID is " + orderId);
}

那么所需的 C# 就是:

lnkSiteName.OnClientClick = "StoreLnkSiteName(this)";

无需更换,无需担心报价。

现场测试用例

如果您决定使用 jQuery,JavaScript 代码可以大大简化,但也可以使用纯 JS 来实现。

于 2012-10-03T16:06:47.383 回答
0

这意味着您的参数传递不正确。你可以粘贴你的代码吗?您是否尝试过设置断点以查看通过了什么?

于 2012-10-03T15:37:13.297 回答