Click here to Skip to main content
15,943,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one user control. Inside this user control it have some child control and also some javascript code. This control is working fine when i am using once in a page. But problem is that when i am try to use it multiple time in the same page the problem is arise. Problem is that when i fire th first user control child control event it automatically fire the second control same event. Plz some one help me to resolve this problem.
Posted
Comments
P_Dash 3-Sep-13 2:38am    
Code Please.
VishwaKL 3-Sep-13 2:56am    
If you post the code and structure of user control we can help you
Dholakiya Ankit 3-Sep-13 5:45am    
cp wants code.........(:O
sanjay acharya 4-Sep-13 7:37am    
Problem Solved.

Try the below code it may work.
JavaScript
$(document).ready(function() {
$("#usercontrolID  .elementClass" ).click( fuction() {

});


}) ;

Try to write Javascript in the page which ur calling the user control so it may fix.Create event for each individual user control so the event will be fired only for that user control element only as above.
 
Share this answer
 
v3
I see one problem with your approach.

I think you have written the javascript event binding inside the user control.

So when you have multiple controls the same event in getting binded mutiple times.

Try externalizing the javascript and check.
 
Share this answer
 
Comments
sanjay acharya 3-Sep-13 7:29am    
you are correct.
Hi,

Please find the sample code for using multiple user control in the same page.

ASPX
~~~~~
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Multiple User Control in Same page</title>
    <style type="text/css">
        .innerTable
        {
            border: 1px solid black;
        }
        
        .outerTable
        {
            border: 1px solid rgb(209,209,209);
        }
        
        .Mandatory
        {
            border: 1px solid red;
        }
    </style>
    <script type="text/javascript">
        function ValidateIsEmpty(ctrl) {
            if (ctrl.value != "") {
                return true;
            }
            else {
                ctrl.className = "Mandatory";
                return true;
            }
        }

        function Validate(obj, ctrl) {
            if (document.getElementById(ctrl).value != "") {
                alert("The value in " + ctrl + " is " + document.getElementById(ctrl).value);
                return true;
            }
            else { return false; }
        }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="outerTable">
            <tr>
                <td>
                    User Control - 1
                </td>
            </tr>
            <tr>
                <td>
                    <uc1:myusercontrol id="myusercontrol1" runat="server" xmlns:uc1="#unknown" />
                </td>
            </tr>
            <tr>
                <td>
                    User Control - 2
                </td>
            </tr>
            <tr>
                <td>
                    <uc1:myusercontrol id="myusercontrol2" runat="server" xmlns:uc1="#unknown" />
                </td>
            </tr>
            <tr>
                <td>
                    User Control - 3
                </td>
            </tr>
            <tr>
                <td>
                    <uc1:myusercontrol id="myusercontrol3" runat="server" xmlns:uc1="#unknown" />
                </td>
            </tr>
        </table>
    </div>
    

</title></head></html>


Ascx design:
~~~~~~~~~~~~
<pre lang="HTML">

XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myusercontrol.ascx.cs"
    Inherits="WebApplication1.myusercontrol" %>
<table class="innerTable">
    <tr>
        <td>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click"
                />
        </td>
        <td>
            <asp:Label ID="lblDisplay" runat="server"></asp:Label>
        </td>
    </tr>
</table>



ASCX.CS
~~~~~~~~
C#
public partial class myusercontrol : System.Web.UI.UserControl
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           txtName.Attributes.Add("onblur", "return ValidateIsEmpty(this);");
           btnSubmit.Attributes.Add("onclick", "return Validate(this,'" + txtName.ClientID + "');");
       }

       protected void btnSubmit_Click(object sender, EventArgs e)
       {
           lblDisplay.Text = txtName.Text;
       }
   }



Note:
In the above coding am passing the child control ID ( Text box client ID from Code behind). The ClientID for a control will be unique. This helps in firing the event for the respective control.

Hope it helps.
 
Share this answer
 
Comments
sanjay acharya 3-Sep-13 7:31am    
thank you.this sample help me good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900