简体   繁体   English

ASPX 与 C# 的连接

[英]ASPX connection with c#

I am trying to create a Dynamic QRcode generator with c# and aspx i don't know html much and i get some error while i try to run it on my browser(opera)我正在尝试使用 c# 和 aspx 创建一个动态 QRcode 生成器我不太了解 html,当我尝试在我的浏览器(opera)上运行它时出现一些错误

This is my c# code QCCode.aspx.cs这是我的 c# 代码 QCCode.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ZXing;


namespace QRCodeSample
{
    public partial class QCCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        // Event to generate the QC Code
        protected void btnGenerate_Click(object sender, EventArgs e)
        {
            GenerateMyQCCode(txtQCCode.Text);
        }
        protected void btnRead_Click(object sender, EventArgs e)
        {
            ReadQRCode();
        }
        private void GenerateMyQCCode(string QCText)
        {
            var QCwriter = new BarcodeWriter();
            QCwriter.Format = BarcodeFormat.QR_CODE;
            var result = QCwriter.Write(QCText);
            string path = Server.MapPath("~/images/MyQRImage.jpg");
            var barcodeBitmap = new Bitmap(result);

            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream(path,
                   FileMode.Create, FileAccess.ReadWrite))
                {
                    barcodeBitmap.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            imgageQRCode.Visible = true;
            imgageQRCode.ImageUrl = "~/images/MyQRImage.jpg";

        }

        private void ReadQRCode()
        {
            var QCreader = new BarcodeReader();
            string QCfilename = Path.Combine(Request.MapPath
               ("~/images"), "MyQRImage.jpg");
            var QCresult = QCreader.Decode(new Bitmap(QCfilename));
            if (QCresult != null)
            {
                lblQRCode.Text = "My QR Code: " + QCresult.Text;
            }
        }
    }
}

Here is my html QCCode.aspx这是我的 html QCCode.aspx

"<%@ Page Language = "C#" AutoEventWireup="true"
   CodeBehind="QCCode.aspx.cs"
   Inherits="QRCodeSample.QCCode" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml%22%3E
   <head runat="server">
      <title>Sample ASP.NET application to Generate and
         Read QR Code from a Browser</title>
   </head>
   <body>
      <form id="QCFrom" runat="server">
         <div>
            <asp:TextBox ID = "txtQCCode" runat="server">
            </asp:TextBox >
             <asp:Button ID = "btnQCGenerate" runat="server"
               Text="Create My QR Code"
               OnClick="btnQCGenerate_Click" />
            <hr/>
            <asp:Image ID = "imgageQRCode" Width="100px"
               Height="100px" runat="server"
               Visible="false" /> <br /><br />
            <asp:Button ID = "btnQCRead" Text="Read My QR Code"
               runat="server" OnClick="btnQCRead_Click" />
               <br /><br />
            <asp:Label ID = "lblQRCode" runat="server">
            </asp:Label>
         </div>
      </form>
   </body>
   </html>

This is the QCCode.aspx.designer.cs page i don't know what is this这是 QCCode.aspx.designer.cs 页面我不知道这是什么

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace QRCodeSample
{


    public partial class QCCode
    {

        /// <summary>
        /// QCFrom control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm QCFrom;

        /// <summary>
        /// txtQCCode control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.TextBox txtQCCode;

        /// <summary>
        /// btnQCGenerate control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button btnQCGenerate;

        /// <summary>
        /// imgageQRCode control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Image imgageQRCode;

        /// <summary>
        /// btnQCRead control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Button btnQCRead;

        /// <summary>
        /// lblQRCode control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Label lblQRCode;
    }
}

This is the error i get when i run it这是我运行时遇到的错误

CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)


Line 13:             <asp:TextBox ID = "txtQCCode" runat="server">
Line 14:             </asp:TextBox >
Line 15:              <asp:Button ID = "btnQCGenerate" runat="server" Text="Create My QR Code" OnClick="btnQCGenerate_Click" />
Line 16:             <hr/>
Line 17:             <asp:Image ID = "imgageQRCode" Width="100px" Height="100px" runat="server" Visible="false" /> <br /><br />

C:\Users\bugra\source\repos\QrCode\QCCode.aspx(15,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(15,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCGenerate_Click' and no accessible extension method 'btnQCGenerate_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(18,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCRead_Click' and no accessible extension method 'btnQCRead_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)
C:\Users\bugra\source\repos\QrCode\QCCode.aspx(18,59): error CS1061: 'qccode_aspx' does not contain a definition for 'btnQCRead_Click' and no accessible extension method 'btnQCRead_Click' accepting a first argument of type 'qccode_aspx' could be found (are you missing a using directive or an assembly reference?)

In your sample code, the name of the button is btnQCRead , however your method to handle the click event is named btnRead_Click .在您的示例代码中,按钮的名称是btnQCRead ,但是您处理单击事件的方法名为btnRead_Click The AutoEventWireup is looking for btnQCRead_Click , which won't match your method btnRead_Click . AutoEventWireup正在寻找btnQCRead_Click ,它与您的方法btnRead_Click不匹配。 The same is true for btnQCGenerate and the unmatched method btnGenerate_Click which will need to be similarly renamed btnQCGenerate_Click .对于btnQCGenerate和不匹配的方法btnGenerate_Click也是如此,它们需要类似地重命名为btnQCGenerate_Click

you need to change your button click event either on aspx sode or code behind side.您需要在 aspx 代码或后面的代码上更改按钮单击事件。 Event names should remain same on both sides.双方的事件名称应保持相同。 Try below aspx code.试试下面的aspx代码。 Do not change anything on code behind.不要更改后面的代码的任何内容。

<form id="QCFrom" runat="server">
         <div>
            <asp:TextBox ID = "txtQCCode" runat="server">
            </asp:TextBox >
             <asp:Button ID = "btnQCGenerate" runat="server"
               Text="Create My QR Code"
               OnClick="btnGenerate_Click" />
            <hr/>
            <asp:Image ID = "imgageQRCode" Width="100px"
               Height="100px" runat="server"
               Visible="false" /> <br /><br />
            <asp:Button ID = "btnQCRead" Text="Read My QR Code"
               runat="server" OnClick="btnRead_Click" />
               <br /><br />
            <asp:Label ID = "lblQRCode" runat="server">
            </asp:Label>
         </div>
      </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM