gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:04:47 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com