I want to set HTML inside a div in my code, but the end result looks like this:
Here is my code:
protected void grdTags_SelectedIndexChanged(object sender, EventArgs e)
{
string RelatedText = grdTags.SelectedRow.Cells[5].Text;
dv.InnerHtml = RelatedText;
}
Here is the ASPX:
<div id="dv" runat="server">
</div>
It looks like you're passing in text into HTML. Consider using HttpUtility.HtmlEncode()
I've not tested this, but try:
dv.InnerHtml = HttpUtility.HtmlDecode(grdTags.SelectedRow.Cells[5].Text);
Related
Please consider the following:
code behind:
public void InitiateTable()
{
Controls.Add(new LiteralControl("<table class=\"table table-invoice\" >")); //Line that gave me error
...
Controls.Add(new LiteralControl("</table>"));
}
on my ASP.Net page:
<% InitiateTable(); %>
When I run the code, it gives me an error saying:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Can someone please explain what I have done wrong?
Thank you.
Note Changed LiteralControl to Control as suggested in comment.
You cannot modify the controls collection where the container contains <% %> code blocks as the error message tells you. Controls.Add will modify the controls collection. To get around this:
In your aspx page change
<% InitiateTable(); %>
to
<asp:Literal runat="server" id="mytable" />
then in your page load method, add a call to InitiateTable()
then in your InitiateTable() method change your code to something like this:
public void InitiateTable()
{
var literalControlValue = new StringBuilder();
literalControlValue.Append("<table class=\"table table-invoice\" >");
...
literalControlValue.Append("</thead>"));
mytable.Text = literalControlValue.ToString();
}
Controls.Add(new LiteralControl("<table class='table table-invoice'>"));
The issue is when you use Controls you are referencing the Page itself rather than the area you call InitiateTable.
If you're inserting controls into this area, this is what a Placeholder is for:
<asp:Placeholder id="phTable"></Placeholder>
Then in your Page_Load or some other event code:
private void Page_Load(object sender, System.EventArgs e)
{
phTable.Controls.Add(new LiteralControl("<table class=\"table table-invoice\" >"));
phTable.Add(new LiteralControl("</thead>"));
}
I have passed a value through a url to a C# .net page using query string. The url of the page looks like this:
http://contoso.com/products.aspx?field1=value1
And in C#, I have this to catch it:
String myValue = Request.QueryString["field1"];
What im looking to do is use this value in the page, something like this:
<h1><%# Eval("myValue") %></h1>
How would I go about doing this? Obviously this HTML code doesn't work. I have exhausted some google searches on the subject so any information would be appreciated!!
You can either create a Property on your page and use code tags, or set the h1 tag as runat="server" and set the value like that.
Property:
public string MyString{ get; set; }
public void Page_Load(object sender, EventArgs e)
{
MyString = Request.QueryString["field1"];
}
Then in your markup:
<h1><%= MyString %></h1>
Alternatively, using the runat="server" method on the h1 tag:
Markup:
<h1 id="myH1" runat="server"></h1>
Code:
myH1.InnerText = Request.QueryString["field1"].ToString();
Try to add runat="server" and id to your h1 tag, so you can use it in cs-file.
HTML:
<h1 id="myHeader" runat="server"></h1>
CS:
myHeader.InnerText = myValue;
In your aspx file, you can define a text field like so:
<h1 Id="label" runat="server"/>
Then, in your code behind file add:
label.InnerText = Request.QueryString["field1"];
I want to change some string located in html file when it loads. For example, i have a html file:
<html>
<head>
<title>MyTitle</title></head>
<body>
Some Text
<script type='text/javascript'>
/*some script*/
var myString = "TargerInfo";
/*some script*/
</script>
</body>
</html>
I use Page_Load method in code-behind file:
protected void Page_Load(object sender, EventArgs e)
{
/*Insert necessary snippet of code*/
}
What code should i use to change string "TargerInfo" to "OtherString" ?
[EDIT]
Sorry, that I have forgot to mention
I can add any info to html page only in code-behind class, because this page isn't generated by me.
I think i should use something like this:
1) load html file
2) find my string
3) replace it
4) send html file
There is an aspx page, but i add only some part of code and other code is added by VS
Unless I'm missing something (because this seems like a bit of an ASP.NET 101), you have several options...
Create a variable in the code-behind and then use that...
protected string _newText = "";
protected void Page_Load(object sender, EventArgs e)
{
_newText = "OtherString";
}
And then in the ASPX...
var myString = "<%=_newText%>";
Otherwise you can use the <asp:Literal> control
UPDATE
After an extensive chat with #andDaviD it turns out that the javascript is in a Master page held in SharePoint Foundation.
The Master page is being referenced in his Content page via the DynamicMasterPageFile attribute in the <%# Page directive, and that is why he said he is able to update some part of the code, but not others.
I am still unsure as to whether it is possible for the Master page to be modified (either by himself or an administrator), that is something he needs to find out from the people in charge at his company. But I believe the adding of a property or method to the Master Page to provide what he needs is the only sensible option.
You could use inline aspx code tags:
<script type='text/javascript'>
/*some script*/
var myString = "<%= getTargetInfo() %>";
/*some script*/
</script>
in codebehind:
protected String getTargetInfo()
{
return "OtherString";
}
You could use a literal:
protected void Page_Load(object sender, EventArgs e)
{
literal.Text = string.Format("var myString = \"{0}\"", targetInfoValue);
}
Markup:
<html>
<head>
<title>MyTitle</title></head>
<body>
Some Text
<script type='text/javascript'>
/*some script*/
<asp:Literal id="literal" runat="server" />
/*some script*/
</script>
</body>
</html>
You can have it in a hiddenfield in asp.net and change the hidden field in code behind.
in your code behind:
public string otherString;
otherString = "some text" //update the string with the value oyu want.
in aspx page put this line in any place you want to see the otherString.
<%=otherString%>
The above is the code in my .aspx page.
How this can be added from code behind dyanmically?
<ul runat="server" id="1">
<li>abc
<ul runat="server" id="2">
<li>3</li>
<li>2</li>
</ul>
</li>
</ul>
you can take the analogy of the tutorial given in this link :
http://neimke.blogspot.com/2011/01/create-delicious-user-interface-for.html
it worked for me - It dynamically adds the list items using the given code below using jquery .. check it pout ...
<li id="tagInputListItem"><input class="tagInput" id="tagInput" /></li>
You can put a PlaceHolder in your .aspx and give it an id, then use that id in code behind page and add controls to that placeholder.
For more information you can see in here.
And if you're really sure about "runat=server" attribute maybe this post of mine it's useful (here)
If you need clarifications give me a feedback.
You must use the "InnerHtml" property of "sidebarmenu1" control.
protected void Page_Load(object sender, EventArgs e)
{
this.loadHtml();
}
So you can generate every list item code and add it to the InnerHtml:
private loadHtml()
{
this.sidebarmenu1.InnerHtml = GetListHtml().ToHtmlString();
}
And a little example for this GetListHtml:
public string GetListHtml()
{
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.AppendLine("<li>Flat");
htmlBuilder.Append("<ul runat="server" id="sidebarmenu2">");
htmlBuilder.AppendLine("<li>Flat 1`enter code here`</li>");
htmlBuilder.Append("<li>Flat 2</li></ul>");
return htmlBuilder.ToString();
}
This GetListHtml method can call to a DAL or load data from any other place... use a foreach to load every item...
You can use ASP Literal to populate data from back-end code
eg. if you have literal with id ltrNavigation
protected void Page_Load(object sender, EventArgs e)
{
ltrNavigation.text = "";
if (!IsPostBack)
{
ltrNavigation.text += "<ul id='sidebarmenu1'>";
ltrNavigation.text += "<li><a href='#'>Flat</a></li>";
ltrNavigation.text += "</ul>";
}
}
I'm new to C# .NET. I would like to ask how this works... What I want is just to show an age selection from 1 to 100.
Inside the .aspx file I put this code, I used data binding for the variable listAge.
<asp:DropDownList ID="AgeDropDown" runat="server">
<%# listAge %>
</asp:DropDownList>
Here's the code-behind for it:
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 101; i++)
{
string listAge;
listAge = "<asp:ListItem>"+ i +"</asp:ListItem>";
}
}
The error shown inside the .aspx is:
Error Creating Control: AgeDropDown - Code blocks are not supported in this context.
Because of the variable listAge?
Thank you for the help!
Drop the <% %> section in .aspx and in code behind you should do something like this:
protected void Page_Load(object sender, EventArgs e)
{
AgeDropDown.Items.Clear();
for (int i = 1; i < 101; i++)
{
AgeDropDown.Items.Add(new ListItem(i.ToString(),i.ToString()));
}
}
From another point of view there are several flaws in your code:
You are generating ASP.NET tags in code behind. ASP tags are processed on the server and are rendered into html tags. You were practically inserting a tag in html, which browsers will render as simple text since it's not a valid HTML tag.
You were creating a new listAge variable on each iteration of the for loop. Even if the code would work it would display just the last item
You could use the server version of AgeDropDown.
ListItem li;
for (int i = 1; i < 101; i++)
{
li = new ListItem(i.ToString(), i.ToString());
AgeDropDown.Items.Add(li);
}
Is this in asp.net or MVC?
Probably
... <%# listAge %>
should be
... <%= listAge %>