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.
Related
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;
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";
I'm trying to access a hiddenfield value from my masterpage that is set in my child aspx page, but cannot access it the masterpage codebehind page_load event.
Child aspx page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server">
</telerik:RadStyleSheetManager>
<div class="center_content">
<div style="text-align: left">
<h2>
</h2>
</div>
<div style="text-align: left">
<uc1:Chart ID="Chart1" runat="server" />
</div>
</div>
<asp:HiddenField ID="hid1" runat="server" Value="Satellite Availability % Report" />
Master page:
<asp:Label runat="server" ID="Label1" Style="text-align: right; font-size: xx-large; color: #808080"></asp:Label>
Master page code behind: This is where I want to set the text value of the report from the child page.
protected void Page_Load(object sender, EventArgs e)
{
HiddenField hid1 = (HiddenField)MainContent.FindControl("MainContent_hid1");
if (hid1 != null)
{
Label1.Text = hid1.Value;
} }
<input type="hidden" name="ctl00$MainContent$hdnRptTitle" id="MainContent_hdnRptTitle" value="Satellite Availability % Report" />
There is no intellisense for the hdnRptTitle variable.
How can I get this to work? It seems simple enough, but don't know why it not working...
You can add the below code in your MasterPage:
HiddenField hid1 = (HiddenField)MainContent.FindControl("hid1");
if (hid1 != null)
{
Label1.Text = hid1.Value;
}
EDIT: Make sure your Label on the MasterPage is outside your ContentPlaceHolder, as I made this mistake when I first tested.
The above code should work as provided, with your control names, I'm not sure why you are using:
.FindControl("MainContent_hid1");
instead of
.FindControl("hid1");
You can use like this.
There can be multiple conterntPlaceHolder on your master page.
use the id which contains your hidden field in this case I assume that it is ContentPlaceHolder1
HiddenField hid1 = (HiddenField)ContentPlaceHolder1.FindControl("hdnRptTitle");
if (hid1 != null)
{
Label1.Text = hid1.Value;
}
There is a similar post on so
How to access content page controls from master page in asp.net
You can reference a master page and get the control like this:
VB.Net:
Dim te As HiddenField
Dim val As String
te = Me.Page.Master.FindControl("hdnRptTitle")
val = te.Value
c#:
HiddenField te = default(HiddenField);
string val = null;
te = this.Page.Master.FindControl("hdnRptTitle");
val = te.Value;
Why do you think that you can access a control in a content-page of a master-page? A MasterPage is used for multiple pages, why do you want to hardlink it with a specific page, it won't work anymore without it. If the HiddenField is essential for the operation of the master you should declare it there.
For every child page, there is a different rpt title which needs to
show up on the master page. How can I accomplish this?
Then the content page can access it's master to set the text but not vice-versa.
You could provide a public property in the master, e.g.:
public string ReportTitle
{
get { return this.LblReportTitle.Text; }
set { this.LblReportTitle.Text = value; }
}
and in the ContentPage, for example in it's Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
// assuming the type of your master is SiteMaster
var master = this.Master as SiteMaster;
if (master != null)
master.ReportTitle = hdnRptTitle.Value;
}
This approach is still linking the master with one (or multiple) of it's childs, but it would still "work" if the content-page would use a different master-type. You'd also be informed with a compiler error if somebody remove or change the property.
However, where the content stores the report-type or where the master displays it is an implementation detail and can be changed in future without breaking anything.
I have a dynamic data web site into which I am attempting to add a text box with an AutoCompleteExtender. I have declared the control like so
<asp:TextBox ID="tbTerm" runat="server" Width="300px"/>
<asp:AutoCompleteExtender runat="server"
id="autoCompleteExtenderTerms"
TargetControlID="tbTerm"
ServiceMethod="GetCompletionList"
UseContextKey="True">
</asp:AutoCompleteExtender>
And in the codebehind on that page I have declared the web method like so
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static List<string> GetCompletionList(string prefixText, int count)
{
using (ProductDataEntities context = new ProductDataEntities())
{
var terms = (from t in context.Terms
where t.Name.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase)
select t.Name).ToList();
return terms;
}
}
Currently this method is not getting called, this is not a forgien key column so I cant use the standard filter for this.
I have ensured that EnablePageMethods="true" is set on the ScriptManager and I am out of ideas as to why this method is not being fired from the page.The contol is not wrapped within an update panel nothing else stands out to me on this.
Set up ServicePath property value.
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.