Pass a value from aspx to ascx - c#

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

Related

How to set the value of a usercontrol's property in ASP.NET?

There are 3 dropdownlists that are being used at several places. So I decided to put them in a userControl.
<%# Control Language="VB" AutoEventWireup="false" CodeFile="DetailsDropDownList.ascx.vb"
Inherits="UserControls_DetailsDropDownList" %>
<asp:DropDownList ID="dedMoth" runat="server"></asp:DropDownList>
<asp:DropDownList ID="dedDay" runat="server"></asp:DropDownList>
<asp:DropDownList ID="dedYear" runat="server"></asp:DropDownList>
In the Code behind, I've defined a property so that I capture the value to display.
public DateTime DetailsDate { get; set; }
So I'm setting the property this way: myUserControl.DetailsDate = myObject.myDate
The problem is that no value is being set. When I set the breakpoint, I notice that UserControl's life cycle starts after that of the main page.
I've tried this as well but no luck.
<uc1:DetailsDropDownList runat="server" ID="myUserControl"
DetailsDate ="<%= myObject.myDate%>" />
So How do I set the value of a UserControl?
You need to persist your property. If you don't it's reset each and every single time the page loads, just like any other local variable. You can easily persist into viewstate though like so:
public DateTime DetailsDate
{
get{
if(ViewState["DetailsDate"] == null)
{
return DateTime.Today;
}
else
{
return (DateTime)ViewState["DetailsDate"];
}
}
set {ViewState["DetailsDate"] = value;}
}
Add Page.DataBind(); in Page_Load() event. This will bind page data. Once debugger pass through page load check expected date is coming or not.

Is there an ASP.NET databindable control (like Repeater), which only expects a single item?

