user controller passing value display result based on the function - asp.net

how to display the result on page on aspx to the user controller? the result have been done by the function. how to passing the value?
i have a user controller name as ddMenu inside have a labal lblQty, and on my cartpage.aspx have done the function and display a result on labal1
<asp:Label ID="lblQty" runat="server"></asp:Label>

EDIT : You can pass the value to the usercontrol using a property in usercontrol.
In the UserControl add a public property and set that propertyfrom Page. Here I am saving the value in ViewState so that it persists between postbacks. In usercontrol (ddMenu.ascx.cs):
public string MyLabelText
{
get
{
if(ViewState["LabelQty"] != null)
return ViewState["LabelQty"].ToString();
return string.Empty;
}
set
{
ViewState["LabelQty"] = value;
}
}
And in Page code (Cart.aspx.cs) set it like this:
myddMenu.MyLabelText = lblQty.Text;
Now you can access the value in usercontrol:
//I have a label "Label1" in usercontrol
Label1.Text = MyLabelText;

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.

how to access ascx custom control property from masterpage.master.cs

I have this following property in my custom user control:
public string selectedtab
{
get
{
if (ViewState["AdminCurrentNavID"] != null)
{
return ViewState["AdminCurrentNavID"].ToString();
}
else {
isfirstload = true;
return null;
}
}
set { ViewState["AdminCurrentNavID"] = value; }
}
I am setting the value of it on my Page_Load() in ascx control. What i need to do is that after setting the value of this property I need to access it from masterpage.cs in code behind. you can see how currently I am trying to do in below code, but the issue is that I am not able to get the value i thing it is because the masterpage's Page_Load() rendering before the ascx control so I thats why I am getting null value, please help, thanks.
masterpage.cs:
usercontrols.mainmenu adminmenu = (usercontrols.mainmenu)LoadControl("~/mymenupath.ascx");
lbmsg.Text = adminmenu.selectedtab;
When you call LoadControl in your master page, you are actually creating a new instance of your user control, not accessing the one you have somewhere in your site.
When you declare the User Control in your page you should have given it an id. You could access the property with something like ((usercontrols.mainmenu)MyUserControlId).selectedtab
I found the solution by using Delegate, you can see in the link below.
http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx

How to create a derived TextBox control?

I need to create a custom TextBox control that allows user input HTML tags. I added a new property called HtmlEnabled, default is false. If it is false, it will act exactly like the original TextBox; if it is set to true, it will call Server.HtmlEncode to encode the text. I never creat a custom control, can anyone tell me what do I need to do? What function I need to override? Thanks.
I created my TextBoxEx class as following: I still get the validation error when I set HtmlEnabled to true, can anybody tell me what is wrong?
namespace WebApplication1
{
[ToolboxData("<{0}:TextBoxEx runat=server></{0}:TextBoxEx")]
public class TextBoxEx : System.Web.UI.WebControls.TextBox
{
public bool HtmlEnabled
{
get
{
return (bool)ViewState["HtmlEnabled"];
}
set
{
ViewState["HtmlEnabled"] = value;
}
}
public TextBoxEx()
{
ViewState["HtmlEnabled"] = false;
}
public override string Text
{
get
{
if (HtmlEnabled)
return HttpUtility.HtmlEncode(base.Text);
else return base.Text;
}
set
{
if (HtmlEnabled)
base.Text = HttpUtility.HtmlDecode(value);
else base.Text = value;
}
}
}
}
Sounds like you could just inherit from the TextBox control and override the Text property. This article should get you started on how to go about doing it:
https://web.archive.org/web/20211020203142/https://www.4guysfromrolla.com/articles/100103-1.aspx
In order to allow the page to accept HTML tags, you need to disable request validation.
<%# Page Language="C#" ValidateRequest="false" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="MyNamespace.TestPage" %>
This has nothing to do with the textbox control, the request validation checks all page input (query string parameters, cookies, headers, and form fields) to ensure that there are no potentially malicious scripts in the request. Be aware that by turning it off, you will need to validate that the input isn't harmful yourself.

Access control from .aspx in .ascx

I have a custom user control that I made called OrderForm.ascx. I also have an .aspx file that utilizes the OrderForm control.
I want to access a control on the .aspx file from the OrderForm control. Is there a way to do this?
You could use the FindControl method in the user control like this:
Label label = Page.FindControl("Label1") as Label;
if (label != null)
string labelText = label.Text;
As a note on the above, depending on where the Label is in the page, you may need to use recursion to find the Label.
You could also create a property on the page that returns the text of the Label:
public string LabelText
{
get { return Label1.Text; }
}
To access the property from the user control, here are two options:
Option #1
string labelText = ((PageName)Page).LabelText;
Option #2
string labelText = Page.GetType().GetProperty("LabelText").GetValue(Page, null).ToString();
If you have two user controls, ControlA and ControlB, and they are both registered on the same page you can easily access one from the other. Simply create a public property that you want to have access to in ControlB such as:
Public ReadOnly Property ControlB_DDL() As DropDownList
Get
Return Me.ddlItems
End Get
End Property
And then you can reference that property in ControlA after finding that control:
ControlB ctrlB = (ControlB)Page.FindControl("cB");
DropDownList ddl = ctrlB.ControlB_DDL;
Refer here for more info: http://www.dotnetcurry.com/ShowArticle.aspx?ID=155
To access the controls of .ascx in .aspx.
HiddenField selectedEmailsId = performanceReportCtrl.FindControl("CONTROLID") as HiddenField;
And to access controls of aspx in ascx.
HiddenField selectedEmailsId = Page.FindControl("CONTROLID") as HiddenField;

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