asp.net masterpage - make hyperlink visible at runtime - asp.net

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.

Related

pass parameters with OnServerClick vb.net

I am really new in VB and It is hard to get my head around it especially working with non-MVC projects, so i wish i can find my answer here.
I want to pass arguments with the OnServerClick the code below is the html code:
<a id="Anchor1" OnServerClick="LogIn" runat=server> Click Me </a>
and the method in the server is below:
Protected Sub LogIn(sender As Object, e As EventArgs)
e.value
End Sub
I want to pass parameters(args) to the LogIn method??
I don't want to use any VB or razor buttons or anchor tags i want to use the html tags with the onserverclick attribute. is that possible??
thank you in advance.
You can add custom attribute to aspx <a> like this.
<a id="Anchor1" OnServerClick="LogIn" runat="server" customdata="hello"> Click Me </a>
And get that value in LogIn.
Protected Sub LogIn(sender As Object, e As EventArgs)
Dim myButton = CType(sender, HtmlAnchor)
Dim valuepassedfromUI = myButton.Attributes("customdata")
Dim buttonID = myButton.ClientID
End Sub
, See the screenshot.

How can I access my Mater Page's radtabstrip?

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

Updating button text when FormView item is Updated

I'm still somewhat new to ASP, and can't seem to figure out what the issue is.
When I update an item in FormView, I need the text of a button to change.
I'm using:
Protected Sub fvClientInfo_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles fvClientInfo.ItemUpdated
btnEditClient.Text = "Edit"
End Sub
The line of code is being called (can tell from debug mode), but the button text isn't changing and I have no idea why.
I should make sure to mention that the button is NOT inside the FormView.
Any ideas?
Try this.
Protected Sub btnchange() Handles FormView1.DataBound
Button1.Text = "Newtxt"
End Sub
and add this to your formview
OnDataBound="btnchange"

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?

Using aspx controls in base class

I've got two aspx pages which are very similar and have various identical functions in the code behind. I'd like to create a base class which both the code behind classes derive from. Is it possible for the base class to access the controls on the aspx page. For instance:
class base
inherits System.Web.UI.Page
Sub prepareScreen()
'txtName is a text box on the aspx page
Me.txtName.text = "George"
end sub
end class
class codeBehind
inherits base
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
prepareScreen()
end sub
end class
Somewhat understandably the code fails to compile with:
'txtName' is not a member of 'clsbase'
Is it possible to link the two together?
You need to declare the control as a property of the base class. Then in the ASP markup, use the CodeFileBaseClass attribute.
The MSDN reference is no longer available.
class base
inherits System.Web.UI.Page
Protected Property txtName() As TextBox
Sub prepareScreen()
'txtName is a text box on the aspx page
Me.txtName.text = "George"
end sub
end class
class codeBehind
inherits base
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
prepareScreen()
end sub
end class
<%# page CodeFileBaseClass="base" inherits="codebehind" ... %>
It would be better if you refactor your code, so that you have no need to do something like this.
One better idea will be if you create a virtual method in the base class, which you can override in your child page(s), and set the value of your textbox, as you'll have an easy access to the textbox.
You could use FindControl, eg.
TextBox txtName=FindControl("txtName");
which would find the control on the rendered page even though it was rendered by the descendant class. Though this is breaking the point of OO and separation of function/data somewhat.
You can use ((TextBox)Page.FindControl("txtName")) to get the textbox. Be careful because if you use this base class else where the control might not exist
In response to your clarification:
You could Create a property:
protected TextBox txtName
{
get{return (TextBox)Page.FindControl("txtName");}
set{Page.FindControl("txtName") = vale;}
}
Or Create a Virtual property:
protected virtual TextBox txtName{get;set;}
In this case you would have to override it at your main class
protected override TextBox txtName{/*same as above*/}

Resources