How to get public property in ImageURL? - c#

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;

Related

To use hidden field value in another aspx file

In the below code i have a hidden value in sample.ascx and i want to use that value in sample.aspx in its codebehind.pls help me to do this.
Sample.ascx
txthidd.Value = "Hai";
<asp:HiddenField ID="txthidd" runat="server" />
You can create a Public property in your ascx like this
public string txt
{
get
{
return this.txthidd.Value;
}
}
and can access this in aspx like this
string textOnAspx = UC_UserControl.txt;
In the codebehind, you should create a property getting the field:
public string TxtHidText{
get
{
return txthidd.Value;
}
}
Then, you'll reference it per the id, let's say you'll have something like this in ASPX:
<u1:Sample id="SomeSampleContentOfThePage" />
and in codebehind, it will be accessible via
var text = SomeSampleContentOfThePage.TxtHidText;
Note that if you want to set it from the other aspx, you should create a set part as well.

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

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

Pass a value from aspx to ascx

I have a web page and it contains an user control inside of it. I have a property on the aspx page which gets set in the pageinit method and I need to that propery on ascx page.
How can I get it?
Create a public property inside the ascx and set it at the same time you set in the aspx page.
Just to let you know, PreInit is a EventHandler not a method.
MyAdminPage myPageInstance = this.Parent as MyAdminPage;
if(myPageInstance != null)
{
...
}
There has been a few questions on this.
Reference .aspx property from .ascx
The easiest options are as follows:
Use a public variable and access it from the parent page.
Assign the variable to a hidden field on the ascx front end. A field like this: <asp:HiddenField ID="ascxField" runat="server" /> .
The example below is for #1, but #2 is almost the same.
Example #1:
Aspx page:
Front end:
<%# Register TagPrefix="Admin" TagName="MyUserControl" Src="~/UserControls/.../MyUserControl" %>
...
<Admin:MyUserControl ID="MyUserControl" AutoPostBack="true" runat="server" Visible ="false" />
Code Behind:
this.MyUserControl.Variable1 = 1;
this.MyUserControl.Variable2= "value";
Ascx page:
Code Behind
public int Variable1 { get; set; }
public string Variable2 { get; set; }

Adding an ASP.NET Web User Control to a Control Dynamically

I have a simple ASP.NET Web User Control. It looks like this:
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="NewsArticle.ascx.cs"
Inherits="Website.Controls.NewsArticle" %>
<div>
<asp:Literal ID="ltlBody" runat="server" />
</div>
My code behind looks like this:
namespace Website.Controls
{
public partial class NewsArticle : System.Web.UI.UserControl
{
public String bodyText
{
//get { return ltlBody.Text; }
set { ltlBody.Text = value; }
}
}
}
On a .aspx page I have <asp:Panel ID="pNews" runat="server" />
In the code behind I have:
foreach (vwNews news in newsQuery)
{
NewsArticle article = new NewsArticle();
article.bodyText = news.Body;
pNews.Controls.Add(article);
}
Every time I run this code the newsQuery is populated correctly and I get to the line
aticle.bodyText = news.Body; and then I received the error article.bodyText threw an exception of type 'System.NullReferenceException'
I am not sure what is causing this error message or how to fix it. I would think that there should not be an issue. I tried creating a constructor for my Web User Control so that it would give default values to my properties but that didn't work. Any idea how to make this work? It doesn't seem like it should be that
To load a control programatically you need to use the Page.LoadControl() method. See this MSDN article
You have a typo within the code you've written. 'aticle' instead of 'article'.

DataRepeater without subcontrols

What is the right way to do this in a datarepeater control?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<strong><%= Eval("FullName") %></strong><br />
<p>
<%= Eval("Summary") %>
</p>
</ItemTemplate>
</asp:Repeater>
Getting error Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I'd like to just write out the FullName and Summary. But I don't want to nest subcontrols.
Is Repsonse.Write the best way?
UPDATE:
Not sure if this is necessary, but the only way I was able to solve it was with controls
The repeater requires a datasource, assigned like so:
public class Foo
{
public string FullName { get;set; }
public string Summary {get;set; }
public Foo(fullName,summary)
{
FullName=fullName;
Summary=summary;
}
}
/// elsewhere...
List<Foo> myFoos = new List<Foo>();
myFoos.Add(new Foo("Alice","Some chick"));
myFoos.Add(new Foo("Bob","Some guy"));
myFoos.Add(new Foo("Charlie","Indeterminate"));
Repeater1.DataSource = myFoos;
Repeater1.DataBind();
As this example shows, your datasource can be anything that implements IEnumerable - lists are my favorites, but most collections in C# fall into this category. Your datasource does not have to come from a database or anywhere particular.
You don't have to use response.write, or subcontrols. (server controls aren't valid inside of a repeater, anyway). You might try replacing
<%=Eval("...
with
<%#Eval("...
I'm unsure of the difference, but the second form is used in most examples.
You can always try the follow:
<%# DataBinder.Eval(Container.DataItem, "FullName") %>

Categories