User Control In Asp.net - asp.net

I created a user control. It contains one textbox. can I control this from parent web page.

Add the following property to your user control:
public string SomeValue
{
get
{
return txtSample.Text;
}
set
{
txtSample.Text = value;
}
}
And if you want to get or set the user control's textbox value from the page that contains the user control, just do the following:
MyUserControl.SomeValue = "Hello from page";
lblTest.Text = MyUserControl.SomeValue;
Where "MyUserControl" is the ID of the user control in the containing page.
Note: Since the TextBox control handles its Text property in the ViewState on its own, you don't have to explicitly handle it for this property.

Just define some public method or property in the user control and you can access it successfully. For example:
In UserControl1.ascx.cs:
public void DoSomething()
{
//Do something here from UserControl
}
In the parent:
MyInstanceOfUserControl1.DoSomething();

Related

Access TextBox value of an ASP.NET page from C# class

Say there is a TextBox on an ASP.NET page
<asp:TextBox id="DateTextBox" runat="server" />
with some value set in the code-behind.
How to access that value from another class of C# code-file through HttpContext or any other way?
You can access a property in you page via HttpContext even from a static method.
in your page:
public string DateTextBoxText
{
get{ return this.DateTextBox.Text; }
set{ this.DateTextBox.Text = value; }
}
somewhere else(even in a different dll):
public class Data
{
public static string GetData()
{
TypeOfYourPage page = HttpContext.Current.Handler as TypeOfYourPage;
if (page != null)
{
return page.DateTextBoxText;
//btw, what a strange method!
}
return null;
}
}
Note that this works only if it's called from within a lifecycle of this page.
It's normally better to use ViewState or Session to maintain variables across postback. Or just use the property above directly when you have a reference to this page.
You can create a public property within the control that returns a reference to the textbox.
You can then use this property to reference the textbox.
OR
You can store in into session and then access it in your entire application.
Store it in the HttpContext Session http://www.codeproject.com/Articles/32545/Exploring-Session-in-ASP-Net
//Storing UserName in Session
Session["DateTextBox"] = DateTextBox.Text;
Now, let's see how we can retrieve values from session:
//Check weather session variable null or not
if (Session["DateTextBox"] != null)
{
// use it...
}
You could place the value in the Session during the post back. Then access it from the Session in the other class. So in your form load event write this:
Session["MyValue"] = DateTextBox.Text
and then in the other class write this:
var val = HttpContext.Current.Session["MyValue"];

Using Control Adapter for User Controls in ASP.NET

We can change the output rendering of an ASP.NET control with Control Adapters. Now I want to know that, is it possible to change a user control rendering by control adapters in ASP.NET? By the way I have several user controls in my page and I don’t want to do same actions for them.
Yes you can but you must define your controls childs as public in your User control
(Associate public on child controls in order to access from your adapter)
public class UserControlSample : userControl
{
public TextBox TextBoxWrapper
{
get
{
return MyTextBox;
}
set
{
MyTextBox = value;
}
}
}
//Access from adapter
UserControlSample.TextBoxWrapper.Text
UserControlSample.TextBoxWrapper.Id
.....

Web user control's private value is empty after UpdatePanel updates

I have a web user control which has an update panel & a gridview is inside it
On page load I set user control's private field value through a publicly exposed property & bind the gridview with data
I enter some new values in through a modal & do a postback inside the updatepanel in user control, when I try to fetch the value of private field on my user control the value is default to zero
private int ftId = 0;
public int FtId
{
set { ftId = value; }
}
the private int ftId=0; is called after every ASync postback.
Is there any way I can overcome this problem ?
You cannot store a value in a variable and have it persist across postbacks. But storing it in viewstate will work:
public int FtId
{
get { return (int)(ViewState["FtId"] ?? 0); }
set { ViewState["FtId"] = value; }
}
HTH.

User control event or method override where custom properties are valid?

I have an ASP.NET user control that is used in another use control. The parent user control uses data-binding to bind to a custom property of the child user control.
What method can I override or page event where I am ensured that the property state is set?
I think in a page it is PageLoaded versus the Page_Load override? I am looking for this in the user control because my property is always null even though it is set.
Thanks.
Example. This is in my user control. FilterEntryId is being bound from inside another user control:
protected int _filterEntry = -1;
public int FilterEntryId
{
get
{
return _filterEntry;
}
set
{
_filterEntry = value;
}
}
protected void Page_Load(dobject sender, EventArgs e)
{
FilterEntry always -1!!
}
The property is being set but never has value when Page_Load. The Page_LoadComplete may be the proper place but does not seem to be an option in user control. I've also tried Page_DataBind.
My hypothesis is that this is a page lifecycle issue but it may be something else.
Not sure what you need to do with that property but since you can't be sure when will be set. Can't you add your logic on the set of the property?
Another option would be a later event as PreRender.
public int FilterEntryId
{
get
{
return _filterEntry;
}
set
{
_filterEntry = value;
//HERE YOUR LOGIC
}
}

how to get instance of .net control which is in child page from user control in master page

i have user control called shopping cart.
which i have used in master page.
i want to get the textbox value from the child page in to user control.
is it possible to access control from child page in "Custom User Control" which is on master page?
I'm not entirely sure what your trying to accomplish but it sounds like to want to access a property of a user control contained in a master page from a content page.
You can use a public property in your master page which exposes the text property of the user control.
public string ShoppingCartText {
get { return ((TextBox)this.ShoppingCart.FindControl("TextBox1")).Text; }
set { ((TextBox)this.ShoppingCart.FindControl("TextBox1")).Text = value; }
}
Then from your content page you can set the value of the text box. You can access the properties of a master page from a content page through the Page.Master property.
Master.ShoppingCartText = "value"
You can recurse through the control tree to find any control in a page.
Here are a couple of extension methods, pop this code in a class file in your solution.
public static class ControlExtensions
{
public static IEnumerable<Control> FindAllControls(this Control control)
{
yield return control;
foreach (Control child in control.Controls)
foreach (Control all in child.FindAllControls())
yield return all;
}
public static Control FindControlRecursive(this Control control, string id)
{
var controls = from c in control.FindAllControls()
where c.ID == id
select c;
if (controls.Count() == 1)
return controls.First();
return null;
}
}
Then use like this in your user control.
TextBox whatYoureLookingFor = this.FindControlRecursive("theId") as TextBox;
if(null != whatYoureLookingFor)
// whatever
What I did was access the master page control through a public function in the code behind.
So in the code behind for the master page, I would declare something like:
public string getTextBoxValue()
{
return TextBox.Text;
}

Resources