What's The Error In this code - c#

this code works fine in php to generate the custom scripts generators
<?php
$arrs = array("script.js","ui.js","jform.js");
foreach ($arrs as $var)
{
?>
<script type="text/javascript" src="<?php echo $var ?>" >
</script>
<?php
}
?>
this not compiling even whts the probem in my code the aspx but its not working
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% lst %>" />
<%
}
%>

You error is with script line change the line to below resolve your one issue..i tried in my environment
<script type="text/javascript" src="<% =lst %>" />
Full code will be :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% = lst %>" />
<%
}
%>

Try with following code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% IList<string> list;
list = new List<string>();
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<% = lst %>" />
<%
}
%>

try this
<%# Page Language="C#" ..
<%# Import Namespace="System.Collections.Generic" %>
<% var list = new List<string>() { "script.js", "ui.js", "jform.js" };
foreach (string lst in list)
{
%>
<script type="text/javascript" src="<%= lst %>"></script>
<%
}
%>

Related

set path in chunks ASP.NET CORE MVC 2.2 WEBPACK 4 HtmlWebpackPlugin

ASP.NET CORE 2.2 WEBPACK 4 HtmlWebpackPlugin
How to set the right path?
problem src="../../wwwroot/public
to src="./public
webpack.config.js
new HtmlWebpackPlugin({
inject: false,
hash: true,
template: './Views/_BundledScriptsTemplate.cshtml',
filename: '../../Views/Shared/_BundledScripts.cshtml'
}),
\Views_BundledScriptsTemplate.cshtml
<% for (var item in htmlWebpackPlugin.files.chunks) { %><script src="<%= htmlWebpackPlugin.files.chunks[item].entry %>"></script><% } %>
<% for (var css in htmlWebpackPlugin.files.css) { %><link href="<%= htmlWebpackPlugin.files.css[css] %>" rel="stylesheet"><% } %>
\Views\Shared_BundledScripts.cshtml
<script src="../../wwwroot/public/js/app.production.bundle.92a519909b5dceede661.js?e345faf977c3f42e2bc1"></script>
<link href="../../wwwroot/public/css/app.production.bundle.95b81d019e75eb4d5400.css?e345faf977c3f42e2bc1" rel="stylesheet">
Makes kostil
_BundledScriptsTemplate.cshtml
#{
StringBuilder jsUrl = new StringBuilder();
StringBuilder cssUrl = new StringBuilder();
}
<!--CSS-->
<% for (var css in htmlWebpackPlugin.files.css) { %>
#{
cssUrl = new StringBuilder("<%= htmlWebpackPlugin.files.css[css] %>");
cssUrl.Replace("../../wwwroot", ".");
<link href=#cssUrl rel="stylesheet">
}
<% } %>
<!--JS-->
<% for (var item in htmlWebpackPlugin.files.chunks) { %>
#{
jsUrl = new StringBuilder("<%= htmlWebpackPlugin.files.chunks[item].entry %>");
jsUrl.Replace("../../wwwroot", ".");
<script src=#jsUrl></script>
}
<% } %>

CS1525: Invalid expression term '<'

Using this code within an aspx file
<% if(storeid=1) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer></script>
<% } %>
<% else if(storeid=2) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-efgh.js" async defer></script>
<% } %>
<% else if(storeid=3) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-ijklmn.js" async defer></script>
<% } %>
<% else if(storeid=4) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-opqrs.js" async defer></script>
<% } %>
Compiling this gives me this error
Compiler Error Message: CS1525: Invalid expression term '<'
Source Error:
Line 62:
Line 63: // Specific Code test 17.4.2014
Line 64: <% if(storeid=1) { %>
Line 65: <script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer> </script>
Line 66: <% } %>
All of the <% and %> look ok. Where is it falling down?
It's storeid == 1, not storeid=1.
Replace this line:
<% if(storeid=1) { %>
With:
<% if( storeid == 1 ) { %>
By the way, this is true for all of the rest equality checks in the other lines of code.
Your storeid='1' is wrong and the rest of your else statement. it should be ==.
storeid == 1 and not storeid=1.
<% if(storeid=1) { %>
should be
<% if(storeid==1) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-abcd.js" async defer></script>
<% } %>
<% else if(storeid==2) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-efgh.js" async defer></script>
<% } %>
<% else if(storeid==3) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-ijklmn.js" async defer></script>
<% } %>
<% else if(storeid==4) { %>
<script src="//d3c3cq33003psk.cloudfront.net/opentag-1234-opqrs.js" async defer></script>
<% } %>

Pass model name as a parameter

I have a partial view where I pass the model name to populate it, is there any way to pass model name as a parameter based on the controller action executed?
<div id = "Details" >
<% List<Freshmen>() = model based on controller action executed %>
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
Controller Action:
public ActionResult FreshmenDetails(string id)
{
DataContext Student = new DataContext();
var FreshmenDetails = Student.Freshmen.Where(a => Convert.ToInt64(a.Id) == Convert.ToInt64(id)).ToList();
return PartialView("FreshmenDetails", FreshmenDetails );
}
I have 3 more Actions each for SophomoreDetails(),JuniorDetails(),SeniorDetails()
Currently I am displaying Partial View like this:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
<% using (Html.BeginForm("SophomoreDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SophomoreDetails", new List<Sophomore>()); %>
<% } %>
<% using (Html.BeginForm("JuniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("JuniorDetails", new List<Junior>()); %>
<% } %>
<% using (Html.BeginForm("SeniorDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("SeniorDetails", new List<Senior>()); %>
<% } %>
</div>
I want something like:
<div id = "Details" >
<% using (Html.BeginForm("FreshmenDetails", "Students")) %>
<% { %>
<% Html.RenderPartial("FreshmenDetails", new List<Freshmen>()); %>
<% } %>
</div>
I am having a hard time figuring out exactly what your after. That being said, have you tried using reflection?
return PartialView(Model.GetType().Name, new { // Partial View object });
Further more, you can use Model.GetType().Name to get the model object name and use it where you want.
That should work for you.

Getting variables value in aspx page rather than code behind

Hello Guys i am having following code in my Content page of my master page
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Description.aspx.cs" Inherits="Description" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="http://maps.google.com/maps?file=api&v=2&key=key&sensor=true"
type="text/javascript"></script>
<script type="text/javascript">
function initialize()
{
if (GBrowserIsCompatible())
{
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(20,30), 10);// I want to change these coordinates
map.setUIToDefault();
}
}
$(document).ready(function ()
{
initialize();
});
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div id="main">
</div>
</asp:content>
In above code i want to fetch coordinates from code behind .
I am having coordinates in code behind ex string coor = "20,30";
so inside page load event how can i put the value of this variable in java script code.
Should I create a String literal there ? or something else.
You could store them in a property and then use them:
map.setCenter(new GLatLng(<%= Lat %>, <%= Lon %>), 10);
where Lat and Lon are two public integer properties in your code behind. You could also use methods if you will.

How to echo something in C# in an .aspx file

I know you can do this
<%= Request.Form[0] %>
But how do you do something like this?
<% if(Request.Form[0]!=null)
echo "abc";
%>
<% if(Request.Form[0]!=null)
Response.Write("abc");
%>
Maybe you want to show the form value unless it doesn't exist then show "abc"?
<%= Request.Form[0] ?? "abc" %>
Or you could can use if blocks and normal markup inside.
<% if(Request.Form[0]!=null) { %>
<div class="echo">abc</div>
<% } %>

Categories