我正在尝试使用用户文本以及从数据绑定复选框中选择的值生成条形码。我的代码编译得很好,但是当我生成条形码时,它不是从复选框中读取选定的值而是使用条形码打印:
SYSTEM.WEB.UI.WEBCONTROLS.CHECKBOXLIST'USER INPUT'
以下是绑定数据库中数据的代码。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BarcodeBind();
}
}
public void BarcodeBind()
{
SqlConnection conn = new SqlConnection(GetConnectionString());
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Statement"
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
ListItem item = new ListItem();
item.Text = sdr["BarcodeNum"].ToString();
cmd.Parameters.AddWithValue("@BarcodeNum", BarcodeNum);
BarcodeNum.Items.Add(item);
}
}
conn.Close();
}
}
以下是生成条形码的代码:
protected void btnGenerate_Click(object sender, EventArgs e)
{
string barCode = Barcode + txtCode.Text;
System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
using (Bitmap bitMap = new Bitmap(barCode.Length * 50, 90))
{
using (Graphics graphics = Graphics.FromImage(bitMap))
{
Font oFont = new Font("IDAutomationHC39M", 18);
PointF point = new PointF(3f, 3f);
SolidBrush blackBrush = new SolidBrush(Color.Black);
SolidBrush whiteBrush = new SolidBrush(Color.White);
graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
graphics.DrawString(barCode, oFont, blackBrush, point);
}
using (MemoryStream ms = new MemoryStream())
{
bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] byteImage = ms.ToArray();
Convert.ToBase64String(byteImage);
imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
}
plBarCode.Controls.Add(imgBarCode);
}
这是我的HTML代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="BarCodeBind" runat="server"
</asp:CheckBoxList>
<hr />
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Button ID="btnGenerate" runat="server" Text="Generate" OnClick="btnGenerate_Click" />
<hr />
<asp:PlaceHolder ID="plBarCode" runat="server" />
</div>
</form>
</body>
我认为它与声明选择了其中一个值有关。但我不知道如何用数据绑定来做到这一点。或者,如果有问题的数据绑定项应该由数据库本身的布尔值控制?