How can I access my Mater Page's radtabstrip? - asp.net

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

Related

Adding to page control collection from inside a user control

I have an asp.net usercontrol which represents a "popup" dialog. Basically, it's a wrapper for the jQuery UI dialog which can be subclassed to easily make dialogs.
As part of this control, I need to inject a div into the page the control is used on, either at the very top or very bottom of the form so that when the popup is instantiated, it's parent is changed to this div. This allows "nested" popups without the child popup being trapped inside the parent popup.
The trouble is, I can't find a safe way to inject this div into the page. A usercontrol doesn't have a preinit event, so I can't do it there, and calling Page.Form.Controls.Add(...) in Init, Load or PreRender causes the standard exception "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
I thought I had found a solution by using...
ScriptManager.RegisterClientScriptBlock(Page, Me.GetType, UniqueID + "_Dialog_Div", containerDiv, False)
... which seemed to work well normally, but recently a coworker tried putting an UpdatePanel inside the dialog and now she's getting the error "The script tag registered for type 'ASP.controls_order_viewzips_ascx' and key 'ctl00$ContentBody$OViewZips_Dialog_Div' has invalid characters outside of the script tags: . Only properly formatted script tags can be registered."
How are you supposed to add controls to the pages control collection from inside a user control?
I'm not sure why you really need to add this div to the page's form, but this should work:
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Private Sub UC_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
AddHandler Me.Page.Init, AddressOf Me.Page_Init
End Sub
Private Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Dim dialogDiv As New Panel
dialogDiv.ID = "DialogDiv"
If Not Page.Form.Controls.Contains(dialogDiv) Then
Page.Form.Controls.AddAt(0, dialogDiv)
End If
End Sub
End Class
How to implement it in C#. I'm always getting the error The control
collection cannot be modified on Load, PreRender
I need a Literal Control to be added to my master page's head from a user control. The literal control will contain the css link.
Do you want to add the literal to the HeadContent-ContentPlaceHolder control in the master or to the page's header(the html head element)? However, here i show both.
Here's the codebehind of your UserControl:
public partial class UC_AddToMaster : System.Web.UI.UserControl
{
private void Page_Init(object sender, System.EventArgs e)
{
this.Page.Init += UC_AddToMaster_Init;
}
private void UC_AddToMaster_Init(object sender, EventArgs e)
{
Literal literal = new Literal{ Text = "Hi World!" };
// if you want to add it to the header of the page:
if (!Page.Header.Controls.Contains(literal))
{
Page.Header.Controls.AddAt(0, literal);
}
// if you want to add it to the master's HeadContent ContentPlaceHolder control:
var siteMaster = Page.Master as SiteMaster;
if (siteMaster != null)
{
if (!siteMaster.Head.Controls.Contains(literal))
{
siteMaster.Head.Controls.AddAt(0, literal);
}
}
}
}
For the HeadContent approach mentioned above i've provided following property in the master:
// in the master
public ContentPlaceHolder Head { get { return this.HeadContent; } }
Therefore i needed to cast the page's master to it's actual type(SiteMaster here) in the UserControl. Otherwise i couldn't access this property.
Usercontrol doesn't have a PreInit event, but nothing's stopping you from adding a handler to the Page PreInit event in your UserControl:
Page.PreInit += new EventHandler(Page_PreInit);
edit - you're right - you can't capture PreInit from a usercontrol, though I'm surprised you can't - but you can still change the page controls collection by adding code to the constructor of the UserControl. I tried this and it works.
public MyUsercontrol()
{
Page page = (Page)HttpContext.Current.Handler;
Literal lit = new Literal();
lit.Text="text";
page.Controls.Add(lit);
}
Adding an event handler to Page.PreInit in the constructor compiles but it never fires.
(end edit)
That said, I'm not exactly sure why this is necessary to achieve your goal. Why don't you just have your dialog control render it's own div in-line wherever you drop it into the form, and use that as the parent, instead of trying to create one somewhere else in the form? I can't think of why it would be important for it to physically be rendered at the beginning or end of the form. It's a dialog so it will always be invisible until you use it, right?

server.transfer in asp.net?

