2011-08-09 49 views
3

我想用C#處理asp.net頁面上的自定義事件作爲我的代碼在後面。 我想在點擊時增加搜索文本框的大小。 這應該有點像這樣...ASP.NET中的自定義事件

StackOverflowsearchTextBoxSnapShot

我知道這是可以使用事件來完成和delegate.I試過的東西,但我不知道什麼時候我應該提高event.So我「M僅僅是提高它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"] = ""; 

    } 
} 

後最後的編輯我現在mentioned.It引發該事件時,作爲postbac發生textchange點擊搜索按鈕k.My的問題是,我無法確定何時完全打電話OnTextBxClicked(e);因爲我希望它發生在點擊文本框時。(本網站上的鏈接本身)

回答

4

TextChanged事件僅在頁面上觸發回發,因此,儘管你可以處理它有增加文本框的寬度,你最有可能想這樣做在客戶端:

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

這將極有可能要在客戶端通過完成JavaScript或任何其他客戶端技術。

+0

在做了一點研究後,我已經理解像這樣的事件總是客戶端事件,並且應該用一些腳本語言來完成。感謝你的回答有幫助 – Pavitar

+0

很高興我能幫忙:) –