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???

Recommended Answers

All 16 Replies

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

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

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 !!

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?

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 :)

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, that looks ok. so all you need to is call your submit function in the TExtBox1_KeyDown sub !

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!!!!

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.

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?

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

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>

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 !

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>

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'.!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.