1

I am in the process of adding a filter to asp grid. I have succeeded in showing a text box for filtering in header.
I need to fire server code for filtering whenever user presses enter key in textbox.
So I started with adding event like

txtFilter.Attributes.Add("onkeyup", keyUpScript);

Then I added client script as

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "registerkeyUPScript", registerkeyUPScript, true);

where

string keyUpScript = "keyUPScript(event);";

string registerkeyUPScript = "function keyUPScript(e){\n" 
                                        + "var unicode=e.keyCode? e.keyCode : e.charCode;\n" 
                                        + "if(unicode == '13')\n" 
                                            +"{\n"
                                                +" //PostBack code"
                                            +"}"
                                        +"}";

Now how could I postback when ever user enters a string in text box and presses enter key. I also need to rebind the filtered data back to grid

Any help will be appreciated.

Robert_Junior
  • 1,122
  • 1
  • 18
  • 40
  • [How to use __doPostBack](http://stackoverflow.com/questions/3591634/how-to-use-dopostback) – Drew Apr 20 '15 at 06:27

1 Answers1

1

You can try using this line of code to attach an onkeyup to your TextBox to only fire a postback event when Enter is pressed. Also, just in case you want to attach an OnTextChanged event for that particular TextBox, you can do so and that will be called as well when the page postbacks.

Use this in your Page_Load event :

TextBoxID.Attributes.Add("onkeyup", "return (event.keyCode==13);");

And this is the OnTextChanged event just in case if you want to attach :

protected void T1_TextChanged(object sender, EventArgs e)
{
    //your logic goes here
}

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34