• Sign up
  • ‎What is Shvoong?‎
  • Sign In
    Sign In
    Remember my username Forgot your password?

Summaries and Short Reviews

.

Shvoong Home>Science>Engineering>User Controls in .net(FAQ) Summary

.

User Controls in .net(FAQ)

Article Abstract by: rattlesnakespy    

Original Author: Rattlesnakespy
1.How can I include an Asp.NET page in many other Asp.Net pages so that I don''t need to rewrite the code?
>One of
the approaches is to write and use a UserControl. You can define the user interface, handle events and expose properties in a UserControl, and then mark a reference and reuse these Controls in any page of your Web application.
2.Is there anything similar to web templates in ASP that I can use in ASP.Net?
>In ASP.NET, you would typically use a User Control to create reusable controls.
3.Are there any settings on the application level to make certain elements appear on every page without having to manually insert code or insert UserControls?
>You can use HttpHandlers and/or HttpModules to insert information on all pages transparently. Check out Bipin Joshi''s article Extending ASP.NET with HttpHandlers and HttpModules
4.How to pass a parameter to a user control?
>Create a User Control
Below Code goes in User Control
<asp:ImageButton id="ImageButton1" runat="server"></asp:ImageButton>
Create a property called source
VB
Public Property source() As String
Get 
           Return ImageButton1.ImageUrl 
      End Get 
      Set(ByVal Value As String) 
           ImageButton1.ImageUrl = Value 
      End Set  
End Property 
C#  
public string source  

     get  
     {  
          return ImageButton1.ImageUrl;  
     }  
     set  
     {  
          ImageButton1.ImageUrl = value;  
     }  

Now in your webform:
Drag and drop User Control and set the source property.  
<uc1:UCImageButton source="b2346.jpg" id="UCImageButton1" runat="server"></uc1:UCImageButton> 
5.How to add user control dynamically into another usercontrol?
 >Here is an example of adding one UserControl into another: 
uc1.ascx:  
<asp:Label runat="server" text="uc1" ID="Label1" />  
<asp:Panel runat="server" id="p1" >Panel UC1</asp:Panel>  
uc2.ascx:  
<br><asp:Label runat="server" text="uc2" ID="Label1" /> 
VB.NET 
Dim uc1 As UserControl = CType(LoadControl("uc1.ascx"), UserControl)  
Controls.Add(uc1)  
Dim uc2 As Control = uc1.LoadControl("uc2.ascx")  
Dim p1 As Control = uc1.FindControl("p1")  
p1.Controls.Add(uc2) 
C# 
UserControl uc1 = (UserControl)LoadControl("uc1.ascx");  
Controls.Add(uc1); 
Control uc2 = uc1.LoadControl("uc2.ascx");  
Control p1 = uc1.FindControl("p1");  
p1.Controls.Add(uc2); 
6.I have a function inside of the .ascx file. How can I call it from the web application page(the .aspx file)?
>All you need to do is give your user control an ID in the aspx. e.g. 
<myTagTest:MarcTest id=myUC runat="server">  
</myTagTest:MarcTest> 
Then in your aspx code, you can simply use the id to call public methods (and properties) defined by the ascx. e.g.
VB.NET  
myUC.writeData(...) 
C#  
myUC.writeData(...) 
7.How to dynamically load User Controls?<>>Create a User Control 
<P>  
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P> 
<P>  
<asp:Label id="Label1" runat="server"></asp:Label></P> 
On button Click
VB.NET  
Label1.Text = "Hello" 
C#  
Label1.Text = "Hello" ; 
Create a Webform to use a UserControl 
<asp:Panel id="Panel1" runat="server"></asp:Panel> 
VB.NET  
Dim myControl As Control = CType(Page.LoadControl("UC1.ascx"), Control)  
Panel1.Controls.Add(myControl) 
C#  
Control myControl =(Control)Page.LoadControl("UC1.ascx");  
Panel1.Controls.Add(myControl); 
8. How to change the imageurl of the image control that exists in a usercontrol?
>In the .ascx: 
<asp:Image id="Image1" runat="server"></asp:Image> 
In .aspx
VB.NET  
<uc1:UCImage id="UCImage1" source="b2346.jpg" runat="server"></uc1:UCImage> 
Dim UC As UserControl  
Dim imgUC As System.Web.UI.WebControls.Image  
UC = Page.FindControl("UCImage1")  
imgUC = UC.FindControl("Image1")  
imgUC.ImageUrl = "b2346.jpg" 
C# 
UserControl UC ;  
System.Web.UI.WebControls.Image imgUC ;  
UC = (UserControl)Page.FindControl("UCImage1");  
imgUC =(System.Web.UI.WebControls.Image)UC.FindControl("Image1");  
imgUC.ImageUrl = "b2346.jpg"; 
 
Published: April 02, 2007
Please Rate this Review : 1 2 3 4 5

Bookmark & share this post

.