c# - 使用 TextChanged 事件搜索信息或执行操作
问题描述
我怎样才能完成这样的事情?我想使用文本框的事件(TextChanged 事件)自动从数据库中获取数据,因此我使用类似这样的东西
protected void StaffID_TextChanged(object sender, EventArgs e)
{
//fetch information from DB
}
StaffID.Text 是我的应用程序上一个控件的名称,所以我想做的是,当我这样的时候
https://localhost/signatureapplication.aspx?StaffID=9655096 将 9655096 传输到文本框后,它会自动使用该文本框中的 9655096 的值从数据库中搜索和获取信息不起作用。有什么我想念的吗?
我已将文本框值设置为像这样使用 AutoPostBack="True"
<asp:TextBox ID="StaffID" runat="server" AutoPostBack="True" OnTextChanged="StaffID_TextChanged"></asp:TextBox>
然后为 C# 表单添加了一个参考,看看它是否会触发一个消息框,然后像这样调用它
protected void StaffID_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Hello from me!");
}
让我展示一下我到目前为止所做的事情。
我这样做是为了将员工 ID 作为 URL 参数
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="signaturestart.aspx.cs" Inherits="MYSignatureAccess.signaturestart" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StaffID.Text = Convert.ToString(Request.QueryString["StaffID"]);
StaffID.DataBind();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="generator" content="Web Page Maker">
<style type="text/css">
/*----------Text Styles----------*/
.ws6 {font-size: 8px;}
.ws7 {font-size: 9.3px;}
.ws8 {font-size: 11px;}
.ws9 {font-size: 12px;}
.ws10 {font-size: 13px;}
.ws11 {font-size: 15px;}
.ws12 {font-size: 16px;}
.ws14 {font-size: 19px;}
.ws16 {font-size: 21px;}
.ws18 {font-size: 24px;}
.ws20 {font-size: 27px;}
.ws22 {font-size: 29px;}
.ws24 {font-size: 32px;}
.ws26 {font-size: 35px;}
.ws28 {font-size: 37px;}
.ws36 {font-size: 48px;}
.ws48 {font-size: 64px;}
.ws72 {font-size: 96px;}
.wpmd {font-size: 13px;font-family: Arial,Helvetica,Sans-Serif;font-style: normal;font-weight: normal;}
/*----------Para Styles----------*/
DIV,UL,OL /* Left */
{
margin-top: 0px;
margin-bottom: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="image1" style="position:absolute; overflow:hidden; left:148px; top:58px; width:113px; height:70px; z-index:0"><img src="images/Image.png" alt="" title="" border=0 width=113 height=70></div>
<div id="roundrect1" style="position:absolute; overflow:hidden; left:140px; top:151px; width:177px; height:35px; z-index:1"><img border=0 width="100%" height="100%" alt="" src="images/roundrect100678000.gif"></div>
<div id="text1" style="position:absolute; overflow:hidden; left:157px; top:157px; width:150px; height:19px; z-index:2">
<div class="wpmd">
<div><font color="#FFFFFF" face="Verdana"><B>Signing Document</B></font></div>
</div></div>
<div id="hr1" style="position:absolute; overflow:hidden; left:150px; top:177px; width:933px; height:17px; z-index:3">
<hr size=1 width=933 color="#970097">
</div>
<div id="hr2" style="position:absolute; overflow:hidden; left:173px; top:473px; width:933px; height:17px; z-index:4">
<hr size=1 width=933 color="#970097">
</div>
<div id="text2" style="position:absolute; overflow:hidden; left:174px; top:486px; width:150px; height:23px; z-index:5">
<div class="wpmd">
<div><font class="ws8">©</font><font class="ws8"> 2019 WEMA Bank Nigeria</font></div>
</div></div>
<div id="table1" style="position:absolute; overflow:hidden; left:165px; top:511px; width:952px; height:48px; z-index:6">
<div class="wpmd">
<div><TABLE bgcolor="#FFFFFF" border=0 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=983 height=61><BR>
</TD>
</TR>
</TABLE>
</div>
</div></div>
<div id="image2" style="position:absolute; overflow:hidden; left:505px; top:231px; width:285px; height:190px; z-index:7"><img src="images/unnamed.gif" alt="" title="" border=0 width=285 height=190></div>
<asp:TextBox ID="StaffID" runat="server" AutoPostBack="True" OnTextChanged="StaffID_TextChanged1"></asp:TextBox>
</body>
</form>
</html>
现在我想使用后面的代码来做这样的搜索:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace WEMASignatureAccess
{
public partial class signaturestart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void SignDocument()
{
string StaffID = Request.QueryString["staffID"];
//string StaffID = "208045P";
string constring = @"Data Source=DESKTOP-9CM4N5S\SQLEXPRESS;Initial Catalog=SignatureBox2;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
string conQuery = "select * from SignatureBox_DB where StaffID = @StaffID";
using (SqlCommand cmd = new SqlCommand(conQuery, con))
{
cmd.Parameters.AddWithValue("@StaffID", StaffID);
using (SqlDataReader rd = cmd.ExecuteReader())
{
try
{
if (rd.Read())
{
string filePath = "~/ImagesSignature/";
string myFile = "sinature.jpg";
string fileName = Path.Combine(filePath, myFile);
byte[] imageBytes = Convert.FromBase64String(rd["SignatureBase64"].ToString());
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
File.WriteAllBytes(Server.MapPath(fileName), imageBytes);
string myphysicalPath = System.Web.HttpContext.Current.Server.MapPath(fileName);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null;
doc = app.Documents.Open(@"C:\Users\emi\Desktop\Jasmine.docx", Type.Missing);
var Signature2Ctrl = doc.SelectContentControlsByTag("Jasmine");
var testingCtrl = Signature2Ctrl[1];
testingCtrl.Range.InlineShapes.AddPicture(myphysicalPath, Type.Missing, Type.Missing, Type.Missing);
doc.Save();
MessageBox.Show("Document Signed successfully!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Response.Redirect("signaturecomplete.aspx");
}
}
}
}
protected void StaffID_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("Hello from me!");
}
protected void StaffID_TextChanged1(object sender, EventArgs e)
{
}
}
}
解决方案
由于没有用户操作,自动回发将不起作用。
当您从 QueryString 获取 StaffID 时,我不会使用 TexBox。您仍然可以使用标签或文字或受保护的字符串来显示 StaffID。
if (!IsPostBack)
{
mystaffID = Convert.ToString(Request.QueryString["StaffID"]);
// Populate your textbox here or any other means you prefer to use.
SignDocument();
}
为了显示数据库中的这些数据,我可能会在 aspx 页面上使用中继器。
应该直截了当,应该做你想做的事。
编辑:
你有一个重定向
Response.Redirect("signaturecomplete.aspx");
这实际上结束了当前页面的生命周期。如果您必须将信息传递到您可以使用的签名完整页面,例如 Session["staffID"] = 26355。
推荐阅读
- django - Angular django rest 框架集成
- ruby-on-rails - 需要对属性的特定参数进行验证
- c++ - 用于 LED 板动画的 Arduino 地图或列表
- c# - 如何使用将使用相同构造函数的相同元素初始化引用类型列表?
- python - PyTorch:如何编写只返回权重的神经网络?
- python - 索引嵌套在字典中的命名元组
- axios - 什么是axios默认超时
- c# - .Core mvc 发布的应用程序部分视图在 ASPNETCORE_ENVIRONMENT “生产”中不起作用,但在“开发”中工作正常
- python - 消除 python 列表中的 n 最高值
- jquery - 具有动态值的 jQuery hasClass()