When i write server.transfer("defaul2.aspx");
in url default2.aspx is not displaying instead of it is showing defaul.aspx page.
when i write response.redirect("default2.aspx");
in url default2.aspx page is showing what is there any different.
That's because Server.Transfer() and Response.Redirect() don't work the same way.
Server.Transfer() doesn't end the current request, it only instructs ASP.NET to stop rendering the current page and start rendering the new page instead. The client is none the wiser, from its point of view the server is still responding to the initial request, so the URL displayed in the address bar does not change.
Response.Redirect() ends the current request and sends a 302 response code to the client. The client then issues another HTTP request to the redirected URL and processes the response. Since the client knows that the URL has changed, it displays the redirected URL in its address bar.
Response.Redirect method:
it helps in navigating to another page from code. This is like clicking a hyperlink.To navigate from one page to another using button click or linkbutton control or from server side code use Response Object's Redirect method.
using Response.rediect() method you can not get information from source page on target page.
its source code is like
VB.Net Code
Private Sub Button1_Click(ByVal Sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
'Display target page.
Response.Redirect("Target.aspx")
End Sub
C# Code
private void Button1_Click(Object sender, System.EventArgs e)
{
//Display Target Page.
Response.Redirect("Target.aspx");
}
Using Server.Execute Method
use Server.Execute Method to process a Target web form without leaving the Source page. This technique let you embed the result from a target page to a region on Source page. Like Server.Transfer, It also required EnableViewStateMac attribute to page directive set to false.
Suppose I have to pages one is Calculate.aspx having two textboxes and a button control. On button Click event. I performed Server.Execute to Result.aspx another aspx page.
Source Code:
protected void btn_Click(object sender, EventArgs e)
{
Server.Execute("Result.aspx");
}
write below code in Result.aspx pageload event.
protected void Page_Load(object sender, EventArgs e)
{
NameValueCollection colform = new NameValueCollection();
colform = Request.Form;
Response.Write("<h2>Additon Result:</h2>" + (Convert.ToInt32(colform["TextBox1"]) + Convert.ToInt32(colform["TextBox2"])));
}

asp.net masterpage - make hyperlink visible at runtime

Is it possible to alter the visible property of hyperlinks on a masterpage at runtime?
thanks
If you expose the hyperlink as a property on the master page, you can get a reference to a pages master and cast that to your specific master page then set visibility of the hyperlinks using that property.
In your master page have something like this:
Public ReadOnly Property RemitViewerLink() As HyperLink
Get
Return hlRemitViewer
End Get
End Property
Then in your child page you can do this
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim MyMaster As MasterPage = DirectCast(Page.Master, MasterPage)
MyMaster.RemitViewerLink.CssClass = "selectedMenuItem" 'or set visibility
End Sub
No, if you set visible=False then it won't even display in the HTML output of the page. You would need to use javascript to hide/show the hyperlinks instead.

Default button in asp.net (Master Page)?

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/

Dynamically add CalendarExtender to Textbox subclass server control?

I'm trying to create a server control, which inherits from TextBox, that will automatically have a CalendarExtender attached to it. Is it possible to do this, or does my new control need to inherit from CompositeControl instead? I've tried the former, but I'm not clear during which part of the control lifecycle I should create the new instance of the CalendarExtender, and what controls collection I should add it to. I don't seem to be able to add it to the Page or Form's controls collection, and if I add it to the (TextBox) control's collection, I get none of the pop-up calendar functionality.
I accomplished this in a project a while back. To do it I created a CompositeControl that contains both the TextBox and the CalendarExtender.
In the CreateChildControls method of the CompositeControl I use code similar to this:
TextBox textbox = new TextBox();
textbox.ID = this.ID + "Textbox";
textbox.Text = this.EditableField.TextValue;
textbox.TextChanged += new EventHandler(HandleTextboxTextChanged);
textbox.Width = new Unit(100, UnitType.Pixel);
CalendarExtender calExender = new CalendarExtender();
calExender.PopupButtonID = "Image1";
calExender.TargetControlID = textbox.ID;
this.Controls.Add(textbox);
this.Controls.Add(calExender);
Of course make sure that the form containing this CompositeControl has a toolkit script manager.
I know this is an old thread, but I came across it when I had a similar question. This is what I ended up implementing, and it works great. If you want the control to BE a TextBox, then simply pump out the extender during the call to Render.
Imports System.Web.UI.WebControls
Imports AjaxControlToolkit
Public Class DateTextBox
Inherits TextBox
Private _dateValidator As CompareValidator
Private _calendarExtender As CalendarExtender
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
_dateValidator = New CompareValidator
With _dateValidator
.ControlToValidate = ID
Rem set your other properties
End With
Controls.Add(_dateValidator)
_calendarExtender = New CalendarExtender
With _calendarExtender
.TargetControlID = ID
End With
Controls.Add(_calendarExtender)
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
MyBase.Render(writer)
_dateValidator.RenderControl(writer)
_calendarExtender.RenderControl(writer)
End Sub
End Class
You can easily add ajax calendar in custom server controls. You need to add two reference in your application.
1. AjaxControlToolkit.dll
2. System.Web.Extensions
With the help of second reference we will get all the property of “CalendarExtender” in your custom server controls.
When you are trying to not allow users to type anything in the textbox, but only be filled by the calendar extender and then you try to get the selected date from the textbox control it may be empty string if you have set the textbox property to ReadOnly="True".
Its because read only controls are NOT posted back to the server. Workaround for this is the following:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("readonly", "readonly");
}
Hope it helps.

Resources