redirect on Textbox onkeydown

Rate It (1)

Last post 03-27-2008 12:14 PM by Boris2. 5 replies.

Sort Posts:

  • redirect on Textbox onkeydown

    03-27-2008, 9:59 AM
    • Loading...
    • Boris2
    • Joined on 02-11-2008, 2:48 PM
    • Posts 38

    Basicaly, I want to have a textfield that on enter sends the text it contains to a certain route.

    I figured the

    <%=Html.TextBox("companyIdTextBox", "", new { onkeydown = "onCompanyIdKeyDown" })%>

    And a function

            protected void OnCompanyIdEntered(object sender, EventArgs e) {
                RedirectToAction("Detail");
            }

    Or something similar would do the trick.
    But I'm way off, the event doesn't get triggered and codebehind doesn't know what RedirectToAction is.

    Maybe I need to take a break, but I seem to fail to do the easiest thing here...

  • Re: redirect on Textbox onkeydown

    03-27-2008, 10:36 AM
    Answer
    • Loading...
    • aravind.pk
    • Joined on 02-20-2008, 1:00 PM
    • India
    • Posts 67

     to my knowledge, i am typing the code block for your scenario.

    Use a javascript:

     
    function fnRedirectToAction()
    {
       if(event.keyCode == 13) //enter key pressed
       {
          var txtMsg = document.getElementById("txtbox1").value;
          if(txtMsg != "")
          {
              window.location.navigate("page2.aspx?action=" + txtMsg);
          }
          else
          {
              event.keyCode =0;
              return false;
          }
       }
       return true;
    }

     

    Make your textbox to map this javascript function by

     

    txtbox1.attributes.add("onkeydown","javascript:return fnRedirectToAction();");

     

    in page_load() event 

    Aravind
  • Re: redirect on Textbox onkeydown

    03-27-2008, 10:37 AM
    • Loading...
    • aravind.pk
    • Joined on 02-20-2008, 1:00 PM
    • India
    • Posts 67

     to my knowledge, i am typing the code block for your scenario.

    Use a javascript:

     
    function fnRedirectToAction()
    {
    if(event.keyCode == 13) //enter key pressed
    {
    var txtMsg = document.getElementById("txtbox1").value;
    if(txtMsg != "")
    {
    window.location.navigate("page2.aspx?action=" + txtMsg);
    }
    else
    {
    event.keyCode =0;
    return false;
    }
    }
    return true;
    }

     

    Make your textbox to map this javascript function by

     

    txtbox1.attributes.add("onkeydown","javascript:return fnRedirectToAction();");

     

    in page_load() event 

    Aravind
  • Re: redirect on Textbox onkeydown

    03-27-2008, 10:44 AM

    There isn't a control event model as with WebForms, so you'll have to stick to HTTP GET and POST to your Controller's Actions, optionally via AJAX. So sending the value as a GET/redirect using jQuery:

    <input id="companyIDTextBox"/>

    $(document).ready(function(){
        $("#companyIdTextBox").blur(function()
        {
            window.location = "/Controller/Action?paramName=" + $("companyIdTextBox").val() ;
        });

    Or something along the lines of this for the change event for AJAX postback (omits the callback function to process the response):

    $.get("/Controller/Action", { id: $("companyIdTextBox").val()  } );

  • Re: redirect on Textbox onkeydown

    03-27-2008, 11:30 AM
    • Loading...
    • JessChadwick
    • Joined on 12-02-2006, 3:58 AM
    • Central New Jersey, USA
    • Posts 6

    Boris,

    From your post it looks like your OnCompanyIdEntered method is in the codebehind of your view?  If this is the case, then RedirectToAction will not be available to you since that is a Controller method, and not available in a ViewPage.  Also, the object collection you're passing into the Html.TextBox function that includes "onkeydown = 'onCompanyIdKeyDown'" actually sets the 'onkeydown' DOM attribute and is not routing to your codebehind like in a Web Forms page.

    Instead, you'll be looking to make this a client-side function.  Your JavaScript would probably end up looking something like this (if it were inline on the view):

    <script type="text/javascript">
    function OnCompanyIdEntered() {
       if(event.keyCode == 13) //enter key pressed
       {
          var txtMsg = document.getElementById("txtbox1").value;
          if(txtMsg != "")
          {
              window.location.navigate("<%=Url.Action("Detail") %>?action=" + txtMsg);
          }
          else
          {
              event.keyCode =0;
              return false;
          }
       }
       return true;
    
    }
    </script> 

     Try adding that and see if it gets you what you want.

     
    http://blog.jesschadwick.com
  • Re: redirect on Textbox onkeydown

    03-27-2008, 12:14 PM
    • Loading...
    • Boris2
    • Joined on 02-11-2008, 2:48 PM
    • Posts 38
    Thanks guys, Indeed I figured it out already :$ I Was still thinking in the old way and trying to get a function triggered from codebehind. This off course won't work anymore. Thanks all for the response.
Page 1 of 1 (6 items)