使用TextChanged事件搜索信息或执行操作

时间:2019-09-17 09:41:34

标签: c# asp.net sql-server webforms

我该如何完成类似的任务?我想使用文本框事件(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">&copy;</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)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

自动回发不起作用,因为没有用户操作。

当您从QueryString获取StaffID时,我不会使用TexBox。您仍然可以使用Label或Literal或受保护的字符串来显示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。