How to run javascript in dynamically loaded user controls (ascx)?Is there a way to use “<%= someObject.ClientID %>” in an external javascript file?asp.net eval script in ajax html responseHow to validate an email address in JavaScriptHow do JavaScript closures work?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Best Ergonomic Design for a handheld ranged weapon
NULL value causes blank row in SELECT results for text concatenation
May a hotel provide accommodation for fewer people than booked?
How is char processed in math mode?
Can you remove a blindfold using the Telekinesis spell?
How and why does the ATR-72 sometimes use reverse thrust to push back from the gate?
Why is Searing Smite not listed in the Roll20 Spell books?
If the Moon were impacted by a suitably sized meteor, how long would it take to impact the Earth?
Is it unprofessional to mention your cover letter and resume are best viewed in Chrome?
Would people understand me speaking German all over Europe?
Should I intervene when a colleague in a different department makes students run laps as part of their grade?
What is the term for completing a route uncleanly?
Value of a limit.
Why Macos creates file mounts for each app?
My employer is refusing to give me the pay that was advertised after an internal job move
PCB design using code instead of clicking a mouse?
Should I put my name first or last in the team members list?
Can I shorten this filter, that finds disk sizes over 100G?
Just how much information should you share with a former client?
Applications of pure mathematics in operations research
How to remove rebar passing through an inaccessible pipe
How can you tell the version of Ubuntu on a system in a .sh (bash) script?
How to prevent a single-element caster from being useless against immune foes?
Planting Trees in Outer Space
How to run javascript in dynamically loaded user controls (ascx)?
Is there a way to use “<%= someObject.ClientID %>” in an external javascript file?asp.net eval script in ajax html responseHow to validate an email address in JavaScriptHow do JavaScript closures work?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How do I include a JavaScript file in another JavaScript file?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a webpage which dynamically loads multiple instances of the same usercontrol (.ascx) into and update panel via LoadControl on the backend.
In the usercontrol, I have javascript which I want to run when the user control is finished loading.
However, every time the page posts back, and a new usercontrol is added, the javascript isn't running.
I've tried adding $(this).load(function()...); at the beginning of the user control, but this doesn't seem to be getting hit.
I've tried using RegisterStartupScript to run some script at the end of the user control's Page_Load, but this doesn't seem to be running either. I can't debug in google chrome, so I don't know.
Here's my javascript from the user control (.ascx):
<script type="text/javascript">
// using the clientIDs as names so they only partain to this instance of sectionDetails on the facultyRequest page
var <%=spanDateRange.ClientID%>,
<%=aDateRange.ClientID%>, <%=aSpecificDates.ClientID%>;
function initSection<%=ClientID%>()
<%=spanDateRange.ClientID%> = $('#<%=spanDateRange.ClientID%>')[0];
<%=aDateRange.ClientID%> = $('#<%=aDateRange.ClientID%>')[0];
<%=aSpecificDates.ClientID%> = $('#<%=aSpecificDates.ClientID%>')[0];
// have to bind the events here because you can't use asp inside tag attributes
$(<%=aDateRange.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = false;
);
$(<%=aSpecificDates.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = true;
);
<%=aDateRange.ClientID%>.click();
</script>
spanDateRange, aDateRange, aSpecificDatesare all divs (runat="server") in my user control
And here's my Page_Load from the .ascx.cs file:
protected void Page_Load(object sender, EventArgs e)
Page.ClientScript.RegisterStartupScript(this.GetType(), "initSection", "initSection" + ClientID + "();", true);
And here's where I dynamically load multiple copies of the usercontrol:
protected void LoadSections()
var numSections = utils.Utils.Clamp(int.Parse(tbNumSections.Text), int.Parse(tbNumSections.Attributes["Min"]), int.Parse(tbNumSections.Attributes["Max"]));
for (int i = 2; i <= numSections && numSections != tbodySections.Controls.Count - 2; i++) // start at 2 because there's always 1 section, don't load any more sections if they're already loaded
var newSection = (usercontrols.sectionDetails)LoadControl("usercontrols/sectionDetails.ascx"); // load a new sectionDetails control
newSection.SectionNumber = i;
tbodySections.Controls.AddAt(i + 1, newSection);
I expect that after I load each section, the load event would get caught, or the startup script would run, but i don't think any javascript from my dynamically loaded user controls is running. I've tried putting the user control directly into the page, so that's how I know that my javascript is correct syntactically.
javascript c# asp.net
add a comment |
I have a webpage which dynamically loads multiple instances of the same usercontrol (.ascx) into and update panel via LoadControl on the backend.
In the usercontrol, I have javascript which I want to run when the user control is finished loading.
However, every time the page posts back, and a new usercontrol is added, the javascript isn't running.
I've tried adding $(this).load(function()...); at the beginning of the user control, but this doesn't seem to be getting hit.
I've tried using RegisterStartupScript to run some script at the end of the user control's Page_Load, but this doesn't seem to be running either. I can't debug in google chrome, so I don't know.
Here's my javascript from the user control (.ascx):
<script type="text/javascript">
// using the clientIDs as names so they only partain to this instance of sectionDetails on the facultyRequest page
var <%=spanDateRange.ClientID%>,
<%=aDateRange.ClientID%>, <%=aSpecificDates.ClientID%>;
function initSection<%=ClientID%>()
<%=spanDateRange.ClientID%> = $('#<%=spanDateRange.ClientID%>')[0];
<%=aDateRange.ClientID%> = $('#<%=aDateRange.ClientID%>')[0];
<%=aSpecificDates.ClientID%> = $('#<%=aSpecificDates.ClientID%>')[0];
// have to bind the events here because you can't use asp inside tag attributes
$(<%=aDateRange.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = false;
);
$(<%=aSpecificDates.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = true;
);
<%=aDateRange.ClientID%>.click();
</script>
spanDateRange, aDateRange, aSpecificDatesare all divs (runat="server") in my user control
And here's my Page_Load from the .ascx.cs file:
protected void Page_Load(object sender, EventArgs e)
Page.ClientScript.RegisterStartupScript(this.GetType(), "initSection", "initSection" + ClientID + "();", true);
And here's where I dynamically load multiple copies of the usercontrol:
protected void LoadSections()
var numSections = utils.Utils.Clamp(int.Parse(tbNumSections.Text), int.Parse(tbNumSections.Attributes["Min"]), int.Parse(tbNumSections.Attributes["Max"]));
for (int i = 2; i <= numSections && numSections != tbodySections.Controls.Count - 2; i++) // start at 2 because there's always 1 section, don't load any more sections if they're already loaded
var newSection = (usercontrols.sectionDetails)LoadControl("usercontrols/sectionDetails.ascx"); // load a new sectionDetails control
newSection.SectionNumber = i;
tbodySections.Controls.AddAt(i + 1, newSection);
I expect that after I load each section, the load event would get caught, or the startup script would run, but i don't think any javascript from my dynamically loaded user controls is running. I've tried putting the user control directly into the page, so that's how I know that my javascript is correct syntactically.
javascript c# asp.net
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.
– mwilson
Mar 26 at 22:40
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42
add a comment |
I have a webpage which dynamically loads multiple instances of the same usercontrol (.ascx) into and update panel via LoadControl on the backend.
In the usercontrol, I have javascript which I want to run when the user control is finished loading.
However, every time the page posts back, and a new usercontrol is added, the javascript isn't running.
I've tried adding $(this).load(function()...); at the beginning of the user control, but this doesn't seem to be getting hit.
I've tried using RegisterStartupScript to run some script at the end of the user control's Page_Load, but this doesn't seem to be running either. I can't debug in google chrome, so I don't know.
Here's my javascript from the user control (.ascx):
<script type="text/javascript">
// using the clientIDs as names so they only partain to this instance of sectionDetails on the facultyRequest page
var <%=spanDateRange.ClientID%>,
<%=aDateRange.ClientID%>, <%=aSpecificDates.ClientID%>;
function initSection<%=ClientID%>()
<%=spanDateRange.ClientID%> = $('#<%=spanDateRange.ClientID%>')[0];
<%=aDateRange.ClientID%> = $('#<%=aDateRange.ClientID%>')[0];
<%=aSpecificDates.ClientID%> = $('#<%=aSpecificDates.ClientID%>')[0];
// have to bind the events here because you can't use asp inside tag attributes
$(<%=aDateRange.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = false;
);
$(<%=aSpecificDates.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = true;
);
<%=aDateRange.ClientID%>.click();
</script>
spanDateRange, aDateRange, aSpecificDatesare all divs (runat="server") in my user control
And here's my Page_Load from the .ascx.cs file:
protected void Page_Load(object sender, EventArgs e)
Page.ClientScript.RegisterStartupScript(this.GetType(), "initSection", "initSection" + ClientID + "();", true);
And here's where I dynamically load multiple copies of the usercontrol:
protected void LoadSections()
var numSections = utils.Utils.Clamp(int.Parse(tbNumSections.Text), int.Parse(tbNumSections.Attributes["Min"]), int.Parse(tbNumSections.Attributes["Max"]));
for (int i = 2; i <= numSections && numSections != tbodySections.Controls.Count - 2; i++) // start at 2 because there's always 1 section, don't load any more sections if they're already loaded
var newSection = (usercontrols.sectionDetails)LoadControl("usercontrols/sectionDetails.ascx"); // load a new sectionDetails control
newSection.SectionNumber = i;
tbodySections.Controls.AddAt(i + 1, newSection);
I expect that after I load each section, the load event would get caught, or the startup script would run, but i don't think any javascript from my dynamically loaded user controls is running. I've tried putting the user control directly into the page, so that's how I know that my javascript is correct syntactically.
javascript c# asp.net
I have a webpage which dynamically loads multiple instances of the same usercontrol (.ascx) into and update panel via LoadControl on the backend.
In the usercontrol, I have javascript which I want to run when the user control is finished loading.
However, every time the page posts back, and a new usercontrol is added, the javascript isn't running.
I've tried adding $(this).load(function()...); at the beginning of the user control, but this doesn't seem to be getting hit.
I've tried using RegisterStartupScript to run some script at the end of the user control's Page_Load, but this doesn't seem to be running either. I can't debug in google chrome, so I don't know.
Here's my javascript from the user control (.ascx):
<script type="text/javascript">
// using the clientIDs as names so they only partain to this instance of sectionDetails on the facultyRequest page
var <%=spanDateRange.ClientID%>,
<%=aDateRange.ClientID%>, <%=aSpecificDates.ClientID%>;
function initSection<%=ClientID%>()
<%=spanDateRange.ClientID%> = $('#<%=spanDateRange.ClientID%>')[0];
<%=aDateRange.ClientID%> = $('#<%=aDateRange.ClientID%>')[0];
<%=aSpecificDates.ClientID%> = $('#<%=aSpecificDates.ClientID%>')[0];
// have to bind the events here because you can't use asp inside tag attributes
$(<%=aDateRange.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = false;
);
$(<%=aSpecificDates.ClientID%>).click(function ()
<%=spanDateRange.ClientID%>.hidden = true;
);
<%=aDateRange.ClientID%>.click();
</script>
spanDateRange, aDateRange, aSpecificDatesare all divs (runat="server") in my user control
And here's my Page_Load from the .ascx.cs file:
protected void Page_Load(object sender, EventArgs e)
Page.ClientScript.RegisterStartupScript(this.GetType(), "initSection", "initSection" + ClientID + "();", true);
And here's where I dynamically load multiple copies of the usercontrol:
protected void LoadSections()
var numSections = utils.Utils.Clamp(int.Parse(tbNumSections.Text), int.Parse(tbNumSections.Attributes["Min"]), int.Parse(tbNumSections.Attributes["Max"]));
for (int i = 2; i <= numSections && numSections != tbodySections.Controls.Count - 2; i++) // start at 2 because there's always 1 section, don't load any more sections if they're already loaded
var newSection = (usercontrols.sectionDetails)LoadControl("usercontrols/sectionDetails.ascx"); // load a new sectionDetails control
newSection.SectionNumber = i;
tbodySections.Controls.AddAt(i + 1, newSection);
I expect that after I load each section, the load event would get caught, or the startup script would run, but i don't think any javascript from my dynamically loaded user controls is running. I've tried putting the user control directly into the page, so that's how I know that my javascript is correct syntactically.
javascript c# asp.net
javascript c# asp.net
asked Mar 26 at 22:34
Tommy GodseyTommy Godsey
335 bronze badges
335 bronze badges
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.
– mwilson
Mar 26 at 22:40
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42
add a comment |
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.
– mwilson
Mar 26 at 22:40
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what
<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.– mwilson
Mar 26 at 22:40
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what
<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.– mwilson
Mar 26 at 22:40
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42
add a comment |
1 Answer
1
active
oldest
votes
asp.net eval script in ajax html reponse
This is it! ^^^
First, I had to move my LoadSections() in the codebehind from the Page_Load to Page_Init.
Then, in my UpdatePanel, I have:
// this function runs after each postback
function pageLoad()
evaluateScripts('#<%=tbodySections.ClientID%>'); //pass the table body (tbodySections) so we can run the js inside each user control
and then in the master page, I have:
// this will run any javascript in the selected div
function evaluateScripts(divSelector)
$(divSelector).find('script').each(function ()
if (this.src)
$('body').append(this);
else
var content = $(this).html();
eval(content);
);
After each postback, I can dynamically load any number of sections I need, and then hook up all the events in js via the functions pageLoad and evaluateScripts
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367142%2fhow-to-run-javascript-in-dynamically-loaded-user-controls-ascx%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
asp.net eval script in ajax html reponse
This is it! ^^^
First, I had to move my LoadSections() in the codebehind from the Page_Load to Page_Init.
Then, in my UpdatePanel, I have:
// this function runs after each postback
function pageLoad()
evaluateScripts('#<%=tbodySections.ClientID%>'); //pass the table body (tbodySections) so we can run the js inside each user control
and then in the master page, I have:
// this will run any javascript in the selected div
function evaluateScripts(divSelector)
$(divSelector).find('script').each(function ()
if (this.src)
$('body').append(this);
else
var content = $(this).html();
eval(content);
);
After each postback, I can dynamically load any number of sections I need, and then hook up all the events in js via the functions pageLoad and evaluateScripts
add a comment |
asp.net eval script in ajax html reponse
This is it! ^^^
First, I had to move my LoadSections() in the codebehind from the Page_Load to Page_Init.
Then, in my UpdatePanel, I have:
// this function runs after each postback
function pageLoad()
evaluateScripts('#<%=tbodySections.ClientID%>'); //pass the table body (tbodySections) so we can run the js inside each user control
and then in the master page, I have:
// this will run any javascript in the selected div
function evaluateScripts(divSelector)
$(divSelector).find('script').each(function ()
if (this.src)
$('body').append(this);
else
var content = $(this).html();
eval(content);
);
After each postback, I can dynamically load any number of sections I need, and then hook up all the events in js via the functions pageLoad and evaluateScripts
add a comment |
asp.net eval script in ajax html reponse
This is it! ^^^
First, I had to move my LoadSections() in the codebehind from the Page_Load to Page_Init.
Then, in my UpdatePanel, I have:
// this function runs after each postback
function pageLoad()
evaluateScripts('#<%=tbodySections.ClientID%>'); //pass the table body (tbodySections) so we can run the js inside each user control
and then in the master page, I have:
// this will run any javascript in the selected div
function evaluateScripts(divSelector)
$(divSelector).find('script').each(function ()
if (this.src)
$('body').append(this);
else
var content = $(this).html();
eval(content);
);
After each postback, I can dynamically load any number of sections I need, and then hook up all the events in js via the functions pageLoad and evaluateScripts
asp.net eval script in ajax html reponse
This is it! ^^^
First, I had to move my LoadSections() in the codebehind from the Page_Load to Page_Init.
Then, in my UpdatePanel, I have:
// this function runs after each postback
function pageLoad()
evaluateScripts('#<%=tbodySections.ClientID%>'); //pass the table body (tbodySections) so we can run the js inside each user control
and then in the master page, I have:
// this will run any javascript in the selected div
function evaluateScripts(divSelector)
$(divSelector).find('script').each(function ()
if (this.src)
$('body').append(this);
else
var content = $(this).html();
eval(content);
);
After each postback, I can dynamically load any number of sections I need, and then hook up all the events in js via the functions pageLoad and evaluateScripts
answered Apr 9 at 20:57
Tommy GodseyTommy Godsey
335 bronze badges
335 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367142%2fhow-to-run-javascript-in-dynamically-loaded-user-controls-ascx%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1. I would look into .NET Core or something more modern. 2. Ditch jQuery. 3. I don't see how your JavaScript would ever work. You would have to do your function naming/variable magic outside of the script tag. JavaScript has no idea what
<%=something%>
is or how to interpret it. I could see it working if you did all of this logic outside of JavaScript which producted a JavaScript file, then loaded that up. But I don't see how any of this would ever work with the logic in the script tag.– mwilson
Mar 26 at 22:40
This thread could also help at how you could go about doing it: stackoverflow.com/questions/6542079/…
– mwilson
Mar 26 at 22:42