-1

I am doing a preview of what I am currently typing in a web page using ASP.NET. What I am trying to achieve is that whenever I type or change text in the textbox, the <h3> or label element will also change and always copy what the textbox value is without refreshing the browser. Unfortunately I cannot make it work. Here is what I tried.

.ASPX

    <div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
    <div class="Padding10px">
        <h1 class="Margin0px">Preview</h1>
        <hr />
        <p></p>
        <h3 id="NewsTitlePreview" class="TextAlignCenter" runat="server">Title</h3>
        <h5 id="NewsContentPreview" class="TextIndent50px TextAlignJustify" runat="server">Content</h5>
    </div>
</div>

<div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
    Title
    <asp:TextBox ID="Titletxt" runat="server" OnTextChanged="Titletxt_TextChanged"></asp:TextBox>
    Content
    <asp:TextBox ID="Contenttxt" runat="server" onchange="Contenttxt_TextChanged"></asp:TextBox>
    <asp:Button ID="Submit" runat="server" Text="Submit" />
</div>

.CS

protected void Titletxt_TextChanged(object sender, EventArgs e)
{
    NewsTitlePreview.InnerText = Titletxt.Text;
}
protected void Contenttxt_TextChanged(object sender, EventArgs e)
{
    NewsContentPreview.InnerText = Contenttxt.Text;
}

I Tried Adding Autopostback = true... but it only works and refreshes the page and i need to press tab or enter or leave the textbox :(

UPDATE: I Tried This - enter link description here But Still Doesnt Work :(

Community
  • 1
  • 1

5 Answers5

1

You are right in your analysis of the behavior of the control (it only fires the event when you leave the control), even when you have AutoPostBack="True".

MSDN says it all:

The TextBox Web server control does not raise an event each time the user enters a keystroke, only when the user leaves the control. You can have the TextBox control raise client-side events that you handle in client script, which can be useful for responding to individual keystrokes.

So you either have to be satisfied with the current behavior, or set up some client side event handling to do some validation, etc. client side.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Just add this script function in your code and in body write onload and call that function.

Javascript:

<script type="text/javascript">
function startProgram() {
       setTimeout('errorcheck()', 2000);

    }

    function errorcheck() {
        setTimeout('errorcheck()', 2000);
        document.getElementById("NewsTitlePreview").innerText = document.getElementById("Titletxt").value
        document.getElementById("NewsContentPreview").innerText = document.getElementById("Contenttxt").value
    }
</script>


<body onload="startProgram();">
<form id="form1" runat="server">

  <div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
 <div class="Padding10px">
<h1 class="Margin0px">Preview</h1>
<hr />
<p></p>
<h3 id="NewsTitlePreview" class="TextAlignCenter" runat="server">Title</h3>
<h5 id="NewsContentPreview" class="TextIndent50px TextAlignJustify" runat="server">Content</h5>
</div>
</div>

<div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
Title
<asp:TextBox ID="Titletxt" runat="server" ></asp:TextBox>
Content
<asp:TextBox ID="Contenttxt" runat="server"></asp:TextBox>
<asp:Button ID="Submit" runat="server" Text="Submit"  />
</div>

</form>
</body>
Anoop B.K
  • 1,484
  • 2
  • 17
  • 31
1

Download and include JQuery library. And also modify title and content textbox so they don't change their Id's

Title <asp:TextBox ID="Titletxt" ClientIDMode="Static" runat="server"></asp:TextBox>

Content <asp:TextBox ID="Contenttxt" ClientIDMode="Static" runat="server"></asp:TextBox>

Then add this script and it will work.

<script>    

    $(document).ready(function () {

        $('#Titletxt').on('input', function () {
            $("#NewsTitlePreview").text($(this).val());

        });
        $("#Contenttxt").on('input',function () {
            $("#NewsContentPreview").text($(this).val());
        });
    });
</script>
Nejdi Kroi
  • 612
  • 1
  • 6
  • 17
0

Try this it will work this how change event call using jquery dont forget to add google apis

   <script>

    $('#txtbox').change(function() {
        alert("change Event");
    });

</script>
Kamran Khan
  • 1,042
  • 10
  • 21
  • i already tried that :( unfortunately it doesnt work the way i need it :( it only works on refresh :( i need it to work everytime i change the text :( for example: when i type a word, it copies the value of the textbox per every letter and one by one – Alpha Gabriel V. Timbol Aug 18 '15 at 08:05
  • Then try javascript it will work fine...or try keypress event for textbox then it will work fine...selectindex work when you write something and then leave the textbox then it will hit event....java script will do the trick for you – Kamran Khan Aug 18 '15 at 08:07
  • do you have any suggestions on how to do it with javascript bro? :) i would really appreciate it :) – Alpha Gabriel V. Timbol Aug 18 '15 at 08:20
  • I have added some code for you may this helps solve your problem – Kamran Khan Aug 18 '15 at 09:52
0

One of the best idea... Just change your code to this. it works

ASPX

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"  UpdateMode="Conditional" ViewStateMode="Enabled">
<ContentTemplate>
  <div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
  <div class="Padding10px">
    <h1 class="Margin0px">Preview</h1>
    <hr />
    <p></p>
    <h3 id="NewsTitlePreview" class="TextAlignCenter" runat="server">Title</h3>
    <h5 id="NewsContentPreview" class="TextIndent50px TextAlignJustify" runat="server">Content</h5>
</div>
</div>

<div class="Width960px MarginLeftAuto MarginRightAuto MarginTop10px">
Title
<asp:TextBox ID="Titletxt" runat="server" OnTextChanged="Titletxt_TextChanged"></asp:TextBox>
Content
<asp:TextBox ID="Contenttxt" runat="server" onchange="Contenttxt_TextChanged"></asp:TextBox>
<asp:Button ID="Submit" runat="server" Text="Submit" />
</div>

</ContentTemplate>
</asp:UpdatePanel>

.CS

 protected void Titletxt_TextChanged(object sender, EventArgs e)
{
NewsTitlePreview.InnerText = Titletxt.Text;
UpdatePanel1.Update();
}
protected void Contenttxt_TextChanged(object sender, EventArgs e)
{
NewsContentPreview.InnerText = Contenttxt.Text;
UpdatePanel1.Update();
}
Anoop B.K
  • 1,484
  • 2
  • 17
  • 31
  • i tried it :( doesnt work :( doesnt it need a keypress trigger or something? i only works when i leave the textbox :( i need it to work everytime i enter a character – Alpha Gabriel V. Timbol Aug 18 '15 at 08:54