I have a master page with a button and i have a client page with master page reference. I would like to change the child page label values while i click the master page button. I can't change that.
Label StrHref= (Label)MainContent.FindControl("lblhead");
HiddenField StrCalId = (HiddenField)MainContent.FindControl("hf_callid");
StrHref.Text = "12345678901";
StrCalId.Value = "1395741766.47";
Try the below code on your masterPage button click event.
I have kept one button on masterpage with name as btnMaster and on child page I have put on text box and on click of MasterPage btnMaster I have used the below method.
protected void btnMaster_Click(object sender, EventArgs e)
{
var textbox = this.ContentPlaceHolder1.FindControl("txtChild") as TextBox;
textbox.Text = "Text from Master to child control";
}
Let me know if you have any issue.
Related
I have a string (lang) in a user control (ascx) inside a Content Page. I want to access (lang) value from the Master Page. How can I do that?
User Control (lang.ascx.cs) - code behind.
lang = (string)(Reader["lang"]); //lang is retrieved from a database
User Control (lang.ascx).
<input runat="server" type="hidden" ID="langInput" value="<%: lang %>"/>
Content Page (lang.aspx)
<uc:lang runat="server" />
Now how can I access (lang) value from the Master Page?
Thank you.
To find that Control you have to do a lot of FindControls
//first find the ContentPlaceHolder on the Master page
ContentPlaceHolder contentPlaceHolder = this.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
//then the PlaceHolder on the aspx page that contains the UserControl
PlaceHolder placeHolder = contentPlaceHolder.FindControl("PlaceHolder1") as PlaceHolder;
//then the UserControl
UserControl userControl = contentPlaceHolder.FindControl("UserControl1") as UserControl;
//and finally the Control we want to manipulate
HiddenField hiddenField = userControl.FindControl("HiddenField1") as HiddenField;
hiddenField.Value = lang;
//or you can you do the same in a single line of code
HiddenField hiddenField = this.FindControl("ContentPlaceHolder1").FindControl("PlaceHolder1").FindControl("UserControl1").FindControl("HiddenField1") as HiddenField;
If you dynamically add UserControls to the page, do not forget to
assign an ID, otherwise FindControl will not work.
myControl = (MyControl)LoadControl("~/MyControl.ascx");
//do not forget to assign an ID
myControl.ID = "UserControl1";
PlaceHolder1.Controls.Add(myControl);
I have a requirement where I need to pass the value of page title to Facebook share plugin only after the page title has been set by the content page. There are a few pages(Dynamic Data pages) where the page's title is set upon a specific control's PreRender event (I cannot change this).
If I pass the content page title on Master page's PreRenderit returns the unset value as Master page's PreRender is fired before content page's control PreRender. I tried passing the value in the master page's Unload event but it did not work! How do I achieve this?
So the question can be summarised as, perform something on master page after the content pages have performed all of their tasks and are ready to unload.
Is there a way to do this in master page itself or will I have to do this in the individual pages?
Reference: Events in ASP.NET Master and Content Pages
Dynamic Data Page:
string Location = null;
protected void DynamicFilter_PreRender(object sender, EventArgs e)
{
DynamicFilter Filter = (DynamicFilter)sender;
MetaColumn metaColumn = table.GetColumn(Filter.DataField);
QueryableFilterUserControl fuc1 = Filter.FilterTemplate as ForeignKeyFilter;
if (fuc1 != null && fuc1.FilterControl != null)
{
DropDownList ddl = fuc1.FindControl("DropDownList1") as DropDownList;
if (ddl != null)
{
if (metaColumn.DisplayName == "Location")
{
if (ddl.SelectedIndex != 0)
{
Location = ddl.SelectedItem.Text;
}
}
}
}
if (Location != null)
{
// set the page title based on the location
Page.Title = String.Format("Recent {0} Fares", Location);
}
}
This function sets the content page's(a dynamic data page) title.
Now I need to access this title on the my master page only after it has been set.
Master Page:
protected void Page_Load(object sender, EventArgs e)
{
// pass the value of page title to SocialNetworkingHelper class to do some work
SocialNetworkingHelper.SetSocialMediaMetaTag(this.Page, this.Page.Title);
}
use page unload event to perform final cleanup like closing files, releasing memory etc...if you set page title here it will not be rendered....you have to set title on control prerender event itself...
Page 1 - Ticket.aspx, DropDownList1, ModalPopUpextender with id mpe
Page 2 - Customer.aspx, btnSave
The index change event of dropdown will pop up mpe which has an iframe. This iframe loads Customer.aspx.
I am trying to access page1 controls in the button click event, but unable to.
Customer.aspx.cs:
protected void btnSave_Click()
{
Ticket page = new Ticket();
ModalPopUpExtender mpe = (ModalPopUpExtender)page.FindControl("mpe");
DropDownList ddl = (DropDownList)page.FindControl("DropDownList1");
//error here - Object reference not set to an instance
mpe.hide();
ddl.selectedindex=0;
}
Why is this not working. Using a Session variable should work right?
You may use Server.Transefer instead of Response.Redirect and then you can find the control in the current page.
Like:
TextBox tb = (TextBox)PreviousPage.FindControl("textbox1");
EDIT:
if (Page.PreviousPage != null)
{
DropDownList ddl1 =
(DropDownList)Page.PreviousPage.FindControl("DropDownList1");
if (ddl1 != null)
{
Label1.Text = ddl1.SelectedItem.Text; //your logic
}
}
What you are trying may not be doable from the server side, but it can be easily be done with a little javascript. Here is a link where you can get a working piece of code.
Hope this helps.
I'm using the asp.net Login control:
<asp:Login ID="Login2" runat="server" LoginButtonType="Link"
Width="280px" Height="150">
</asp:Login>
How could I set the Username textbox to have the focus when initially the page loads and also the Login link button should be clicked when the user enters.
I tried the below in the Page_Load event but didn't work?
this.Login2.FindControl("Username").Focus();
this.form1.DefaultButton = this.Login2.FindControl("LoginButton").UniqueID;
Thanks,
Try something like this:
TextBox tb = (TextBox)Login1.FindControl("UserName");
tb.Focus();
if (!HttpContext.Current.User.Identity.IsAuthenticated) {
Login lg = (WebControls.Login)LoginView1.FindControl("Login2");
TextBox tb = (TextBox)lg.FindControl("UserName");
tb.Focus();
}
Have a look here to see how you can define the login-button as default button in Login:
Submit Login control button when I hit Enter
protected void Page_Load(object sender, System.EventArgs e)
{
System.Web.UI.WebControls.Login login = LoginArea.FindControl("LoginForm");
TextBox txt = login.FindControl("UserName");
Page.SetFocus(txt);
}
You can simply write
Login1.Focus() at page load event and also basically, you can define a DefaultButton not just on the Form level, but also on individual Panel level, as long as the focus is within the panel, the default button for the panel will be used if you hit "Enter
I have a Label in my mater page which i want to access in a page which uses the same mater page.
I tried..
string text = ((Label)Master.FindControl("myLabel")).Text; //Always returns empty string
P.S i have included <%# MasterType virtualpath="~/Masters/Master1.master" %>
still not working
As Waqas Raja mentioned in comments, the problem is in event sequence: master's Load event occurs after page's Load event. So you could just use Page.LoadComplete event in your page:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = ((Label)Master.FindControl("myLabel")).Text;
}
and it should give you desired value of the textbox.
i thing you label in master page reside in content place holder. so
ContentPlaceHolder mpContentPlaceHolder =
(ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
string TextBoxvalue =
((TextBox) mpContentPlaceHolder.FindControl("TextBox1")).Text;