开发者

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

开发者 https://www.devze.com 2023-03-16 13:04 出处:网络
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

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 saveE开发者_如何学JAVAvent() 
    { 
           var eventText = document.getElementById('<%=txtEventDescription.ClientID%>').value; 
           // make ajax call 
     } 


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号