How Embedded Code Blocks In WebPages
Sep 28, 2010where we use <%@ %> <%= %> <%# %> etc.
what else asp tags can be added in asp.net web pages?
where we use <%@ %> <%= %> <%# %> etc.
what else asp tags can be added in asp.net web pages?
I am writing a dynamic control that I want to pass in something along the lines of the following:
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "/Home.aspx?id=<%= DataBinder.Eval("PageId") %>";
myLink.Text = "The link";
myControl.Controls.Add(myLink);
I want to be able to resolve the myLink.NavigateUrl at run-time according to the context that myControl puts the control into in the end (this is not a WebControl and therefore adding to the collection of controls doesn't output to page).
Is it possible to do something like this in regular ASP.NET (or something similar):
<ul>
<% foreach (var item in Model) { %>
<li id="<%: item.Id %>">
blah blah
</li>
<% } %>
</ul>
I need to do a gird, but I want to control how the html table is output.
I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this:
<span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %>">Vote Up</span>
Ideally I'd like to do this:
<span class="vote-up@{puzzle.UserVote == VoteType.Up ? "-selected" : ""}">Vote Up</span>
However there's two problems here:
vote-up@{puzzle.UserVote .... is not treating the @ symbol as a start of a code block @puzzle.UserVote == VoteType.Up looks at the first part @puzzle.UserVote as if it's supposed to render the value of the variable.
I'm building an asp.net app using themes, and I've assigned a theme to the app using the web config.
I have a bookmark icon that I want to use for my page and it is located in the themes directory, but I am having trouble referencing the themes location from a link tag in my header.
First I tried putting a code block inside the link tags href element, which did not work. Instead all it did was html encode the <% characters and output it directly to the browser:
<link rel="shortcut icon" href="/App_Themes/<%=Page.Theme %>/images/bookmark.ico" type="image/x-icon" runat="server"/>
I am able to put a code block inside an element in an hr tag though, so I don't know why it won't work in a link tag:
<hr test="<%=Page.Theme %>"/>
Then I tried doing a Response.Write inside the head tag, but I got an error saying the Controls collection cannot be modified because the control contains code blocks:
<% Response.Write("<link rel="shortcut icon" href="/App_Themes/" + Page.Theme + "/images/bookmark.ico" type="image/x-icon"/>"); %>
I also tried it just with a string literal, and got the same error:
<%= "<link rel="shortcut icon" href="/App_Themes/" + Page.StyleSheetTheme + "/images/bookmark.ico" type="image/x-icon"/>" %>
Is there any way to reference something from the themes directory inside the link tag?
I'm trying to do this in both an ASP.NET 2 and an ASP.NET 2 MVC app.
i saw few of solutions for this exception (like putting it in place holder, or replace the <% to <# ...etc) , but i don't get the idea about it, i mean what is the controls collection? and why it can't be modified if the added control contains code blocks?
View 2 RepliesI recently put some code <% %> code blocks in my Master Page. Note I've read of the "fix" for either moving things out of <head> or using <%# %> but neither of them work well for my application.
Now the weird thing is that I only get this error on one page of mine. All the other pages seem to work fine, so what actually causes this error? There is nothing I can think of that is unique about this page. It uses the script manager as does other working pages and there is just nothing extraordinary about this page. It does have quite a few custom controls on it, so hunting down what is different in this page is more difficult than usual.
So what actually causes the Controls collection cannot be modified because the control contains code blocks exception?
Error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)
I have the following in my master file to load a dynamic seasonal background image:
HTML Code:
<style type="text/css">
body
{
background-image:url("<%=backgroundImage%>");
background-position: top center;
}
</style>
It works fine on most of my pages, but on one it throws the above error.
We are using Infragistics' WebDataGrid, but this question probably applies to all other grids. I am new to Infragistics (actually have worked with their controls years ago, but feeling new regardless!) and this application as well. So I'm looking to its existing code as a model.
I want to have a column called Tobacco with a Y or N value depending on if Smoker is true/false.
Ideally, the whole thing would be something like this:
Code:
<ig:BoundDataField Header-Text="TobaccoUse" Key="Smoker">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.Smoker") != DBNull.Value ? (DataBinder.Eval(Container, "DataItem.Smoker") == true ? "Y" : "N") : "N"%>
</ItemTemplate>
Because I want to check for a null first and when I know I don't have one, test the value.
But since I am getting an error on that I am trying to start simple and build it up. I am getting the same error on this:
Code:
<ig:BoundDataField Header-Text="TobaccoUse" Key="Smoker">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.Smoker") == true ? "Y" : "N"%>
</ItemTemplate>
The error is: Code blocks are not supported in this context.
Why is this a code block and should it be something else? What is my context?
This is an example of a line that works:
Code:
<ig:TemplateDataField Header-Text="DOB" Key="DOB">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.DOB") != DBNull.Value ? Convert.ToDateTime(DataBinder.Eval(Container, "DataItem.DOB")).ToShortDateString() : ""%>
</ItemTemplate>
</ig:TemplateDataField>
I don't know if I need a Convert.ToBoolean in there somewhere but I tried and it didn't work and at this point I am just guessing which will take a long time for me to get it right.
I have web.confing file when adding tag page for set theme
After running project i have this error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
<pages theme="Theme" styleSheetTheme="Theme" clientIDMode="AutoID" />
I am getting http exception in of the method of class file (search.aspx.vb) like "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)",
I have tried placing Java script function in the body of Master page from header, whereas I just removed the Java script tag and its contents in search.aspx (as of its not required now). Still getting exception.
I also know that if we have something like this <%= in Java script function we need to replace with <%#. But I don’t have this kind of code in any function.
I want use meta tag in my page that read keyword from database
I set this code in my product.aspx page
protected void Page_Load(object sender, EventArgs e) {
SqlCommand _cmd = new SqlCommand("select Keyword1 + ',' + Keyword2 + ',' +Keyword3 from House_info where BehCode=1115", _cn);
_cn.Open();
SqlDataReader _dr = _cmd.ExecuteReader();
while (_dr.Read())
[Code] ....
It worked correctly and show my keywords from database and show it in my product.aspx metatag
I have two other page
index.aspx and store.aspx in index.aspx i have 1button and 1 TextBox when user type their BEHCODE(columns name in users table) in TextBox it go to store.aspx page and show that users information on store.aspx
Now i want set meta tag in store.aspx page and i want fill store.aspx meta tag from my users table in data base
This is index.aspx
protected void ImageButton3_Click(object sender, ImageClickEventArgs e) {
string data = Server.UrlEncode(txtNumeric.Text);
SqlCommand _cmd = new SqlCommand("traidname", _cn);
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Parameters.Add("@BehCode", data);
[Code] ....
Here when i run website this error occur
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
In this line
this.Page.Header.Controls.Add(htmlMeta);
I use this code too
<script type="text/javascript">
$("input[id$=btnButton]").live("click", function () {
return confirm("Do you want to submit?");
});
</script>
But it didn't worked this problem just happen here when i used meta tag code in other page it worked correctly . Why this happen?
i have a doubt difference between object code and embeded code .
View 2 RepliesThe code on one of my aspx pages is giving me this error:
I'm getting this on the code behind for export to excel on one of my pages.
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).It is referring to this line Me.GridView1.Parent.Controls.Add(frm)
I have this code in the aspx page. I am using two gridviews. I need to have this on the page any way I can fix the code so I can use that export to excel you created?
<% If(Session("PendingClaims"))%>then display gridview1<%Else%>
Then display gridview2<% End If %>
I was working on an asp.net 3.5 website and used ajaxtoolkit in it and when I use it in my page it was registered as below
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
and it was working well even After I upgraded my wevbsite to .net version 4 but when I started a new website from scratch in vs2010 using .net4 when I use ajax toolkit in this new site it is registered as folowing
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
and when I run the page I get the error The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) I don't Know what is the reason for this. I noticed another thing. in my old website I was using thckbox script by using the files common.js, jquery-1.3.2.js,jquery.thickbox.js and puting them in a Javascript folder ad then registering them in Script manager as following
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Javascript/jquery-1.3.2.js" />
<asp:ScriptReference Path="~/Javascript/jquery.thickbox.js" />
<asp:ScriptReference Path="~/Javascript/common.js" />
</Scripts>
</asp:ScriptManager>
this was working well also but in my new site the jquery file version was jquery-1.4.1.js but also I added the old version f the javascripts files in the built in Scripts folder that is created by default b the new site and followed the same way in registering them in the script managerchange the JavaScript folder to the built in folder Scripts
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.3.2.js" />
<asp:ScriptReference Path="~/Scripts/jquery.thickbox.js" />
<asp:ScriptReference Path="~/Scripts/common.js" />
</Scripts>
</asp:ScriptManager>
but this doesn't work also in the new site I think that there is something diffirent in the 2 case which leads to this change in performance I wish your support and advice about how to solve this matter
I have use embedded code to assign the value to label's text property.But the assign value is not rendering.Here is my code sample,
<%lblProposedYear.Text="Hello"%>
I can not display the text "hello" at the time of page load.
Is there any documentation or tutorials anywhere on what can be done or put between these magical symbols in ASP.NET aspx files? I've tried a number of resources though they all seem aimed at Classic ASP.I'm already writing protected functions for updating and grouping in my grids using Eval. But, for example, I noticed the following in my code :-
ConnectionString="<%$ ConnectionStrings:WORK_MyConnectionString %>"
I presume that $ ConnectionStrings is a macro or built-in function of some sort and I'm trying to track down what else is available in this area. I would also like to use the
<% if (my function) then .....endif %>
to conditionally write items to the page.
I have a view in ASP.NET MVC. It takes the model object and iterates over a list of strings and displays them in a table row, like so:
Details
<table>
<tbody>
<tr>
<th>Values in the database</th>
</tr>
<% foreach (string value in Model.lstDistinctValues)
{%>
<tr>
<%=value%>
<%} %>
</tr>
</tbody>
The problem is that the values appear ABOVE the header. So 'Values in the database' appears at the bottom, while the values are at the top.
I have a multiline (> 50 lines) textbox containing plain text. I want to add either a session variable to the text in this box. In asp I would have done it by putting in <% %> code blocks but in .net I am getting the following error: "Code blocks are not supported in this context". I assume therefore that this would need doing in code behind.
Is there a quicker way than putting all the text from the textbox in a string in code-behind and then adding the variable on like this? I would like to keep the text in my aspx page if possible.
I'm receiving this error on a page that previously worked fine, in fact the only change I've made to the page recently was to add another asp:TextBox and asp:RequiredFieldValidator control.
The page already had numerous ASP.NET controls on it, so I cannot see why these extra controls would make a difference, anyway I shall post the code below and hopefully you can see what the error is:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MeetingGenerator.ascx.cs"
Inherits="usercontrols_MeetingGenerator" %>[code]......
I want to integrate paypal inside my shopping cart. For that it requires a html form to be embeded inside .aspx page with the action to paypal and method is post. Inside that form there are some hidden input filed. I want to assign one of those hidden field's value from server side code so i need to access that filed from server side. But how? Here is my form:
<form action="[URL] method="post" id="PaypalForm">
<input type="hidden" name="cmd" value="_cart"/>
<input type="hidden" name="upload" value="1"/>
<input type="hidden" name="business" value="[URL]
<input type="hidden" name="item_name_1" value="Item Name 1" />
<input type="hidden" name="amount_1"/>
<input type="submit" value="PayPal" />
</form>
I want to access amount_1 filed.
Using StringBuilder im building my object in code behind so its customize for our needs.. I placed a Literal control within the modalpopup and populating it when you click on a button..On my test page everything works great to display it and when fed a value video file, it plays.. on that page, it wasnt contained within a modalpopup.Now on my actual dev page that i need it on, i placed the literal control within the modalpopup, and when you click on the button, it builds the object..my modal popup comes up and everything shows up except the object which is weird, because within the stringbuilder i have 4 buttons below the object and those show up, just not the object control itself.. can a object not be built and displayed like this?What makes it weirder is that when the modal popup opens, you can view the source of the page and my entire customize object control is there, but it just doesnt render on the page..
View 4 RepliesI've been trying to get into TDD but am hitting some stumbling blocks. I suppose a simplified example would be the best way to explain. My first assumption is that I start from the top. I want a web page that displays a list of categories say. So I write a test for a CategoryController whose Index action should return this list:-
Code:
[code]....
Bingo our test will pass. Now my problem here is how does TDD drive us beyond this point? Our tests have passed and it fulfills out requirements but of course we know that a fully working app would need say an ICategoryService to be passed into the Controller and the controller would use this to retrieve the categories.
I want to create a usercontrol that behaves like the Label usercontrol or the HyperLink usercontrol.
What I mean - the Label usercontrol has the Text attribute, and the text can also be set with the following way:
<asp:Label runat="server" id="lblTest">Text Here</asp:Label>
If I wish to create a usercontrol that can set the text between blocks to the actual Text attribute of the control.
Do I need to use Templated UserControl? If no - what is the correct way?
In ASP.NET I would like your opinion regarding what is the best way for Handling Exceptions Would you to use try/catch blocks? Better to use try/catch blocks on every single piece of code/component? Or in wich situations?
View 5 Replies