Tuesday, 9 July 2019

more instance of ascx on same page

I have a asp.net ascx calles ProductSearch

In the ascx I have these 4 controls

<asp:TextBox runat="server" ID="txtSearchProduct" type="text" class="form-control typeahead" data-provide="typeahead" autocomplete="off">
</asp:TextBox>
<asp:TextBox runat="server" ID="txtProductNames" CssClass="hidden"> 
</asp:TextBox>
<asp:TextBox runat="server" ID="txtSearchProductID" Style="display: none;"> 
</asp:TextBox>
<asp:TextBox runat="server" ID="txtCaricati" Style="display: none;"> 
</asp:TextBox>

In this ascx I have a javascript funtion that fill typeahead textbox with all possible values:

 function LoadProducts() {
   var data = $("#<%=txtProductNames.ClientID%>").val();

   var $input = $(".typeahead");
   var jsonObj = $.parseJSON(data);
   var sourceArr = [];
   for (var i = 0; i < jsonObj.length; i++) { sourceArr.push(formatRow(jsonObj[i].id, jsonObj[i].code, jsonObj[i].name)); }

   // init Typeahead
   $input.typeahead({
      ...
   }

Now, the problem is that I have an aspx page that needs to implement the ascx twice. One in a principal div and one in a modal div (hidden by html while modal not visible).

Exploring html with F12 tool I see that I have the same function LoadProducts() two times.

In the first one the function works with its ClientId objects and the second one too.

I call The function LoadProducts() from code behind and the first one found inside the code is executed, maybe the wrong one.

I need a method to identify the javascript function depending on wich instance of ascx I'm using.

Is there any way to to this? if I haven't been clear enough, ask me questions



from more instance of ascx on same page

No comments:

Post a Comment