Get value from text box in User Control from Web Page - asp.net

I have this Web Control call uc_Register.asxc
Inside this web control got a Text Box i.e txtName
I add this Web Control into my web page call register.aspx
<%# Register Src="~/controls/uc_Register.ascx" TagPrefix="ecommmbs" TagName="uc_Register" %>
<hr />
<ecommmbs:uc_SummaryCart runat="server" ID="uc_SummaryCart" />
<hr />
i want to get the value from txtName.txt from uc_Register.asxc at register.aspx.
how to make this happen?

Try this in Register.aspx
TextBox txtbox = (TextBox)uc_Register.FindControl("txtName");
but keep in mind Page_Load() of aspx page is called first than Page_Load()of .ascx is called.

Here is an example:
Declare On User Control (PrevTransList2.ascx.cs)
public string TransHxPage
{
get
{
return name;
}
set
{
name = value;
}
}
On Class file
public interface IUserControlTransHx
{
string TransHxPage { get; set; }
}
txtSomthing.Text = TransHxPage;
Now On Web page SET its values
PrevTransList2.TransHxPage = "POSP";

Related

ASP.NET/C# - Getting value of HiddenField control from Master Page

I have the following HiddenField controls on my client pages:
<asp:HiddenField ID="hidRecordEditMode" runat="server" />
<asp:HiddenField ID="hidRecordEditId" runat="server" />
I am trying to access their value from a method located on my master page, using this code (sample):
protected string GetValue()
{
Page page = (Page)HttpContext.Current.Handler;
Control ctrlEditId;
ctrlEditId = (HiddenField)page.FindControl("hidRecordEditId");
return ctrlEditId.Value;
}
I'm being told the Value property doesn't exist. I've tried with and without casting (HiddenField), and setting the method static, to no avail.
How can I get this to work?
protected string GetValue()
{
var hfEditId = (HiddenField)ContentPlaceHolder1.FindControl("hidRecordEditId");
return hfEditId != null ? hfEditId.Value : string.Empty;
}
Where ContentPlaceHolder1 is the ID of the ContentPlaceHolder displaying your content page.

Set the Enabled Property of a UserControl From CodeBehind

It's been a while since I worked on WebForms so I need a refresher when working on an old site.
I have a userControl on the page that I need to programatically set the enabled state
<%# Register Src="CalandarControl.ascx" TagName="CalandarControl" TagPrefix="uc" %>
I have this at the C# code but Enabled is not available here. What am I missing?
if (c is UserControl)
{
var x = c.GetType();
if (x.Name == "calendarcontrol_ascx")
{
((UserControl)c).Enabled = true;
}
}
Thanks
You should have something on the code-front that places the control on the page, like:
<uc:CalendarControl ID="dtePrepaymentExpiresDate" FieldName="Prepayment expires date" runat="server" Enabled="false" />
Then in the code behind, you can set this custom property as follows:
dtePrepaymentExpiresDate.Enabled = true;
If you really need to do it in the loop, then you need to cast c as the CalendarControl and not UserControl because CalendarControl has the property Enabled while a normal UserControl does not.
((CalandarControl)c).Enabled = true;
you can define a panel in usercontrol witch cotaines all of controls of the user control, then define a property Enabled named as bool (panelMain.Enabled;) and from ur page set it,
in user control ascx
<asp:panel runat="server" id="panelMain" Enabled="false">
<!-- define ur other controls between panel-->
</asp:panel>
in usercontrol ascx.cs
public bool Enabled
{
get { return panelMain.Enabled; }
set { panelMain.Enabled = value; }
}
in page first register ur usercontrol and then set Enabled property from code behind ....
for example
protected void Page_load(object sender,EventArgs e )
{
panelMain.Enabled = true;
}
as simple as drink water :))

How to access id of <asp:Hidden> control from ascx page to cs page

