1

I'm in a asp.net c# project. I want to print employees card with bar code. So I write a html tag in string variable call htmlCon and it will bind a DIV tag in client side (get all employees and loop it for print one by one). it work fine. but inside the htmlCon variable has java script function it will not run in loop.

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'/>" +        

"........................................"+
    }
}

this code comes with bar code and it will print first round in the loop..I want to run all employees for get bar code.

1 Answer 1

1

You are generating many images with the same ID, you should generate a new id for each loop iteration. I would also recommend using a StringBuilder instead of a bunch of string concatenations:

    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();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you.. I got the answer in this.. $('#barcode{0}').JsBarcode('{1}', { width: 1, height: 30 });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.