Solved

Losing Data on Verification

Posted on 2014-03-06
6
248 Views
Last Modified: 2014-03-08
Dear Experts,
I have a text box that counts down allowed number of words. Before the contributor can submit the article they have to agree to the submission policy. The agreement checkbox is validated. If it is unchecked at submission the validation posts the error so the author can agree and submit their article. Only one problem. On the post back of the submit button it clears out the text box of the article the author had just spent so much time typing in.

What I need is the textbox not to clear  if the page is invalid. Below is the code for the html and for the C# asp.net on the code behind. Can you tell me how to make it happen?

Thanks,
Bob Bellows

HTML:
    <textarea id="FaithStory" cols="60" rows="9"></textarea>

    <div id="Submission" 
        
        
        style="position: absolute; width: 479px; height: 36px; top: 188px; left: 42px; text-align: left; font-size: medium;">
    
        <asp:CheckBox ID="ckbxAgreeSubmissionPolicy" runat="server" Text="I agree to the Submission Policy of I-CollaborateInc.com" />
        <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="You must agree to the Submission Policy" 
            onservervalidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
        <br />
        <asp:HyperLink ID="HyperLink1" runat="server" Font-Names="Niamey" 
            Font-Size="Small" ForeColor="Blue" 
            NavigateUrl="http://www.i-collaborateinc.com/SubmissionPolicy.aspx" 
            Target="_blank">Submission Policy</asp:HyperLink>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />    
     
    </div>
    <div style="position: absolute; width: 115px; height: 62px; top: 190px; left: 619px;">
    <asp:Button ID="btnSubmitFaithStory" runat="server" Text="Submit" />
    </div>

Open in new window


c#

public partial class _hbiyg_FaithStories_AddYourStory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid=false;
        if (ckbxAgreeSubmissionPolicy.Checked)
            args.IsValid=true;
    }
 
}

Open in new window

0
Comment
Question by:bobbellows
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912055
You'll need to turn your textarea into a server control and enable viewstate:
<textarea id="FaithStory" runat="server" enableviewstate="true" cols="60" rows="9"></textarea>

Open in new window

0
 

Author Comment

by:bobbellows
ID: 39912118
That kept the text in the textarea from losing it's text but by going to the server it lost the word counting function.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39912125
The code you posted doesn't show how your word counting works, but assuming it is a javascript function, you simply need to spit out a line of javascript after postback to call that again and have it recount the words.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bobbellows
ID: 39912234
Sorry. You're right. I forgot about the jquery plugin.  

I'm sorta new to this and had another expert help me create the textarea with word counting. That was my first experience working with jquery and I know very little about java script.

Here is the code for the jquery. It's on a master page if that makes a difference.

jquery

<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="../JS/word-and-character-counter.js"></script>

<script>
$(function() {
  $("#FaithStory").counter({
    type: 'word', 
    goal: 250
});
});
</script>

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39912341
You will probably need to move the javascript to the WebForm itself. When you switch the textarea to be a server control, the ID will be generated at runtime by the ASP.Net engine, and you will need that ID for the javascript.

So, move this:
<script>
$(function() {
  $("#FaithStory").counter({
    type: 'word', 
    goal: 250
});
});
</script>

Open in new window

To your WebForm and put it somewhere towards the bottom of the page. Then change:
$("#FaithStory").counter({

Open in new window

To:
$("#<%= FaithStory.ClientID %>").counter({

Open in new window

That will inject the auto generated ID into the script, and allow it to be re-applied after postback.
0
 

Author Closing Comment

by:bobbellows
ID: 39915344
Decided to create a page without the master page and this worked! Thanks!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running powershell scripts from ASP.NET 6 57
Save json data from URL using SSIS 1 26
Set custom font WPF 2 21
C# DataTable (in memory) vs SQL Lookups 4 36
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question