Shimaa
Shimaa

Reputation: 21

ASP.net user control doesn't call a javascript function

I am developing asp.net web application, I have a repeater that call a registered user control, I have in the user control a button that I want to call a javascript function that make Ajax call to do some action on server. this button doesn't call the javascript method, I don't know why? and when I view source I found the javascript function is repeated for every item in the repeater, how to eliminate this repetition specially that I read server items inside the function, and why the function is not called?

Thanks a lot!

sercontrol.ascx 

<div id="divBtnEvent" runat="server"> 
   <input type="button" id="btnAddEvent" class="ok-green" onclick="saveEvent();" /> 
</div> 

<script type="text/javascript"> 
    function saveEvent() 
    { 
           var eventText = document.getElementById('<%=txtEventDescription.ClientID%>').value; 
           // make ajax call 
     } 

Upvotes: 1

Views: 2983

Answers (3)

Deepak Kumar
Deepak Kumar

Reputation: 682

Problem 1: In view source I found the javascript function is repeated for every item in the repeater.

Solution: Put your js function on the page on which the user control is being called. You also have to place your js files references on your page not on user control.

Problem 2: You are trying to get control's value as <%=txtEventDescription.ClientID%>. And I think, this control is on user control.

Solution: Please check your page source code and see that the control's actual clientid is.

If still have issues in calling js function, check firefox's Error consol.

Hope this help.

Upvotes: 1

IUnknown
IUnknown

Reputation: 22448

Replace the saveEvent function definition from user control onto the page where the control is used. All the way as I understand, you have use in this function textbox id from that page. Actually, if you have place javascript block in user control's markup it will be rendered along with the rest control's markup. To avoid this you may register javascript from the server code and check is that code block is already registered. By the way, as it is - only the last function definition being taked into attention as it redefined the previous one each time new control instance rendered.

Upvotes: 0

Nika G.
Nika G.

Reputation: 2384

OP said and when I view source I found the javascript function is repeated for every item in the repeater

to get rid of it put ur javascript as follows

<head runat="server">
//heres goes ur js
</head>

I guess that's ur problem

Upvotes: 0

Related Questions