in my web application i have a master page and i want to implement defaultbutton for a login page when user press enter (my application has Master page) how can i place default button.
Page.Form.DefaultButton = crtlLoginUserLogin.FindControl("LoginButton").UniqueID
or just
Page.Form.DefaultButton = LoginButton.UniqueID
This will work.
If you want to set a default button in a Master Page, and the button is in a Content Page or a User Control, you cannot set this directly in the Master Page markup.
<form id="form1" runat="server" defaultbutton="MyButton" >
Will generate the following error:
The DefaultButton of 'form1' must be the ID of a control of type IButtonControl.
The workaround for this is to set the default button during the Page_Load of your Content/User Control:
protected void Page_Load(object sender, EventArgs e)
{
Button myButton = (Button)FindControl("MyButton");
Page.Form.DefaultButton = myButton.UniqueID;
}
The VB version of this is as follows:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'cast the master page form to set the default values for the Default button and Default focus'
Dim myForm As HtmlForm = TryCast(Me.Master.FindControl("myMasterForm"), HtmlForm)
myForm.DefaultButton = Me.btnAdd.UniqueID
myForm.DefaultFocus = Me.txtMyTextbox.UniqueID
End Sub
Nothing to do with Master Pages - have a look here for how Web Browsers interprept the forms.
http://geekswithblogs.net/ranganh/archive/2006/04/12/74951.aspx
Personally I would enclose the form in its own Panel control and set the defaultbutton property to that of the submit button.
NOTE: This will only work in ASP.NET 2.0 and above.
IMHO, There is a BuiltIn Control designed for Login called as LoginView.
It integrates into your Master page or any other page and could provide full use of the authentication system. Here is the code for it
<asp:LoginView ID="LoginView1" runat="server">
</asp:LoginView>
Asp.Net provides a complete framework for authentication and authorization of an application. I would recommend having a look at if you are about to implement one for your application and you have not reviewed it as option already.
Security Framework For Asp.Net
EDIT: If you want a button to be place over master page, Drag and Drop the button like we do for a normal Web-form and Implement following event:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("MyApplication/SomePage.aspx");
}
Hope it Helps
(Page.Master.FindControl("Form1") as HtmlForm).DefaultButton = this.cmdSubmit.UniqueID;
From http://www.dotnetthoughts.net/2010/06/21/asp-net-default-button-and-master-pages/
Related
I have 5 dropdownlist in asp.net.User selects 5 dropdownlist after that click to button.Button sends page to another page.If i go previous page dropdownlist selectedvalues and datas lose(it displays default values without selected values)
I tried below
Response.Redirect("PreviousPage.aspx");
datas losing is there any solution ?
Instead of using Response.Redirect(), you can change the PostBackUrl of the button to the target page, or use Server.Transfer(). Once there you should be able to access the properties you need from the Page.PreviousPage object.
Example using Server.Transfer:
Page1.aspx.cs:
protected void SubmitButton_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx");
}
Page2.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
var addressDropdownSelectedValue = ((Page1)Page).PreviousPage.AddressDropdown.SelectedItem.Value; //or SelectedValue
}
With the ScriptManager in the AJAX control toolkit (you get this in 4.0+, I think maybe even in 3.5), you can add history points. You add a history point and use that value to rebuild the page state.
http://msdn.microsoft.com/en-us/library/cc488548%28v=vs.140%29.aspx
You can also strongly type the previous page if you want to grab values from it. You do this with <%# PreviousPage %> directive. Then in code you could use Page.PreviousPage.FindControl
I have a master page with a radtabstrip I use as my main navigation. In a content page I'd like to be able to disable/enable a tab from that strip. I've tried using findControl but I think it's returning null. Here's my code:
RadTabStrip menuStrip = (RadTabStrip)Master.FindControl("tabMain");
menuStrip.FindTabByText("Lookup Table").Enabled = true; //null reference error occurs
Fairly simple, but I'm guessing referencing an object like a radTreeStrip isn't the same as a label or textbox..any ideas?
EDIT:
I actually got it to work by making a function on my master page and calling it from the content page. However, what I REALLY need is to access the master page from a page that is not implementing the master page. Would that be possible? Or is there a way to include the master page but not show it's contents? –
You must first select the Page.Master and convert it to a MasterPage; next you load the control using FindControl, then convert it to a RadTabStrip, like so:
VB.NET
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim menuStrip As RadTabStrip = CType(CType(Page.Master, MasterPage).FindControl("tabMain"), RadTabStrip)
End Sub
C#
Proected void Page_Load(Object sender, System.EventArgs e)
{
RadTabStrip menuStrip = (RadTabStrip)((MasterPage)Page.Master).FindControl("tabMain")
}
EDIT:
If you want the same RadWindow to be used across multiple pages, make sure you make a different aspx page and add its URL inside the NavigateUrl of the RadWindow.
Below is a link from Telerik that displays info about the RadWindow and its NavigateUrl:
http://demos.telerik.com/aspnet-ajax/window/examples/contenttemplatevsnavigateurl/defaultcs.aspx
If I add the event to the RadDatePicker control then obviously it works
<telerik:RadDatePicker runat="server" ID="MyRadDatePicker" ClientEvents-OnDateSelected="clientEvent" />
.
.
But My question is how do I add the event "ClientEvents-OnDateSelected" from the code behind of my master page when the radDatePicker is in a custom control and that control is on the master page?
I tried this but it wont work?
protected void Page_Load(object sender, EventArgs e)
{
UC1MyCustomControl.FindControl("MyRadDatePicker").ClientEvents-OnDateSelected="clientEvent";
}
Cast the result of FindControl so you can access properties specific to RadDatePicker, and change the - in the attribute to .:
var datePicker = (RadDatePicker)UC1MyCustomControl.FindControl("MyRadDatePicker");
datePicker.ClientEvents.OnDateSelected = "clientEvent";
Instead of relying on FindControl, though, it might be better for the user control to expose the date picker as a public property:
// Inside the code-behind for the user control...
public RadDatePicker DatePicker
{
get { return MyRadDatePicker; }
}
Using this property, the master page can access the date picker easily:
UC1MyCustomControl.DatePicker.ClientEvents.OnDateSelected = "clientEvent";
+1 for Michaels answer. Best way is to for the custom control to expose the DatePicker as a property and the Master Pager can use that property to set the required events.
In the MSDN page lifecycle reference it states that the pre-init is used to "Create or re-create dynamic controls."
However, elsewhere on MSDN, an example implies that a dynamic user control should be loaded in Page_Load
Is this a contradiction? Or is pre-init used only for standard aspx controls?
What have I missed :)
Edit:
Either way works, however, there is presumably some benefit of one approach over the other.
(WebUserControl1 is a UserControl with a simple label property, SomeProperty)
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
WebUserControl1 wc1 = LoadControl(#"~\WebUserControl1.ascx") as WebUserControl1;
wc1.SomeProperty = "Hello World";
Controls.Add(wc1);
}
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1 wc1 = LoadControl(#"~\WebUserControl1.ascx") as WebUserControl1;
wc1.SomeProperty = "Hello World";
Controls.Add(wc1);
}
What exactly are you trying to do? Are you trying to pass information to the user control? If so you can pass the information to the user control in the page_load event from the parent page.
The answer is it depends on your custom control. If your user control doesn't need anything to happen before page_load, then you can go ahead and add your control during page load, if you have code in your user control that executes at an earlier time, then you should add your control at an earlier stage as the MSDN article suggests.
it is very easy to access master page control from content page like
protected void Page_Load(object sender, EventArgs e)
{
// content page load event
DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
userLabel.Text = thisDropDown.SelectedValue;
}
but how could i access controls of content page from master page. suppose a textbox there in content page and one button is there in master page. i want that when i will click on master page button then i want to show the text of textbox in the content page in the label of master page. how to achieve it. please help me with code sample. thanks.
In master page button click event should access page contents by:-
protected void Button1_Click(object sender, EventArgs e)
{
TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");
if (TextBox1 != null)
{
Label1.Text = TextBox1.Text;
}
}
It's been a while, but I believe you can do so by using the ContentPlaceHolder as a reference:
Control control = this.myContentPlaceHolder.FindControl("ContentPageControlID");
In my opinion it even better to use event raise from Master page and catch this event in contenet page for changing some contenet on this page, for instance. The main advantage is reusability. In future you may want to change content on other content page from the Master page and in this case you should only add event handler to this content page without changing code on master page. Within such approach you needn't hardcode control name from some content page. And moreover you shouldn't add dependency for some content's control at all.
A sample of implementation you can find here, for example.
you should look for contentplaceholder from master page then contentplaceholder in child of the master page
this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
You can find control by using this:
ContentPlaceHolder contentPage = Page.MasterPage.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label lblHead =(Label)contentPage.FindControl("lblHeading");
Response.Write(lblHead.Text);
Source:
http://xpode.com/ShowArticle.aspx?ArticleId=629
C# code within Site.Master:
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
Code within Site.Master.cs:
public partial class SiteMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
ToolkitScriptManager1.RegisterPostBackControl(this.MainContent.FindControl("btnOnDefaultPage"));
}
}
This is an example how a client control on the Default.aspx is referenced from a Site.Master.cs
Try this code
Page.Master.FindControl("MainContent").FindControl("DivContainer_MyProfile").Visible = True