We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to create barcode image dynamically on an ASP.NET page?

Hi,

Sorry I delete this bacause of it's a wrong question!

Hello,

Thank you for considering Aspose.BarCode!

Could you tell us what symbology are you using, and the code text length?


Best regards!

We use Code128 and the CodeText length is 10 .

Hello,

Thank you for your post!
That’s really uncanny, could you send me a sample web project you are working on?
I’ve attached a web demo here:
http://www.aspose.com/products/aspose.barcode/tmp/sampleweb.zip

And the size of a normal Code128 barcode image of code text length 10 will be less than 10k ( of JPG format ):

screen shot

Best regards.

I think I didn't make myself clearly, sorry.

I bought Aspose.Barcode for Crystal report and it works fine.

This time I create an ASP.NET page for a report (some kind of "packing slip").

So I wrote following code:

protected void Page_Load(object sender, EventArgs e)
{
BarCodeWebControl bcw1 = new BarCodeWebControl("TEST01", Aspose.BarCode.Symbology.Code128);
bcw1.GenerateBarCodeImage();
Page.Controls.Add(bcw1);

bcw1 = new BarCodeWebControl("TEST02", Aspose.BarCode.Symbology.Code128);
Page.Controls.Add(bcw1);
}

I found that Page_Load is executed 3 times, 1 for the page and 2 times for the barcode images.

There're some business logic codes in the real program and the business logic is processed many times.

If I add javascript "window.print()" at the page. Page_Load is executed 2 more times. (Because there's 2 barcode images on the page.)

Do you have solution for this case?! I just want to generate the barcode image dynamicly and don't need to execute whole page logic.

Thank you!

Hello,

Thank you for your post!

Try adding this to the Page_Load method:

if (!IsPostBack)

{
BarCodeWebControl bcw1 = new BarCodeWebControl(“TEST01”, Aspose.BarCode.Symbology.Code128);
bcw1.GenerateBarCodeImage();
Page.Controls.Add(bcw1);

bcw1 = new BarCodeWebControl("TEST02", Aspose.BarCode.Symbology.Code128);
Page.Controls.Add(bcw1);
}

Or totally stop the Page_Load method by setting AutoEventWireup to false:
<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>

And override other method to do your logic:
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
//place your code here
}


Best regards!

I tried following code, but it didn't work! OnPreLoad is executed 3 times.

protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);

BarCodeWebControl bcw1 = new BarCodeWebControl("TEST01", Aspose.BarCode.Symbology.Code128);
bcw1.GenerateBarCodeImage();
Page.Controls.Add(bcw1);

bcw1 = new BarCodeWebControl("TEST02", Aspose.BarCode.Symbology.Code128);
Page.Controls.Add(bcw1);

}

!IsPostback flag is no use as well. My codes run 3 times.

Hello Jaylan,

I think I misunderstood the situation here, sorry for that.
Dynamically adding some controls such as:

Page.Controls.Add(new System.Web.UI.WebControls.Image());

And Page_Load did load twice because of this.

Now I could only suggest such a solution currently:

protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
writer.Write("<iframe src=“Image.aspx” />");
}

Overload the Render method, and add another page “Image.aspx”, and write the dynamic barcode generation logic in Image.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
BarCodeWebControl w = new BarCodeWebControl(“test1”, Aspose.BarCode.Symbology.Code128);
Page.Controls.Add(w);

w = new BarCodeWebControl(“test2”, Aspose.BarCode.Symbology.Code128);
Page.Controls.Add(w);
}

By that we could separate the business logic and the image generation logic, but that’s still not efficient. I will try to come up with a solution and keep you posted.

Best regards.

Hi! I just downloaded Aspose a few days ago, and I wanted to say that I’m experiencing the same problem.

I’ll give the “webpage that outputs an image” idea you posed above a try… but I see in the documentation that at one point there was a BarCode Image Handler class. The documentation’s pretty sparse, and it says it’s obsolete (which makes me hesitate to use it).

However I imagine that such a handler class would accomplish the desired task effectively (that is, preventing multiple firings of the page_load event and multiple re-postings of the page to render the barcodes).

Is it a bad idea to try and use these HttpHandler objects? If not, is there some more documentation on them and how they can be used? Thanks!

Hi EdgarVerona,

Thank you for your post!

The BarCodeWebControl supports 3 RenderMethods: FromCache, FromSession, FromHttpHandler. You can select the RenderMethods by setting the RenderMethod property. Considering the advantage of convenient usage, we do not suggest the HttpHandler method, but if you are farmiler with the processing of HttpHandler and it's easy to modify the Web.config file in your application, you can also apply the FromHttpHandler Render Method.

1st. Set the BarCodeWebControl.RenderMethod property to be FromHttpHandler.

2nd.Modify the Web.config as showing below:

...



...

Then the barcode images will be generated by the BarCodeImageHandler class and other behavior will be the same.

Thanks

Would that prevent the multiple re-loading of the page? I’ll give it a shot and report the results here.

If this’ll help prevent the page from re-loading multiple times, it’s worth the extra effort I think… as long as this capability won’t go away in a future release! =) I was hoping I could just check for if IsPostBack was true and not process in that situation, but it seems that whenever the barcode control needs to re-render it does a whole new post (i.e. not a Post-Back).

I have a scenario where there’s a variable quantity of barcodes depending on some passed in information. In order to do this, I use a repeater and set the barcode’s image on databind. It works, but because of the repeater/databind and the fact that the barcode control in its default setting reposts the site every time one changes, I found that it reposts somewhere along the lines of N ^ 2 times (N being the number of barcodes on the page).

I haven’t been able to test it accurately yet because once I do the databind, the line-by-line debug in Visual Studio goes CRAZY (I think it’s sending multiple re-post requests at once, which confuses Visual Studio), but it’s definitely more than just N times. For instance, if I end up generating 3 barcodes, the page seems to refresh/repost at least 9 times before it displays. I think it’s because since it’s not a PostBack event, the repeater re-binds again which causes the barcode controls to set their value again, which causes another refresh… why it doesn’t do it infinitely many times, I don’t know… but it stops after a while and displays the page properly. It just refreshes (and consequently, queries my database during the databind process) too many times for my liking.

But anyways, I’ll go try the HttpHandler method and see if that smooths out this situation. Be back once I’ve got it figured out. =)

Fantastic! Using the HttpHandler was EXACTLY what I was looking for, thanks!

It didn’t attempt to re-post the page at all when rendering the barcodes: just went straight to the HttpHandler to generate the image. I highly support keeping HttpHandler support in Aspose.Barcode, as it’s extremely useful and helpful. Woot!

On a small side note, I had to change the web.config line from “BarCodeImageService.axd” (as you put in your example) to “BarCodeImageHandler.axd”… it wasn’t rendering when it was pointing to BarCodeImageService.axd, and when I investigated I realized that the control was looking for a “BarCodeImageHandler.axdto process it, not BarCodeImageService.axd.

Once I changed that, it worked like a charm. Thanks! I think you guys should consider re-adding this information into your documentation, as I imagine there may be many people like me who’d like to use the Barcode control without having the page re-post multiple times.

Hi EdgarVerona,

Thank you for your information and also sorry for my mistake :)

We will re-active the section of using Httphandler into the next version of document, thanks again.

No worries! Was but a small thing. =) I’m glad to hear that you guys will be putting it back in as a feature, it works very well! =)