I'm trying display lots of properties from a business object on a Web Form. I could of course, create loads of labels and assign all the values in code-behind, but I feel there must be a less verbose way.
What I want is something like an asp:Panel but where you can specify a datasource. It doesn't seem like Panels support any kind of databinding.
What I'd like is something like this
// C#
panel.DataSource = theCompany;
panel.DataBind();
Then:
// ASPX
<asp:Panel runat="server">
Name: <%# Eval("Name") %>
Phone: <%# Eval("Phone") %>
...
</asp:Panel>
..but I can't find anything which will allow me to work in this way.
I thought I might be able to use asp:FormView but this just gives the error "Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource."
A caveat is that I do not want to call a global DataBind() (this has caused us no-end of problems in the past) - I would like the databind to be constrained to a particular part of the page.
It seems you can do this using a Panel, but you have to assign your business object to a page property first, as there's no way to set the business object as a "DataSource" for the panel (as you would for a Repeater control, for instance).
Once the object is assigned as a page property, you can then use the following syntax in the .aspx to access the properties of that object, without needing to manually assign each item to control values in code behind:
<%# Company.Name %>
You don't need to databind (although you can). What you need is a simple expression evaluator. Add a property to your code behind like this
public string Test { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Test = "<script>alert('test');</script>";
}
Then use it to render code directly to the page like this
The value: <%: Test %>
Note that the <%: syntax escapes the input. If you wish to NOT escape the input then you can use <%= syntax. Note that you don't need to have a string object you can access any properties you like for example
The value lenght: <%: Test.Length %>
Use The below:
<asp:DetailsView runat="server">
Name: <%# Eval("Name") %>
Phone: <%# Eval("Phone") %>
</asp:DetailsView>
Use DetailsView. You can add it from the Toolbox. It's for Single Row Data Presentation.
<asp:DetailsView runat="server">
Name: <%# Eval("Name") %>
Phone: <%# Eval("Phone") %>
</asp:DetailsView>
Why not using DetailsView. Its perfect for what you want. Showing single row of data only and that too in two column form.
I suggest using a standard Repeater, databound with an array containing a single item.
Repeater.DataSource = new [] { theCompany };
Repeater.DataBind();
Advantage over databinding to a Panel: you can still use the ItemType attribute, and have access to the nice strongly typed Item object and don't have to go about using Eval, i.e.:
<asp:Repeater runat="server" Id="Repeater" ItemType="CompanyViewModel">
<ItemTemplate>
Name: <%# Item.Name %>
Phone: <%# Item.Phone %>
</ItemTemplate>
</asp:Repeater>
(Replace "CompanyViewModel" with the Type of your: "theCompany".)
You can also try experimenting with DetailsView, but it's not as malleable as a Repeater.
Create your own user control that shows the properties of the objects. You can use reflection to read property names and values and display in control.
Create a property for your object in your user control. Inside user control code behind write function Show() with below code.
//Build html strin from all propeties
PropertyInfo[] properties = yourObject.GetType().GetProperties();
string lbl = "<label>{0}</label>";
string value= "<span>{0}</span>";
string tab ="\t";
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo pi in properties)
{
var label = string.Format(lbl,pi.Name);
var val = string.Format(value, pi.GetValue(yourObject, null))
sb.Append(label+tab+val);
sb.Append("<br/>")
}
Response.Write(sb.ToString());
Now in your pager add that control and sets its object property in code behind like
myControl.MyObject = yourObject;
myControl.Show();
NickG's answer will work... however consider two scenarios.
If your business object is ever null, the page will crash with an "Object Reference" error. This can be avoided with a cumbersome looking
<% if(MyObject != null) { %><%= MyObject.Prop %><% } %>
... but doing that every time makes for messy code.
If your page uses PostBack processing via UpdatePanel, the business object will have to be reloaded to the property every time the Page lifecycle runs... even if that portion of the page isn't being redrawn. This is because IIS will resolve all the <%= MyObject.Prop %> references regardless, causing wasted CPU cycles and probably wasted database calls if your object is coming from a database.
For these reasons I always use a Repeater control, which is lightweight, supports ViewState, can easily be assigned a one item list, and avoids the aforementioned issues. Here's an example using the HttpContext.Current.Request object as a "business object".
<asp:Repeater ID="rptTest" runat="server">
<ItemTemplate>
Request.URL = <%# Eval("Url") %>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var objTest = Request; //Using the Request object as a test business object
rptTest.DataSource = new List<System.Web.HttpRequest>() { objTest };
rptTest.DataBind();
}
}
To make this work we can customize ASP.NET Panel by inheriting it and using Custom Server control for ASP.NET
Use Below Code to modify the ASP.NET Panel in an ASP.NET Custom Server Control Project:
[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomPanel runat=server></{0}:CustomPanel>")]
public class CustomPanel : Panel
{
[Bindable(true)]
public object MyDataSource
{
get;
set;
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public StringBuilder Text
{
get;
set;
}
public void MyDataBind()
{
Text = new StringBuilder();
foreach (PropertyInfo p in MyDataSource.GetType().GetProperties())
{
Text.Append(string.Format("<b>{0}</b>", p.Name));
Text.Append(":");
if (p.GetIndexParameters() == null || p.GetIndexParameters().Length == 0)
Text.Append(p.GetValue(MyDataSource, null));
Text.Append("<br />");
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
Then add this control's reference and toolbox item to your ASP.NET page:
<cc2:CustomPanel ID="MyCustomPanel" runat="server">
</cc2:CustomPanel>
Use the control as shown below:
MyCustomPanel.MyDataSource = theCompany;
MyCustomPanel.MyDataBind();

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.

Can not access my user controls' properties

I have a user control:
<%# Control Language="C#" ClassName="WebUserControl" %>
<script runat="server">
public string name { get; set; }
</script>
Then on a page I have
<%# Register Src="~/Controls/my_control.ascx" TagName="MyControl" TagPrefix="Me"%>
<Me:MyControl runat="server" ID="myControl1" />
and I can see the property if I do
<Me:MyControl runat="server" ID="myControl1" name="Hello World"/>
However, if from my code behind, or code tags I try
myControl1.name
I get nothing. What am I doing wrong?
Please try this:
This is related to control state:
public string name
{
get
{
object obj = ViewState["name"];
return (obj == null) ? "" : obj.ToString();
}
set
{
ViewState["name"] = value;
}
}
You may find addtional information here:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.loadcontrolstate(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/1whwt1k7(v=vs.100).aspx
You're missing a using directive in your page. Add a using {your.control.namespace} statement.
It works fine. Did you drag and drop the control onto a form?

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'.

Categories