Solved

Losing Data on Verification

Posted on 2014-03-06
6
247 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.

829 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