This is my ascx Code:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Demo.ascx.cs"
Inherits="Demo" %>
<asp:HiddenField ID="hidden" runat="server" Value="" />
And the aspx:
<%# Register TagName="Hidden" TagPrefix="CRS" Src="~/Demo.ascx" %>
<div>
<CRS:Hidden ID="hid" runat="server" />
</div>
Now How to access Hidden variable ID From ascx page to this cs page backend
Do you mean the actual ID? or the Value within the hidden field?
You can access the value using the FindControl method
HiddenField hf = (HiddenField)this.hid.FindControl("hidden");
string theValue = hf.Value;
Not sure if this is exactly what you are looking for.
Alternatively, you can declare some public properties in the UserControl in which you can access directly
In the ascx code:
public string theValue { get; set; }
In the aspx code:
string theValue = this.hid.theValue;
To access the HiddenField inside the UserControl from the asp.net web page you will need to wire up something called a Public Property.
This code should be added to the UserControl ascx.cs code behind:
public string Value
{
get { return hidden.Value; }
set { hidden.Value = value; }
}
You could then write code like this in your asp.net page:
string SomeHiddenValue = hid.Value;
hid.Value = "Its a secret!";
Note: I haven't compiled this so I am not sure if the public property name of Value will compile. I am also not sure if the second value in set { hidden.Value = value; } needs capitalising. Try changing these two values if you encounter problems.

Access property of parent page in user control

I have a hidden field on my default.aspx page. Within default.aspx page I have a user control which has a label on it. I need the label to display the value on the hidden field. How can I go about this?
Create a public property on the user control which exposes the label text:
public string LabelText
{
get { return this.label1.Text; }
set { this.label1.Text = value; }
}
Then set this from the page code-behind. So if the ASPX for your page has something like this in it:
<uc1:UserControl ID="userControl" runat="server" />
In the code-behind, you could do
this.userControl.LabelText = "something";
Doing it this way means that your user control doesn't have to know about the page that's using it. This helps the user control to be re-used on many different pages.

Find control in usercontrol from a Page ASP.NET

I am loading a control to a page dynamically with LoadControl("src to file").
In the usercontrol i have a validator and some other controls that i would like to access from my page. I canät get it to work, null pointer exception.
Scenario is like this. I have a Edit.aspx page which loads the EditTemplate.ascx usercontroll. I would like to get information or find the controls in the EditTemplate from the Edit.aspx site.
I have tried exposing the controls and validators as properties but how do i access them from my Edit.aspx?
Example code:
Edit.aspx, the control is later added into a
Control control = LoadControl("src to ascx");
TemplatePlaceHolder.Controls.Add(control);
EditTemplate.ascx
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyImageFile" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
CodeBehind
public partial class EditTemplate : System.Web.UI.UserControl, IEditTemplate {
public RequiredFieldValidator Validator {
get { return this.RequiredFieldValidator1; }
set { this.RequiredFieldValidator1 = value; }
}
From the Edit.aspx site i would like to check the validators isValid property. Isvalid is set in a Save method.
The save button that saves the template is located in edit.aspx, so the post in done from that page.
So the question is how to get a hold of the property from the usercontrol in the edit.aspx page, where and how should this be done?
Thanks again.
Easiest way is to have the user control define properties like:
public IValidator SomeValidator {
get { return this.cuvValidator; }
set { this.cuvValidator = value; }
}
public string Text {
get { return this.txtText.Text; }
set { this.txtText.Text = value; }
}
Which your edit page can use.
HTH.
You can always use recursive approach. Check the solution on Steve Smith's blog:
Recursive-FindControl.
As mentioned in previous answers, I would expose any validators you must access from the parent ASPX page as properties in the user control.
public RequiredFieldValidator ValidatorToCheck
{
get { return this.rfvMyField; }
}
Then, you can dynamically add your user control to some placeholder (being sure to assign an ID to the user control).
// In my example, this is occurring in the Page_Load event
Control control = LoadControl("~/Controls/EditTemplate.ascx");
control.ID = "ucEditTemplate";
pnlControlHolder.Controls.Add(control); // the placeholder in my example is a panel
When you want to access the IsValid property on the given validator (presumably in your save action) you can do so as follows (being sure to cast the control to the appropriate type and using the ID you originally assigned to the user control):
EditTemplate control = (EditTemplate)pnlControlHolder.FindControl("ucEditTemplate");
if (control.ValidatorToCheck.IsValid)
{
// Some action
}

Resources