1

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 
     } 
3
  • 2
    post some code. that would help to rectify the problem.. Commented Jul 1, 2011 at 10:26
  • " I have a repeater that call a registered user control" means? Commented Jul 1, 2011 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 } Commented Jul 1, 2011 at 10:38

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I put it on the top of the page without head and it worked, thanks a lot :)
0

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.

2 Comments

the function is still not called, note that the page is not post back because the button is not a server control.
Could you show page's markup? By the way are you show full markup of the user control?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.