【问题标题】:ASPX connection with c#与 c# 的 ASPX 连接
【发布时间】:2021-09-04 03:29:56
【问题描述】:

我正在尝试使用 c# 和 aspx 创建一个动态二维码生成器,我不太了解 html,当我尝试在浏览器(opera)上运行它时出现一些错误

这是我的 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;
            }
        }
    }
}

这是我的 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>

这是 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;
    }
}

这是我运行时遇到的错误

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?)

【问题讨论】:

    标签: c# html asp.net


    【解决方案1】:

    在您的示例代码中,按钮的名称是btnQCRead,但是您处理单击事件的方法名为btnRead_ClickAutoEventWireup 正在寻找 btnQCRead_Click,它与您的方法 btnRead_Click 不匹配。 btnQCGenerate 和不匹配的方法 btnGenerate_Click 也是如此,需要类似地重命名为 btnQCGenerate_Click

    【讨论】:

    • 感谢您睁开眼睛,我总是忘记这些方法名称,而且大多数时候我的代码看起来很混乱,而且您的解决方案也有效,我很感激!
    【解决方案2】:

    您需要在 aspx sode 或代码后面更改您的按钮单击事件。双方的事件名称应保持相同。试试下面的 aspx 代码。不要更改代码后面的任何内容。

    <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>
    

    【讨论】:

    • 感谢它对我的代码起作用的回复,我不再在本地主机页面上崩溃!
    猜你喜欢
    • 2013-11-26
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多