0

hi i have a textbox and a button on my asp.net page i want that when i write any value on textbox and after that when i click on textbox then a click would perform auto on button so how can i do this???

5
Contributors
16
Replies
21
Views
8 Years
Discussion Span
Last Post by Ramesh S
0

hello,
if u mean that without you clicking the button, the button code should work, do this.

1. Go to the properties of the textbox then make the enable postback = true.

2. copy the code of the button inside the textbox

run to confirm.


If this solve ur problem mark it has solved.
tanx

0

What is a "auto on button"? Just click the button and it will post back the textBox if you used an <asp:TextBox> in the form fields

0

Well, i think i can visualize the logic of waht you want - though don't know why you would want to do that !
I would use 2 events:
- first in the onchange event of the textbox i would set a certain module level boolean variable to true (eg textBoxSet = true)
- second in the onclick event of the textbox, check the boolean variable's value. If it is true then do whatever you are referrring as an auto on process !!

0

o thanks wilch i think you got my point but again i want to clear you what exact i want like here in daniweb when we clickon member login n then two textboxes apear when we fill username and after that when we fill password on next textbox and without clicking on login button we just click on textbox n then it executes code this is what i want?

0

well, surely if you had some code to the screen, it would be farmuch easier to correct and point out the direction to. can you send the relevant part of what you have done - then we can help :)

0

ok dear this is what i want like vb i do this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String
text = TextBox1.Text
MsgBox(Text)
End Sub

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub

when i hit enter button within textbox click will perform on button this is what i need to do in asp.net because i did'nt find _KeyDown event of textbox in asp.net i hope it will clear you!

0

well, that looks ok. so all you need to is call your submit function in the TExtBox1_KeyDown sub !

0

i knw that code is perfect which i posted but you are still not getting me that code works very fine in vb.net for desktop but i want to do that same task in asp.net but in asp.net there is not any TExtBox1_KeyDown event availble for textbob control thats my problem!!!!

0

Well, if it is a problem with a webpage, then if you have a submit button in a form, despite which form's textbox you maybe in, pressing the enter key will automatically submit the form.
This means you do NOT need to trap the key_press event or any such event to achieve what you want (submit a form)!
Just keep your code in the submit button, and it will be executed automatically on pressing the enter key.

0

well dear i know that as well but what if i have 2 buttons in one page ?? one is to submit data n other is for nother purpz?

0

i am still facing this problem plz any one solve this !!!

0

ok dear this is what i want like vb i do this

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim text As String
text = TextBox1.Text
MsgBox(Text)
End Sub

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub

when i hit enter button within textbox click will perform on button this is what i need to do in asp.net because i did'nt find _KeyDown event of textbox in asp.net i hope it will clear you!

Well. If you want to postback and call the button's click event when enter key is pressed in the TextBox, then set the defaultbutton property in the <form> tag to the button's id.

For example,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage16.aspx.cs" Inherits="DemoPage16" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo on defaultbutton property</title>
</head>
<body>
    <form id="form1" [B]defaultbutton="btnSubmit" [/B]runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        </table>
    </div>
    </form>
</body>
</html>
0

hi Ramesh,
well i am facing this problem only because i have 2 buttons in one page now if i set defaultbutton then when i click other textbox it will only perform click on submit button which i dont want i want that on each textbox i set code to perform there own tasks for their own defined buttons !

0

Ok. I understand your requirement..

Now try this.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage16.aspx.cs" Inherits="DemoPage16" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo on defaultbutton property</title>

    <script type="text/javascript" language="javascript">

        function submitButton1(event) {
            if (event.keyCode == 13) {

                document.getElementById("Button1").click();
                return false;
            }
        }

        function submitButton2(event) {
            if (event.keyCode == 13) {
                document.getElementById("Button2").click();
                return false;
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td width="200px">
                    <asp:TextBox ID="TextBox1" onkeydown="return submitButton1(event)" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox2" onkeydown="return submitButton2(event)" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="Button2_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
0

Ramesh can you plz send me code without html on in vb.net its very complicated to understand because i am new in it and ya when i do write that code i found an error at
OnKeyDown = "return submitButton1(event)" and written on it that the Attribute 'OnKeyDown' is not a valid attribute of element ' Textbox'.!

0

I have posted the html content only except page directive. Therefore other things are common to bothe VB/.NET and C#.

onkeydown is a client side(javascript) event. It is a warning when you put client side event in Server side control. Do not worry about that and ignore it.

Just copy all the content below header tag and paste in a sample .aspx page. It will work.
Just ensure that you have server side client event exists for two command buttons.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.