JavaScript函数在c# asp.net按钮点击事件不工作

JavaScript function in c# asp.net button click event not working

本文关键字:事件 工作 按钮 net 函数 asp JavaScript      更新时间:2023-09-26

我在一个asp.net c#项目。我想打印带有条形码的员工卡。所以我在字符串变量htmlCon中编写一个html标记,它将在客户端绑定一个DIV标记(获取所有员工并逐一循环打印)。它工作得很好。但是在htmlCon变量里面有java脚本函数,它不会在循环中运行。

protected void btnGenarate_ClickEvent(object sender, EventArgs e)
{  
    ......
    foreach (var item in empDetails)
    {
      htmlCon += ..........+
"<script>"+ 
"$(document).ready(function ()"+
"{ $('#barcode').JsBarcode('"+ item.EmployeeNo + "', { width: 1, height: 30 }); });"+
"</script>" +
"<img id='barcode' class='barcode'/>" +        
"........................................"+
    }
}

此代码带有条形码,它将在循环中打印第一轮..我想运行所有员工获取条形码。

您正在生成许多具有相同ID的图像,您应该为每个循环迭代生成一个新的ID。我还建议使用StringBuilder而不是一堆字符串连接:

    protected void btnGenarate_ClickEvent(object sender, EventArgs e)
    {             
        StringBuilder htmlCon = new StringBuilder();
        for (int i = 0; i < empDetails.Count; i++)
        {
            htmlCon.AppendFormat("<script>$(document).ready(function () { $('#barcode{0}').JsBarcode('{1}', { width: 1, height: 30 }); });</script><img id='barcode{0}' class='barcode'/>", 
                i.ToString(), empDetails[i].EmployeeNo);
            htmlCon.Append("........................................");
        }
        //To Use StringBuilder value
        string html = htmlCon.ToString();
    }