Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Question Related to Asp.net 3.5 using C#

I have a textbox at the top of a gridview, when i press a key, the gridview filled with the data related to the text in the textbox.
now i have to click the gridview with mouse to go up and down with arrow keys within the gridview.
I want, that when i press down arrow key in the textbox the first row of the gridview is automatically get clicked and the focus goes to the first row(if exist)

Any help?

thanks
Faiz
Posted
Comments
Sandeep Mewara 17-Jun-12 8:09am    
Odd requirement and behavior. What have you tried so far?
Faiz_Khan 17-Jun-12 14:22pm    
Tried to do something like this

<script type="text/javascript" language="javascript">
function FindProduct()
{
if(event.keyCode != 38 && event.keyCode != 39 && event.keyCode != 40 && event.keyCode != 41 && event.keyCode != 8 && event.keyCode != 32)
{
if((event.keyCode > 96 && event.keyCode < 123) || (event.keyCode > 64 && event.keyCode < 91) || (event.keyCode > 47 && event.keyCode < 58))
{
document.getElementById('<%= txtfind.ClientID %>').value =document.getElementById('<%= txtfind.ClientID %>').value + String.fromCharCode(event.keyCode);
event.keyCode=9;
event.cancelBubble=true;
document.getElementById('<%= lnkFindProd.ClientID %>').click();
}
else if (event.keyCode == 13)
{
var control = '<%=CurRow.ClientID %>';
document.getElementById(control).value = "0";
event.keyCode=9;
event.cancelBubble=true;
setCursorPositionToEnd('<%= txtfind.ClientID %>');
document.getElementById('<%= btnAddFromGrid.ClientID %>').click();
}
}
else if(event.keyCode == 8)
{
var strLen = (document.getElementById('<%= txtfind.ClientID %>').value).length;
if(strLen >=1)
{
document.getElementById('<%= txtfind.ClientID %>').value = (document.getElementById('<%= txtfind.ClientID %>').value).slice(0,strLen-1);
event.keyCode=9;
event.cancelBubble=true;
setCursorPositionToEnd('<%= txtfind.ClientID %>');
document.getElementById('<%= lnkFindProd.ClientID %>').click();
}
}
else if(event.keyCode == 40)
{
event.keyCode=9;
event.cancelBubble=true;
document.getElementById('<%=lnkFocus.ClientID %>').click();
}
}
</script>

<table>
<tr>
<td style="width:60%">
<asp:TextBox ID="txtfind" runat="server" Width="99%" onkeydown="FindProduct()" OnTextChanged="txtfind_TextChange" AutoPostBack="true" onfocus="this.style.backgroundColor='#ffccff'" onblur="this.style.backgroundColor='White'">
<asp:LinkButton ID="lnkFindProd" runat="server" OnClick="lnkFindProd_Click">
<asp:LinkButton ID="lnkFocus" runat="server" OnClick="lnkFocus_Click">
</td>
</tr>
</table>




protected void lnkFocus_Click(object sender, EventArgs e)
{
// what to write here to click the gridview and send the focus on it
}

1 solution

Hi Faiz,

You won't be able to get this directly from any of the .net class and it will be difficult if you are using any 3rd party gridview or ajax. But you will able to do so if you try with the basic.

in keypress event catch the key and then increment the selected row by 1.

C#
TextBox1.Attributes.Add("OnKeyPress", "GetKeyPress()")

In the Default.ASPX Code page, I have

<script>

Function GetKeyPress()
{
    alert("you Pressed " & event.KeyCode;
}

</script>


now within this function you can use something like this

C#
GridView gv = new GridView();
       GridViewRow r = gv.SelectedRow; // handel case when no row is selected
       // set the new focus to new row

       gv.SelectedRow.RowIndex = r.RowIndex + 1;


this might now work as of now because i havent done this on VS .. please try and let me know in case of any issue.

All the best!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900