Question

I'm having a trouble while devolping my .net application.

I'm using the event OnTextChanged on a textbox to change the content in another Textbox, the first textbox has AutoPostBack="true", but when i write in it and click in another part, the page refreshes completely.

here is my ascx code:

<form id="Form1" runat="server">
    Change text
    <asp:TextBox id="txt1" runat="server" ontextchanged="ejemplo" autopostback="true"/>
    <p><asp:Label id="lbl1" runat="server" /></p>
</form>

and the script in the same ascx page:

<script runat="server">
protected void ejemplo(object sender, EventArgs e)
        {
            lbl1.Text = "Changed";
        }
</script>

I'm using MVC4, Thanks for your answers.

EDIT:

here is a video of what is really happening: http://remainedesign.com/video/asd.html

Was it helpful?

Solution

Life cycle of MVC and webforms both are different. MVC is not about server controls.... viewstate... no page life cycle events in web form...

What is the 'page lifecycle' of an ASP.NET MVC page, compared to ASP.NET WebForms? hope this helps..

Now coming to your point.

if you want to display something in the Textbox2 while entering a Value into the Textbox1 you have to use client side script, see example below

javascript

<script type="text/javascript" language="javascript">
        function textCounter(field, field2, maxlimit) {
            var countfield = document.getElementById(field2);
            if (field.value.length > maxlimit) {
                field.value = field.value.substring(0, maxlimit);
                return false;
            } else {
                countfield.value = maxlimit - field.value.length;
            }
        }
</script>

Your Html page

<%using (Html.BeginForm("Index", "Account", FormMethod.Post)) // here index is a ActionName, Account is a controller name
      {%>

<input type="text" id="textbox1" name="Message"  onkeyup="textCounter(this,'textbox2',208)"/>

<input disabled  maxlength="3" size="3" value="208" id="textbox2"  /></label>

<input type="submit" value="Send" />  
<%}%>

Here
textCounter() function on keyup event in textbox1 will display value in textbox2,

submit button will submit form which call action "index" on controller "Account",see below how action act

public class AccountController : Controller
{
    [HttpPost]
    public ActionResult index(FormCollection result)
    {
     string TextBoxValue=result["Message"];
     return view("yourviewname");
     }
}

please note,above example is solely for MVC project

i hope this example may help you..

OTHER TIPS

the first textbox has AutoPostBack="true", but when i write in it and click in another part, the page refreshes completely

That is exactly what AutoPostBack is for. If you click on another part, that's when you lose focus on the textbox and it will trigger a postback. If you need more proof then read this from MSDN:

Gets or sets a value that indicates whether an automatic postback to the server occurs when the TextBox control loses focus.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top