2011-08-09 26 views
3

私のコードとしてC#を使用してasp.netページでカスタムイベントを処理したい。 検索テキストボックスのサイズを大きくしたいときにクリックします。 それは多少このように...ASP.NETのカスタムイベント

StackOverflowsearchTextBoxSnapShot

である必要があり、私はこのイベントを使用して行うことができるとdelegate.Iが何かをしようとしたけど、まさに私がevent.So Iを上げる必要がある場合、私はわからないんだけど私は今mentioned.It最後の編集はtextchangeがpostbacに発生すると、検索ボタンがクリックされたときにイベントを発生させた後

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"] = ""; 

    } 
} 

:単にtxtSearch_TextChangedイベントここ

にそれを上げる「メートルはスニペットですk.私の問題は、正確にいつ呼び出すのかを判断することができないことです。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

嬉しいです:) –

関連する問題