3

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

StackOverflowsearchTextBoxSnapShot

我知道这可以使用事件和委托来完成。我尝试了一些方法,但我不确定我应该什么时候引发事件。所以我只是在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"] = "";   

    }
}

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

4

2 回答 2

4

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

<script type="text/javascript" language="javascript"> 
<!--
    $(document).ready(function () {
        $('#myTextBoxID').focus(function () {
            $(this).width(500)
        });
    }) 
//--> 
</script>
于 2011-08-09T11:24:59.170 回答
2

这很可能必须通过 javascript 或任何其他客户端技术在客户端完成。

于 2011-08-09T12:29:53.893 回答