Sandeep Kumar

Sandeep Kumar

  • 1.1k
  • 684
  • 61.7k

How To fire click event on QR code,which conrol should i use

Dec 26 2022 6:17 AM

I want click on Qr code and to tale print of it.

Asp.net

<asp:GridView ID="grdQrCodeResult" runat="server" AutoGenerateColumns="false" OnRowDataBound="grdQrCodeResult_RowDataBound" OnSelectedIndexChanged="grdQrCodeResult_SelectedIndexChanged">
    <Columns>
         <asp:TemplateField HeaderText="Port" HeaderStyle-HorizontalAlign="Center">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblPortName" runat="server" Text='<%#Eval("Port") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Lots#" HeaderStyle-HorizontalAlign="Center">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblHatchName" runat="server" Text='<%#Eval("Lots") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="WMT">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                 <asp:Label ID="lblHatch1" runat="server" Text='<%#Eval("WMT") %>' SkinID="GridLabel1"></asp:Label>
             </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="QR Code">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                <asp:PlaceHolder ID="plBarCode" runat="server" />
             </ItemTemplate>
         </asp:TemplateField>
                  
         <asp:TemplateField HeaderText="QR Code1">
             <HeaderStyle HorizontalAlign="Center" Wrap="false" />
             <ItemStyle HorizontalAlign="Center" Wrap="true" />
             <ItemTemplate>
                <asp:PlaceHolder ID="plBarCodes" runat="server" />
             </ItemTemplate>
         </asp:TemplateField>
    </Columns>
</asp:GridView>

CS page

using QRCoder;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Reports_QRCode : System.Web.UI.Page
{
    SqlConnection con;
    String strConn = ConfigurationManager.ConnectionStrings["ConOrms"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ShowData();
        }

    }
    protected void ShowData()
    {
        DataTable dt = new DataTable();
         con  = new SqlConnection(strConn);
        SqlDataAdapter adapt = new SqlDataAdapter("select   distinct P.Port,Lots,WMT from [IQMS].[dbo].[tblDischargeLots] t LEFT JOIN [IQMS].[dbo].tblPort P on t.PortId=P.PortId where WMT >20000", con);
        con.Open();
        adapt.Fill(dt);
        con.Close();
        grdQrCodeResult.DataSource = dt;
        grdQrCodeResult.DataBind();
    }

    protected void grdQrCodeResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Checking the RowType of the Row  
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //Find the PlaceHolder control.
            PlaceHolder plBarcode = (e.Row.FindControl("plBarCode") as PlaceHolder);
            PlaceHolder plBarcodes = (e.Row.FindControl("plBarCodes") as PlaceHolder);
            int index = e.Row.RowIndex;
            string code = "Scanner" + Convert.ToInt32(index);
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            QRCode qrCode = new QRCode(qrCodeData);
            //QRCodeGenerator qrGenerator = new QRCodeGenerator();
            //QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(code, QRCodeGenerator.ECCLevel.Q);
            System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
            imgBarCode.Height = 60;
            imgBarCode.Width = 60;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
                plBarcode.Controls.Add(imgBarCode);
               
            }
            System.Web.UI.WebControls.Image imgBarCodes = new System.Web.UI.WebControls.Image();
            imgBarCodes.Height = 60;
            imgBarCodes.Width = 60;
            using (Bitmap bitMap = qrCode.GetGraphic(20))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    byte[] byteImage = ms.ToArray();
                    imgBarCodes.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                }
             
                plBarcodes.Controls.Add(imgBarCodes);
            }
        }
    }

    protected void grdQrCodeResult_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

My Screen


Answers (5)