asp.net - ASP.NET 中的自定义事件

标签 asp.net delegates eventargs custom-event

我想使用 C# 作为我的代码在 asp.net 页面上处理自定义事件。 我想在单击时增加搜索文本框的大小。 应该有点像这样……

StackOverflowsearchTextBoxSnapShot

我知道这可以使用事件和委托(delegate)来完成。我尝试了一些,但我不确定我应该在什么时候引发事件。所以我只是在 txtSearch_TextChanged 事件

这是一个片段:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


delegate void MessageEventHandler(object sender, EventArgs e);

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

    if (Session["Redirected"] != null)
    {
        Session["Redirected"] = null;
        if (Session["FirstName"] != null)
            txtSearch.Text = Session["FirstName"].ToString();
        if (Session["Gender"] != null)
            ddlGen.SelectedValue = Session["Gender"].ToString();
        btnSearch_Click(sender, e);
    }


    if (!Page.IsPostBack)
    {   
        BindGrid();
    }
}

private void BindGrid()
{
    //Get dataset
    //bind
    string query = "select EmployeeId,FirstName,Password,Address,sex,Deptno,act_book,actTV,DOJ,isActiveYN from employees";

    DataSet ds = new DataSet("Employees");
    SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
    SqlDataAdapter da = new SqlDataAdapter(query, con);

    da.Fill(ds);
    gvSession.DataSource = ds;        
    gvSession.DataBind();
}

protected void btnSearch_Click(object sender, EventArgs e)
{

    string query = "Select EmployeeId,FirstName,Password,Address,sex,Deptno,act_book,actTV,DOJ,isActiveYN from employees where 1=1";

    if (txtSearch.Text != "")
    {
        query += " and FirstName like '%" + txtSearch.Text + "%'";            
        Session["FirstName"] = txtSearch.Text;   
    }

    if (ddlGen.SelectedValue != "")
    {
        query += "  and sex='" + ddlGen.SelectedValue.ToUpper() + "'";            
        Session["Gender"] = ddlGen.SelectedValue;
    }


    DataSet ds = new DataSet("Employees");
    SqlConnection con = new SqlConnection("Password=admin;User ID=admin;Initial Catalog=asptest;Data Source=dbsvr");
    SqlDataAdapter da = new SqlDataAdapter(query, con);            


    da.Fill(ds);
    gvSession.DataSource = ds;
    gvSession.DataBind();


}


private event MessageEventHandler texBoxOnClick;

void _Default_texBoxOnClick(object sender, EventArgs e)
{
    //this function auto generates when you use += and tab twice in Visual Studio
    //handle the event here
    txtSearch.Width = Convert.ToInt32(300);
    //throw new Exception("The method or operation is not implemented.");
} 

private void OnTextBxClicked(EventArgs e)
{
    if (texBoxOnClick != null)
        texBoxOnClick(this, e);
}

protected void txtSearch_TextChanged(object sender, EventArgs e)
{
    //adding handler
    this.texBoxOnClick += new MessageEventHandler(_Default_texBoxOnClick);
    //Raising a Notification
    OnTextBxClicked(e);
    Session["FirstName"] = "";

}

protected void ddlGen_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["Gender"] = "";   

    }
}

在我提到的最后一次编辑之后。它现在在单击搜索按钮时引发事件,因为回发时发生文本更改。我的问题是我无法确定何时调用 OnTextBxClicked(e); 因为我希望它在单击文本框时发生。(本网站本身的链接)

最佳答案

TextChanged 事件仅在页面回发时在服务器上触发,因此虽然您可以在那里处理它以增加文本框宽度,但您很可能希望在客户端执行此操作:

<script type="text/javascript" language="javascript"> 
<!--
    $(document).ready(function () {
        $('#myTextBoxID').focus(function () {
            $(this).width(500)
        });
    }) 
//--> 
</script>

关于asp.net - ASP.NET 中的自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6995149/

相关文章:

asp.net - 如何在 ASP.Net 的 GridView 中显示带有回车符的文本

swift - 我的 VC 没有初始化器

Jquery - 使用 jquery 传递 asp 按钮命令参数

c# - 在 SelectedIndexChange 上传递一个值

c# - Web 应用程序时区问题

c# - 用正则表达式和我自己的参数替换字符串

asp.net - HttpRequestBase.UserHostAddress 抛出错误

c# - 在另一个范围内调用从 Expression<Action> 创建的 Action

ios - 如何覆盖 XCTest 中的 TableView 委托(delegate)

c# - 如何在 EventArgs 中传递对象