So I have a div, in which I have an a element. I want the other div to Render another page (RenderPage) as soon as the a element in the other div is clicked.
<div id="Left" style="width:29.49%; height:100%; border-style:solid; border-color:darkgray;">
<h1 id="Pages"> Articles </h1><br />
<a OnClick="LoadUE()">UE</a>
<a></a>
<script>
function LoadUnity() {
}
function LoadUE() {
document.getElementById("Body").innerHTML = #{RenderPage("Bottom.cshtml")};
}
</script>
</div>
<div id="Body" style="width:69.49%; height:100%;">
</div>
The RenderPage method only works on the server when the page is first executed. If you want to load partials in ASP.NET Web Pages (which is what you are using) from client script, you should remove the leading underscore from the file name and then create an AJAX request to load it. Here's an example that uses Fetch:
#section scripts{
<script>
function loadUE() {
fetch('/bottom')
.then((response) => {
return response.text();
})
.then((result) => {
document.getElementById('body').innerHTML = result;
});
});
</script>
}
As a side note, the ASP.NET Web Pages framework is pretty much dead. If you are just learning and have a choice, you should use Razor Pages instead: https://www.learnrazorpages.com/
Related
I have a small angular app that makes an api call based on a month/year from a drop down list in asp. It is meant for that api call to be made whenever year or month drop down lists are changed.
The first/original load works just fine. According to the debugger on any change I'm hitting the api on the change, and getting a return. The entire "card-widget"/ChartController div is disappearing (even with css properties commented out).
My ASP code:
<div id="card">
<div>
</div>
<div id="card-widget" ng-app="ChartApp" ng-controller="ChartController" style="width:350px; height: 350px;">
<div>
<asp:DropDownList ID="lstMonth" runat="server" ng-model="month" ng-change="updateChart()"></asp:DropDownList>
<asp:DropDownList ID="lstYear" runat="server" ng-model="year" ng-change="updateChart()"></asp:DropDownList>
</div>
<div>
<canvas id="Chart" height="350" width="350"></canvas>
</div>
</div>
<div id="card-widget-fail" style="width: 350px; height: 100px;">
<label>No chart</label>
</div>
</div>
My Angular code:
var app = angular.module("ChartApp", []);
app.controller('ChartController', function ($scope, $http) {
$scope.updateChart = function () { getChart() };
var dataChart = document.getElementById("Chart").getContext('2d');
var year = document.getElementById("<%=lstYear.ClientID %>");
var month = document.getElementById("<%=lstMonth.ClientID %>");
getChart();
function getChart() {
//api call to load chart data based off month.options[year.selectedIndex].text & year.options[year.selectedIndex].text
}
});
The reason getChart() is not being called is because it's not present on the $scope element. Use $scope.getChart = function() { ... } instead. I recommend reading this documentation page, specifically the part "Scope as Data-Model".
I am in a tricky situation,
Scenario- There are gadgets which are to be shown in mobile site.
2.One of the gadget is RSS which user can add multiple times for different topics like one for security, one for news, one for events.
3. So we have 1 partial view for RSS, but if the user has 2 RSS gadgets then the same partial view should load with different gadget name. Here the functionality is working fine using foreach loop.
#foreach (var rssFeed in Model.RSSFeedList)
{
<article class="bm2014_bigBoxWrap bm2014_bigBoxRSS bm2014_paginate">
<img src="~/Content/images/iconRefresh.png" width="20" height="20" alt="refresh icon" title="refresh icon">
<div class="bm2014_expColCtrl">
<h1 class="bm2014_bigBoxHdr">
<span class="bm2014_hiddenHdr"> </span>
<!-- for markup validation -->
#if (rssFeed.Channel.Name == "xyznews")
{
<span>#Html.Label(Labels.Title_xyz)</span>
}
else if(rssFeed.Channel.Category=="xyzRSSFeed")
{
<!--<span>#Html.Label(Labels.xyz) - #rssFeed.Channel.Title</span>-->
<span>#rssFeed.Channel.Title</span>
}
<span class="bm2014_expColBtn"><img src="~/Content/images/iconPlus.png" width="32" height="32" alt="expand collapse icon" title="expand collapse icon"></span>
</h1>
<div class="bm2014_expColContent bm2014_bellnetRSSWrapper" id="bm2014_divBellnetRSS">
#Html.Partial("~/Views/Shared/_RSS.cshtml", rssFeed)
</div>
</div>
</article>
}
<!-- RSS Panel end here -->
Problem is with refresh issue
if i hit the refresh button for selected gadget, it is by default taking only one RSS name and loading the content irrespective of different gadget selected.
partialview page code-
#model Models.RSSFeed
#{
Layout = null;
}
<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.multilevelpushmenu.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-simple-pagination-plugin.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.dataTables.js" type="text/javascript"></script>
<script type="text/javascript">
/* scripts to load after the DOM gets ready */
$(function () {
offCanvasMenu(); // trigger Javascript controlled OFF canvas menu after AJAX refresh
$.ajaxSetup({ cache: false });
$("article.bm2014_bigBoxRSS #btnRefresh").on('click', function (event) {
var $rssGadgetID = $(this).parents("article.bm2014_paginate").find("div#bm2014_divBellnetRSS");
var $rssGadgetLdr = $rssGadgetID.find("div#bm2014_gadgetLoader");
ajaxLoaderHeightCtrl($rssGadgetID, $rssGadgetLdr);
// AJAX control
$.ajax({
url: '#Url.Action("RefreshBellnetRSS", "Home", new { feedName = Model.Channel.FeedName })',
contentType: 'application/html; charaset=utf-8',
type: 'GET',
dataType: 'html',
success: function (result) {
$rssGadgetLdr.fadeOut(100, function () {
$rssGadgetID.html(result);
var moveRSS = $("article.bm2014_bigBoxWrap").css("float");
if (moveRSS == "left") {
mQueryAJAX("portrait", $rssGadgetID);
}
else if (moveRSS == "none") {
if (window.matchMedia("(orientation: portrait)").matches) {
mQueryAJAX("portrait", $rssGadgetID);
}
if (window.matchMedia("(orientation: landscape)").matches) {
mQueryAJAX("portrait", $rssGadgetID);
}
}
hideTableHeader();
});
},
error: function (xhr, status) {
alert(status);
}
});
});
});
</script>
<div class="bm2014_gadgetLoader" id="bm2014_gadgetLoader" style="display: none;">
<img src='#Url.Content("~/Content/Images/loaderGadget.gif")' width="48" height="48" alt="ajax loader image" title="ajax loader image">
</div>
<div class="bm2014_strategyContent">
#if (Model.url != null)
{
<table>
<thead>
<th>dummy header - to be hidden</th>
</thead>
<tbody>
#foreach (var url in Model.url)
{
<tr>
<td>
#url.Name
</td>
</tr>
}
</tbody>
</table>
}
</div>
need help/suggestions
If I understand correctly, you need to have 3 refresh buttons for 3 RSS gadgets e.g. one for security, one for news, one for events.
In the current example, every time you call the code to apply click event, you replace the earlier event and the 'feedname' parameter in url for ajax call also gets updated.
$("article.bm2014_bigBoxRSS #btnRefresh").on('click', function (event) {
......
}
You need to be able to distinguish between the refresh buttons and pass correct parameters. One way is to use data-feedname attribute on your btnRefresh anchor tag (if using HTML5)
I'm trying my hand and Asp.net MVC 4 and having an issue with an Ajax form returning a whole new page instead of just updating a div.
Here's the razor html code:
<div class="All">
<div class="Search">
#using (Ajax.BeginForm( new AjaxOptions { UpdateTargetId = "CurrentSku" } ))
{
#Html.Label("Enter Sku:")
#Html.TextBox("textBox1")
<input type="submit" value="Find" />
}
</div>
<div id="CurrentSku">
<span>No Sku selected.</span>
</div>
and here's the controller:
public ActionResult Index()
{
// Pay no attention to this, just a place holder
return View( db.xInventoryExt.Take(1) );
}
[HttpPost]
public ActionResult Index(string textBox1)
{
if (db.xInventoryExt.Count(a => a.InvtID == textBox1) < 1)
{
return Content("Sku not found.", "text/html");
}
var ret = db.xInventoryExt.First(b => b.InvtID == textBox1);
return Content(ret.ToString(), "text/html");
}
I was reading that this sometimes happens when not including MicrosoftAjax.debug.js but I can't find a copy of that file anywhere.
I was reading that this sometimes happens when not including MicrosoftAjax.debug.js
Actually you need to include the jquery.unobtrusive-ajax.js script. MicrosoftAjax.debug.js was part of older versions of ASP.NET MVC and is completely obsolete now. So basically if you are using ASP.NET MVC 4 and using the default bundles (~/App_Start/BundleConfig.cs), in your _Layout.cshtml you could simply have the following towards the end of the DOM:
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryval")
#RenderSection("scripts", required: false)
Now your Ajax.* helpers will work and also you would have enabled unobtrusive jQuery validation. This will happen because of the way this ~/bundles/jqueryval bundle is defined:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
If you are not using bundles then simply include the corresponding scripts in that order:
<script type="text/javascript" src="~/scripts/jquery-1.8.2.js"></script>
<script type="text/javascript" src="~/scripts/jquery.unobtrusive-ajax.js"></script>
#RenderSection("scripts", required: false)
I have a standalone html page that contains a dojo DataGrid that works just fine. I am including the relevant code below.
<script type="text/javascript">
var readStore, grid;
var gridLayout = [
new dojox.grid.cells.RowIndex({ name: "Row #", width: 5, styles: "text-align: left;" }),
{
name: "Name",
field: "name",
styles: "text-align:right;",
width:30
},
{
name: "Type",
field: "type",
width:20
}
];
function initGrid()
{
readStore=new dojox.data.QueryReadStore({url:"/EG3/orgUnit/getChildren", requestMethod:"get"});
console.info("readStore initialized");
grid=new dojox.grid.DataGrid({store:readStore,
id:"grid",
delayScroll:true,
structure:gridLayout,
query:{id:2},
loadingMessage:"Loading..."
}, document.createElement('div'));
dojo.byId("gridContainer").appendChild(grid.domNode);
grid.startup();
}
dojo.addOnLoad(initGrid);
</script>
</HEAD>
<BODY class="claro">
<div id="list" class="list">
Table goes here
<div id="gridContainer">
</div>
</div>
Now, the problem happens when I try to include this page as a contentpane within a TabContainer. The TabContainer loads the page markup as is and does not fire the onLoad script of the page that contains the grid.
<div dojoType="dijit.layout.TabContainer" region="center" tabStrip="true" id="orgUnitTabs">
<div dojoType="dojox.layout.ContentPane" title="Info" selected="true" id="orgInfo" jsId="orgInfo">
</div>
<div dojoType="dojox.layout.ContentPane" href="/EG3/app/orgUnit/orgUnitChildren.gsp" executeScripts="true" parseOnLoad="true" extractContent="true" title="Children" id="children" jsId="children">
Children of the selected Org
<script type="javascript" event="onLoad">
initGrid();
</script>
</div>
Any ideas on how the onLoad script of the child page can be fired?
Thanks in advance.
you should check if the param executeScripts set to TRUE, and if you wanna use declarative method, you have to parse the domNode manually, see the detailed dojo.parse method:http://docs.dojocampus.org/dojo/parser
Not sure what the right answer is, but if I were you, I would try doing it all programatically instead of declaratively, trap errors, use console outputs to see what's happening and log events as it loads.
Using CSS, when a link is clicked it brings up a hidden DIV that contains a form. The user will then enter information and then submit the form. I'd like the hidden DIV to remain visible, and a 'success message' to be displayed after submission. Then the user will have the option of closing the DIV. I can't get it to work without reloading the page, which causes the DIV to become hidden again. Any ideas?
<body>
Click Me
<!--POPUP-->
<div id="hideshow" style="visibility:hidden;">
<div id="fade"></div>
<div class="popup_block">
<div class="popup">
<a href="javascript:hideDiv()">
<img src="images/icon_close.png" class="cntrl" title="Close" />
</a>
<h3>Remove Camper</h3>
<form method="post" onsubmit="email.php">
<p><input name="Name" type="text" /></p>
<p><input name="Submit" type="submit" value="submit" /></p>
</form>
<div id="status" style="display:none;">success</div>
</div>
</div>
</div>
<!--END POPUP-->
<script language=javascript type='text/javascript'>
function hideDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideshow').style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.hideshow.visibility = 'hidden';
}
else { // IE 4
document.all.hideshow.style.visibility = 'hidden';
}
}
}
function showDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideshow').style.visibility = 'visible';
}
else {
if (document.layers) { // Netscape 4
document.hideshow.visibility = 'visible';
}
else { // IE 4
document.all.hideshow.style.visibility = 'visible';
}
}
}
</script>
</body>
Forms by default submit content by changing to the specified page in its 'action' attribute. You will need to build additional scripts to prevent it from doing that and submit the data using either AJAX or jQuery then process the result.
Or you could simply use whatever language you're programming in to set the default visibility for the division. If the form data exists, display it by default, otherwise hide it by default.
How about using an AJAX call to post the form instead of posting back the whole page?
Instead of using a "submit" type for your button, you can use a "button" type and use a script called by onclick which will use ajax to submit the form and do whatever is necessary.
This defeats slightly the meaning of a form, but works well. You might also want to think about using a javascript library like prototype or similar (jquery, etc) that gives you the functionality to create a get or post array of your form in order to make it easier.