0

I was wondering how i can trigger an event when a textbox is selected or focused. My textbox is an asp.net type.

 <asp:TextBox ID="TB" runat="server"></asp:TextBox>

When the event is triggered, i would like to do something in code-behind with c#.

thanks in advance for your reply.

3 Answers 3

2

We do not have any textbox selected event in asp.net. You can try using text changed event of textbox or you can try using java script as below.

<head runat="server">
<title></title>
<script type="text/javascript">
function RefreshUpdatePanel() {
    __doPostBack('<%= Code.ClientID %>', '');
};
</script>

         <asp:TextBox ID="Code" runat="server" onkeyup="RefreshUpdatePanel();"     AutoPostBack="true" OnTextChanged="Code_TextChanged"></asp:TextBox>
    <asp:UpdatePanel ID="Update" runat="server">
    <ContentTemplate>
        <asp:DropDownList runat="server" ID="DateList" />
        <asp:TextBox runat="server" ID="CurrentTime" ></asp:TextBox>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Code" />
    </Triggers>
</asp:UpdatePanel>
Sign up to request clarification or add additional context in comments.

2 Comments

Code behind : protected void Code_TextChanged(object sender, EventArgs e) { //Adding current time (minutes and seconds) into dropdownlist DateList.Items.Insert(0, new ListItem(DateTime.Now.ToString("mm:ss"))); //Setting current time (minutes and seconds) into textbox CurrentTime.Text = DateTime.Now.ToString("mm:ss"); }
thanks for your help, but i figured out by a different approach.
2

oki, so i figured it out by reading on an article on a different page.

check link: http://codingresource.blogspot.no/2010/01/how-to-use-events-like-onblur-onfocus.html

instead of using onblur i use onclick

Comments

0

Their is no serverside onfocus event for asp textbox you can use textchanged event as follows.using update panel of ajax control you can avoid page refreshment.

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ontextchanged="TextBox1_TextChanged1" ></asp:TextBox>

protected void TextBox1_TextChanged2(object sender, EventArgs e)
{

}

On Client side onfocus event You can invoke a c# webmethord through ajax call as follows

HTML SOURCE

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#TextBox1").focus(function () {
                    $.ajax({
                        type: "POST",
                        url: "Default.aspx/test",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(msg) {
                        }
                    });
                });
            });
  </script>

C# code

   [WebMethod]
    public static void test()
    {

    }

1 Comment

interesting, but the client don't want to use ajax for the solution. I might try this on my next project. Thanks for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.