This code will return the file name of the user control, placed in the code behind of a user control. This can be useful when creating generic code that needs to know the name of the user control. By eliminating the need to hard code the file name into the
code behind, the file name can be renamed, copied, etc. without having to update the hard coded name. It effectively allows the control to ask the question Who Am I?. This should also work when you need the name of the current page. However it has not been
tested for that function.
This is different from the various techniques to get the file name of the PAGE (orders.aspx) in that it returns the FILE NAME of a USER CONTROL (MyInformation.ascx) in the event that the USER CONTROLS needs to know its own name.
This code block demonstrates the practical use of this. Anytime I create JavaScript in code behind, I always include a comment that details the file that created the code in order to help debug the JavaScript if an issue arised. This technique allows the file name to be change without having to change the statement. It also allows for code reuse.
Protected Sub FormViewContactDetail_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ClientSideJavaScript As New StringBuilder
ClientSideJavaScript.Append("//Code File" & Me.AppRelativeVirtualPath.Replace(Me.AppRelativeTemplateSourceDirectory, "") & Chr(10))
ClientSideJavaScript.Append("function HidePopup(TargetPopup) {" & Chr(10))
ClientSideJavaScript.Append("var popup = $find(TargetPopup);" & Chr(10))
ClientSideJavaScript.Append("popup.hidePopup();" & Chr(10))
ClientSideJavaScript.Append("}" & Chr(10))
ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "ShowSetup_ServiceKitClientSideScript", ClientSideJavaScript.ToString, True)
End Sub
Searches:How do I get the filename of a user control? Get filename of a user control Get a user control's file name User control file name property filename of a ascx file code behind
dch3
Member
447 Points
638 Posts
Obtain a User Control's File Name From Within Its Code Behind (Who Am I?)
Aug 11, 2011 06:20 PM|LINK
This code will return the file name of the user control, placed in the code behind of a user control. This can be useful when creating generic code that needs to know the name of the user control. By eliminating the need to hard code the file name into the code behind, the file name can be renamed, copied, etc. without having to update the hard coded name. It effectively allows the control to ask the question Who Am I?. This should also work when you need the name of the current page. However it has not been tested for that function.
This is different from the various techniques to get the file name of the PAGE (orders.aspx) in that it returns the FILE NAME of a USER CONTROL (MyInformation.ascx) in the event that the USER CONTROLS needs to know its own name.
This code block demonstrates the practical use of this. Anytime I create JavaScript in code behind, I always include a comment that details the file that created the code in order to help debug the JavaScript if an issue arised. This technique allows the file name to be change without having to change the statement. It also allows for code reuse.
Protected Sub FormViewContactDetail_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Dim ClientSideJavaScript As New StringBuilder ClientSideJavaScript.Append("//Code File" & Me.AppRelativeVirtualPath.Replace(Me.AppRelativeTemplateSourceDirectory, "") & Chr(10)) ClientSideJavaScript.Append("function HidePopup(TargetPopup) {" & Chr(10)) ClientSideJavaScript.Append("var popup = $find(TargetPopup);" & Chr(10)) ClientSideJavaScript.Append("popup.hidePopup();" & Chr(10)) ClientSideJavaScript.Append("}" & Chr(10)) ScriptManager.RegisterClientScriptBlock(Me, Me.GetType, "ShowSetup_ServiceKitClientSideScript", ClientSideJavaScript.ToString, True) End SubSearches:How do I get the filename of a user control? Get filename of a user control Get a user control's file name User control file name property filename of a ascx file code behind
Bryan Strade...
Participant
1478 Points
248 Posts
Re: Obtain a User Control's File Name From Within Its Code Behind (Who Am I?)
Aug 13, 2011 05:08 AM|LINK
Explore the System.Reflection namespace, there are many items there to help get current assembly/class information.