0

I have an issue for ASP.net user control

Let's say for Example that I have a Main Page with two user control inside.

The Page consist of

Main Page : Survey.aspx

UserControl1 : RadioButton.ascx

UserControl2 : CheckBox.ascx

In both User Control, I added a specific Javascript to assist for both CSS functions.

The issue is, When I ran the Survey Page, let say that I load for RadioButton first, and then Checkbox second. The javascript added in Checkbox does not load in Browser. Only Javascript for RadioButton. The same goes vice versa when Checkbox is load first and then RadioButton load second.

When i inspect in the source page for Survey.aspx, it seems that the Javascript from the second loaded User Control is not registered in the Page. I have also tried to load the Javascript all in The Main Page, but failed to do so. Cannot call the specific functions inside every User Control

The user control is binded through repeater events for ASP.NET

If you have any ideas, please let me know. thanks in advance.

In Survey.aspx

 <asp:Repeater runat="server" ID="rptQuestion">
   <ItemTemplate>
     <uc:CheckBox ID="ucCheckBox" runat="server" Visible="False">
     <uc:RadioButton ID="ucRadioButton" runat="server" Visible="False">
   </ItemTemplate>
 </asp:Repeater>

In ucCheckBox.ascx

<script type="text/javascript">
function checkchange(){
--some function---
}
</script>

In ucRadioButton.ascx

<script type="text/javascript">
function radChange(){
--some function--
}
</script>

So if I load checkbox first

In source file in Browser only exist javascript in checkbox, no javascript from radiobutton usercontrol

Iman Manan
  • 25
  • 7
  • this question is a lot about 'let's say' and 'imagine that' - please show some relevant code samples, so it will be much easier to help you. – Homungus Jul 22 '20 at 07:17

1 Answers1

0

there are multiple issues here:

  1. If you set the controls to Visible="False" they won't be rendered at all - so also no javascript from that control.
  2. If you put your javascript in the .ascx it will be rendered for every instance of that control. So you will have multiple radChange() and checkchange() function declarations in your page - every one overwriting the last one. That's not what you want, I'm guessing.

Of course you can put your javascript directly in the aspx-markup, this will work.

Homungus
  • 1,114
  • 1
  • 10
  • 20
  • No other way to contact you, but I just saw your edit of [this](https://stackoverflow.com/questions/63031317/how-this-parameter-knows-the-argument). Could you please not remove space characters that a) make the code more readable b) are used by every single auto-formatter in the world c) were inserted by jsfiddle's auto-formatter? Fixing a typo in the question text is fine, but changing code to a weird, inconsistent and less readable spacing style is clearly not. –  Jul 22 '20 at 09:51
  • Hey there @ChrisG - do you say, the edited version visible now is not correct? All I did was removing the wrong indentation: the `return lengGreaterThanNum;` belongs on the same level as `function lengGreaterThanNum`. Don't get your point. By the way: my edit was reviewed by others, because I don't have the privilege to do edits without it... Please delete your comment when you are satisfied. – Homungus Jul 22 '20 at 10:04
  • Ok, so maybe your edit ended up in the queue and somebody else "improved" it? I just saw this: https://stackoverflow.com/posts/63031317/revisions and concluded you had messed things up –  Jul 22 '20 at 10:11
  • @ChrisG apology accepted. One advice for the future: read and review carefully before reaching out for people. – Homungus Jul 22 '20 at 11:04
  • Looks like your edit restored the "missing" spaces when it was approved because that happened after my edit, which inserted them. No need to get arrogant though. –  Jul 22 '20 at 11:58