Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 
     } 
share|improve this question
2  
post some code. that would help to rectify the problem.. – niksvp Jul 1 '11 at 10:26
" I have a repeater that call a registered user control" means? – mahesh Jul 1 '11 at 10:29
Here is some code to clarify: usercontrol.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 } – Shimaa Jul 1 '11 at 10:38

3 Answers

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.

share|improve this answer

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

share|improve this answer
I put it on the top of the page without head and it worked, thanks a lot :) – Shimaa Jul 1 '11 at 11:37
@Shimaa nice to see that I've pointed to the right direction... – Niko G. Jul 1 '11 at 11:45

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.

share|improve this answer
the function is still not called, note that the page is not post back because the button is not a server control. – Shimaa Jul 1 '11 at 11:26
Could you show page's markup? By the way are you show full markup of the user control? – Yuriy Rozhovetskiy Jul 1 '11 at 11:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.