Satyam Singh
Satyam Singh

Reputation: 19

Custom KeyDown Control

I am very new to ASP.NET and this is my first job and I need to perfom good.

I am stuck on this search page where I have two text boxes. One searches by ID which is int, the second by last name. Both the searched populate a grid view. As per the requirement when the user types in last name and clicks search the grid view should populate which I got working. Now if the user stats typing in the ID Search text box the Lastname Search text box should clear as well as the grid view that had gotten populated should be hidden.

I achieved clearing the text box by using

txtSNumberSearch.Attributes["onKeyDown"] = "clearTextBox(this.id)";

txtSNumberSearch.Attributes.Add("onKeyDown", string.Format("document.getElementById('{0}').innerText='';", txtLastNameSearch.ClientID));

txtLastNameSearch.Attributes.Add("onKeyDown", string.Format("document.getElementById('{0}').innerText='';", txtSNumberSearch.ClientID));

But am not able to clear or hide the grid view on key down in the text box, my boss says I need to create a custom key down event handler. I do not know how to do it. Any help would be appreciated as I really need to perform at this job.

Upvotes: 0

Views: 716

Answers (2)

keyboardP
keyboardP

Reputation: 69372

An easy way of hiding the GridView is by simply adding it into a standard div and hiding that.

<div id="divGV">
  <asp:GridView>...
</div>

You can hide the div by doing this in javascript:

document.getElementById("divGV").style.display='none';"

You've already got a handler added for txtLastNameSearch, so you could do something like this:

   txtLastNameSearch.Attributes.Add("onKeyDown", string.Format("document.getElementById('{0}').value='';
document.getElementById("divGV").style.display='none';", txtSNumberSearch.ClientID));

Upvotes: 2

SaravananArumugam
SaravananArumugam

Reputation: 3720

Text boxes don't use InnerText. Try using value property. Keydown is just fine I think.

Try this.

txtSNumberSearch.Attributes["onKeyDown"] = "clearTextBox(this.id)";

txtSNumberSearch.Attributes.Add("onKeyDown", string.Format("document.getElementById('{0}').value='';", txtLastNameSearch.ClientID));

txtLastNameSearch.Attributes.Add("onKeyDown", string.Format("document.getElementById('{0}').value='';", txtSNumberSearch.ClientID));

Upvotes: 0

Related Questions