<div>try this;</div> <div> </div> <div>Add this line of code somewhere in your aspx page</div> <div><div></div> <div><asp:TextBox runat ="server" id="Txt_SearchByName" </div> <div> style="margin-top:0px;margin-bottom:3px;width:20%;"
OnTextChanged ="searchByName" AutoPostBack="true" ></asp:TextBox></div> <div><aspx:Textbox runat="server" id="Txt_Result" ></asp:TextBox></div> <div></div></div> <div> </div> <div>Add following line of code to aspx code file(.cs file)</div> <div>protected
void searchByName(object sender, EventArgs e)
{</div> <div> Txt_Result.Text=Txt_SearchByName.Text;</div> <div> //replace above line of code with your own code.
}</div> <div> </div> <div>Regards,</div> <div>Sunil.
</div>
I tryed with OnTextChanged, but it load the function if i only press one number.
How can i make it load X funtion if i press only enter?
Hi,
What did you mean by load X function(s)? If you need to execute several functions by OnTextChanged event, you can just call these functions in OnTextChanged event handler like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Enter (return) was pressed.
// Call a custom method when user presses this key.
AcceptMethod();
}
}
Use JavaScript to raise and execute server side event.
<script type="text/javascript">
function checkEnter(e) {
var characterCode
if (e && e.which) {
e = e
characterCode = e.which
}
else {
e = event
characterCode = e.keyCode
}
if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
__doPostBack('Button1', 'EnterKeyPressed')
return false
}
else {
return true
}
}
</script>
This is the textbox and the hidden button that raise the server side event.
LfVo
Member
21 Points
53 Posts
textbox and keypress enter
Jul 31, 2009 11:52 PM|LINK
Hi all,
What i wanna do is,
I have a textbox, and i insert a number, after that, i press enter, and goes to a function in the cs file,
I dont wanna use default button.
Is possible to do that?
regards,
Luís
http://informatictips.blogspot.com/
manoranjansahoo
Participant
978 Points
226 Posts
Re: textbox and keypress enter
Aug 01, 2009 12:31 AM|LINK
To call a cs function after press enter in a textbox you just set the AutoPostBack property of TextBox to True.
Then In the TextChanged event of that textbox call that cs method.
Manoranjan Sahoo
My Site: .Net Articles & Discussion Forum
If My Answer Helps You, Then Please click “Mark as Answer”.
PeteNet
All-Star
81342 Points
11398 Posts
Re: textbox and keypress enter
Aug 01, 2009 01:38 AM|LINK
you could try this too: http://www.mredkj.com/vbnet/textboxtextchangedpostback.html
Peter
sunilnooranad
Member
14 Points
2 Posts
Re: textbox and keypress enter
Aug 01, 2009 01:11 PM|LINK
OnTextChanged ="searchByName" AutoPostBack="true" ></asp:TextBox></div> <div><aspx:Textbox runat="server" id="Txt_Result" ></asp:TextBox></div> <div></div></div> <div> </div> <div>Add following line of code to aspx code file(.cs file)</div> <div>protected void searchByName(object sender, EventArgs e)
{</div> <div> Txt_Result.Text=Txt_SearchByName.Text;</div> <div> //replace above line of code with your own code.
}</div> <div> </div> <div>Regards,</div> <div>Sunil.
</div>
LfVo
Member
21 Points
53 Posts
Re: textbox and keypress enter
Aug 01, 2009 07:18 PM|LINK
Hi
I tryed with OnTextChanged, but it load the function if i only press one number.
How can i make it load X funtion if i press only enter?
http://informatictips.blogspot.com/
khanwasim07
Member
98 Points
57 Posts
Re: textbox and keypress enter
Aug 01, 2009 07:31 PM|LINK
try it with javascript onkeypress
<script type="text/javascript" language="javascript">
function checkvalue(e)
{
var f=e.value;
window.alert(f); //it will give you value of text box in above code you can also called asp.net webservice for your task
}
</script>
<asp:Textbox runat="server" id ="txt" onkeypress="javascript:checkvalue(this)"/>
LfVo
Member
21 Points
53 Posts
Re: textbox and keypress enter
Aug 01, 2009 10:19 PM|LINK
hi,
i can't use javascript because i have to some functions in the cs file that i will access from that funtion.
is another way to do that in c#?
http://informatictips.blogspot.com/
Shengqing Ya...
All-Star
45983 Points
2997 Posts
Microsoft
Re: textbox and keypress enter
Aug 05, 2009 09:21 AM|LINK
Hi,
What did you mean by load X function(s)? If you need to execute several functions by OnTextChanged event, you can just call these functions in OnTextChanged event handler like this:
protected void TextBox1_TextChanged(object sender, EventArgs e) { Response.Write("This is OnTextChanged event handler. <br />"); FunctionA(); FunctionB(); FunctionC(); } protected void FunctionA() { Response.Write("This is FunctionA"); } protected void FunctionB() { Response.Write("This is FunctionB. <br />"); } protected void FunctionC() { Response.Write("This is FunctionC. <br />"); }If I have misunderstood you, please feel free to let me know.
Best Regards,
Shengqing Yang
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Spider.net
Member
351 Points
162 Posts
Re: textbox and keypress enter
Aug 05, 2009 09:51 AM|LINK
Insted of using onTextChanged use KeyDown event
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Enter (return) was pressed. // Call a custom method when user presses this key. AcceptMethod(); } }piccolo
Member
32 Points
20 Posts
Re: textbox and keypress enter
Aug 05, 2009 10:37 AM|LINK
Hi,
Use JavaScript to raise and execute server side event.
<script type="text/javascript"> function checkEnter(e) { var characterCode if (e && e.which) { e = e characterCode = e.which } else { e = event characterCode = e.keyCode } if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key) __doPostBack('Button1', 'EnterKeyPressed') return false } else { return true } } </script>This is the textbox and the hidden button that raise the server side event.
And this the server side method definition.
protected void EnterKeyPressed(object sender, EventArgs e) { Response.Write("Enter detected"); }You also must change the @page even validation and set it to false.
EnableEventValidation="false"
This could be a risk. You can read about it searching in forums.
best regards.