Solved

ASP.NET: Problem with multiple user controls on same page

Posted on 2008-10-14
4
504 Views
Last Modified: 2013-11-07
Hi,
I built a user control to validate and compare two values and I use it on my page. When I only have one instance of the user control, the control works fine. But When I put many of them on the same page, if I type a value in the first instance, it is reflected invisibly on the last one. This make that I can't make do my comparison(done with Javascript) any more. Does some one know why?


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TextBoxcompareDate.ascx.cs" Inherits="TextBoxcompareDate" %>
<%@ Register TagPrefix="Date" Namespace="userControls" Assembly="TextBoxDate" %>
 
<head >
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
 
function validateDates(source, arguements)
{
    var thevalue = arguements.Value;
    
    arguements.IsValid=false;    
    
    if(!validateDate(thevalue))
    {
        alert(source.errormessage);
        return;
    }
    arguements.IsValid=true;  
            
}
function compareStartEnd()
{ 
var stringFromDate = document.getElementById('<%=txtStart.ClientID%>').value;
var stringToDate = document.getElementById('<%=txtEnd.ClientID%>').value;
 
var FromDate = Date.parse(stringFromDate);
                var ToDate = Date.parse(stringToDate);
                if (FromDate > ToDate)
                    alert('The end date must be greather than the start date');
}
 
    </script>
</head>
<table width="100%">
    <tr>
        <td align="right" width="75%">
            <asp:Label ID="lblCaption" runat="server" Text="Title: " Font-Names="Verdana" Font-Size="10px" ForeColor="#000000"></asp:Label>
        </td>
        <td align="left" width="10%">
            <asp:TextBox 
                ID="txtStart" 
                runat="server" 
                MaxLength="10" 
                Width="80px"             
                style="padding: 2px;border:solid 1px #92C86A;background-color: #ffffff;font-family:Verdana;font-size: 11px;color:#000000;"
                onfocus="this.select();"></asp:TextBox>
         </td>
         <td align="center" width="5%">
            <span style="font-family:Verdana;font-size: 10px;font-weight:bold;color:#000000;">-</span>	        
         </td>
         <td align="left" width="10%">
            <asp:TextBox 
                ID="txtEnd" 
                runat="server" 
                MaxLength="10" 
                Width="80px" 
                style="padding: 2px;border:solid 1px #92C86A;background-color: #ffffff;font-family:Verdana;font-size: 11px;color:#000000;" 
                onfocus="this.select();" 
                onchange="compareStartEnd();" ></asp:TextBox>
        </td>            
    </tr>            
</table>
<asp:CustomValidator 
    ID="cuvStart" 
    runat="server" 
    ControlToValidate="txtStart" 
    ClientValidationFunction="validateDates" 
    ErrorMessage="Incorrect starting date." 
    SetFocusOnError="true"
    Display="None">&nbsp</asp:CustomValidator>
<asp:CustomValidator 
    ID="cuvEnd" 
    runat="server" 
    ControlToValidate="txtEnd" 
    ClientValidationFunction="validateDates" 
    ErrorMessage="Incorrect ending date" 
    SetFocusOnError="true"
    Display="None">&nbsp</asp:CustomValidator>

Open in new window

0
Comment
Question by:karakav
[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
  • 2
4 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22710780
This is beacuse your functions are not unique.
you can name your function like

function validateDates_<%=this.ClientID%>(source, arguements)


then hook up your validators in OnLoad like

cuvEnd.ClientValidationFunction = string.format("validateDates_{0}",this.ClientID);
0
 
LVL 8

Accepted Solution

by:
the_crazed earned 500 total points
ID: 22711004
as your function is part of the user control, you're redeclaring the same function over and over, changing the startid and endid

I'd take the function out of the control, put it in a common js file, and alter it to something like the code below.

then your onchange needs to be:
onchange="compareStartEnd(event);"
function compareStartEnd(e)
{ 
    if(!e) e=event;
    if(e.srcElement) var el = e.srcElement
    else var el = e.target;
 
    var endId = el.id;
    var startId = endId.replace("txtEnd","txtStart")
 
    var stringFromDate = document.getElementById(startId).value;
    var stringToDate = document.getElementById(endId).value;
 
    var FromDate = Date.parse(stringFromDate);
                var ToDate = Date.parse(stringToDate);
                if (FromDate > ToDate)
                    alert('The end date must be greather than the start date');
}

Open in new window

0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31505832
Thanks a lot. Do you mind telling me how I can set back to focus to the control upon the error message?
0
 
LVL 8

Expert Comment

by:the_crazed
ID: 22718944
yep, you've got a pointer to the original control there in "el" so you just add this at the end:
el.focus();

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

626 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