How do I use a C# string in my .aspx webpage? - c#

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"];

Related

html is not showing as html in div

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);

How to get public property in ImageURL?

I have one public property named "ID"
public int ID { get; set; }
and an asp Image control
<asp:Image ID="ImagePicture" runat="server" />
I want to get the ID into a ImageURL. I tried like this
<asp:Image ID="ImagePicture" ImageUrl='ImageHandler.ashx?ID='<%= ID %> runat="server" />
but I get an Error: Server tags cannot contain <% ... %> constructs.
Does anyone know how I can fix it, or propose me another way to get the ID?
Thanks
It would be a lot cleaner to just do this in the code behind:
ImagePicture.ImageUrl = string.Format("ImageHandler.ashx?ID={0}", this.ID);
Did you try to set the value by the page_load method?
Something like that should work:
ImagePicture.imageURL = "yourLink.ashx?id=" + valueParam;

How to assign href to anchor tag in codebehind c#

The text I inserted in database is
You also have to click on is link
This text I am assigning to the label when page loads. My requirement is when I click the "link" I need to redirect to certain page. How can I set the href to the above code in code behind .
Try to use HyperLink.
<asp:HyperLink id="hyperlink1"
ImageUrl="images/pict.jpg"
NavigateUrl="http://www.microsoft.com"
Text="Microsoft Official Site"
Target="_new"
runat="server"/>
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.aspx
You should add runat="server" to your anchor and then give an ID to it. So you can edit href property in codebehind.
in html side:
in code behind: xxxxx.HRef = "bla bla"
Look at this:
How do you set href attribute of anchor tag that is within a repeater in code behind?
Assuming you can slightly change the format of what you put in the database then I'd do something along these lines:
string labelFromDatabase="You also have to click on is link ";
string url = "mypage.aspx";
myLabel.Text = String.Format(labelFromDatabase, url);
Adding in the {0} placeholder into the database held string means you can easily just use String.Format to put in whatever url you want.
Main things to be aware of are that putting { or } in the DB string will need special care (since they are special characters when you pass it into String.Format. Also you will of course need to make sure that url is appropriately escaped if necessary (but that is the case with all solutions).
Try this
string Myurl="index.aspx";
label1.Text = "You also have to click on is link
you need to store your string in database with some formate like this...
"You also have to click on is <a href='{0}' target='_blank'> link </a>"
and when you assign that text to your lable at that time use string.formate method to add URL to href like this...
//get your database string
string _samplestring ="You also have to click on is <a href='{0}' target='_blank'> link </a>";
string _url ="http://stackoverflow.com/";
lbl.Text = string.Format(_samplestring, _url);
if you also need to assign something to target at run time then store your string like this..
"You also have to click on is <a href='{0}' target='{1}'> link </a>"
and use it like this...
//get your database string
string _samplestring ="You also have to click on is <a href='{0}' target='{1}'> link </a>";
string _url ="http://stackoverflow.com/";
string _target = "_blank";
lbl.Text = string.Format(_samplestring, _url,_target);
Use the following code for assigning string to href in anchor tag which is created in code behind
string strstring = "../master/YourPage.aspx?TransID="+ dr["TransId"];
Assign this string to url
marqueeText += "<a href='"+strstring+"'" + <span style='color:red;font-weight:bold;font-size:16px'>"
+ Convert.ToString(dr["SocietyName"]) + "</span></a>";
Hope this will help you.
simplest workaround is to add 'runat' attribute with 'server' value, and set Target and Href properties.
//in design
<a id="aBack" runat="server">
<img src="backarrow.png" style="cursor: pointer;" width="32px" height="32px" />
</a>
//in Code behind C#
aBack.Target = "_parent";//to target parent page from iframe
aBack.HRef = url;

Change html code on server side asp.net

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%>

How to get css styles in code behind asp.net

I need to get css styles from code behind in asp.net c#, haven't found solution on the web, is it possible to get it directly from styles.css file or do I need to workaround?
I'm using themes in my web app, but I also need to do some server processing and I need colours from ccs files, which are different for each user of course:s
You should be able to retrieve the current styles with the following approach:
var targetElement = document.getElementById("myFancyElement");
var currentStyles = window.getComputedStyle(targetElement, null);
var color = currentStyles["color"];
document.getElementById("myCurrentColor").value = color;
The next step would be to post that color value back to the server. Which could either get posted as json with an XHR request, or simply set as a form value via a hidden input element, like so:
<input type="hidden" runat="server" name="myCurrentColor" id="myCurrentColor" />
Cs file
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.CssClass = "RedBackground";
Panel1.Style.Add("font-size", "200%");
/// get value back
string pvalue = Panel1.Attributes["class"] ;
or
btn_4.Attributes.CssStyle["property"]
}
Html File
<style type="text/css">
.RedBackground
{
background-color: Red;
}
<asp:Panel ID="Panel1" runat="server">
Hello
</asp:Panel>

Categories