ASP .Net Interview Question Answers - 7

ASP .Net Interview Question Answers - 7

Q1: How do I loop through all the controls in a form?
A1: There are a variety of reasons a person may want to loop through the controls on a Form; perhaps to set a common color or to validate custom business rules. This kind of thing is not hard to do, but the logic is not entirely intuitive unless you know a few details.

You might already know that every Form has a Controls collection. From that, you might assume that you can simply loop through this collection to do what you need to all the controls in your form. You’d be wrong. A form is a complex tree of controls, and many controls can contain collections of controls themselves, such as a Panel and a Table. In fact, a form itself is nothing more than a fancy Control. (It inherits from, and extends the Control class.)

Since each tree branch can itself have N child branches, the only efficient solution is recursion. A recursive function is a function that calls itself as many times as necessary to work through any kind of hierarchical structure

The following function uses recursion to loop through all the controls in a Form and sets the BackColor of all TextBoxes to the specified value. The function works with both Web Forms and Windows Forms.


private void SetTextBoxBackColor(Control Page, Color clr)


foreach (Control ctrl in Page.Controls)


if (ctrl is TextBox)


((TextBox)(ctrl)).BackColor = clr;




if (ctrl.Controls.Count > 0)


SetTextBoxBackColor(ctrl, clr);






Private Sub SetTextBoxBackColor(ByVal Page As Control, _

ByVal clr As Color)

For Each ctrl As Control In Page.Controls

If TypeOf ctrl Is TextBox Then

CType(ctrl, TextBox).BackColor = clr


If ctrl.Controls.Count > 0 Then

SetTextBoxBackColor(ctrl, clr)

End If

End If


End Sub

Figure 1: This code loops recursively through all the controls in Form to set the backcolor of all the TextBoxes to a common color.

The function in Figure 1 can be called from pretty much anywhere in your code behind file (or Windows Form class) with a simple line such as this:

SetTextBoxBackColor(Me, Color.Red) 'VB.NET

- or -

SetTextBoxBackColor(this, Color.Red); //C#

The SetTextBoxBackColor function accepts the Form (or any other container control) as a parameter, along with the color that will be applied to the background of each TextBox within. The ForEach loop iterates through each control in the collection. If the Control is a TextBox, it applies the BackColor. If the control contains other controls, the function calls itself recursively to loop through each of those controls, and so on.

You can take these same principals and modify them in many ways as a potential solution for virtually anything you’d want to do to all your controls or to a certain kind of control. You now have the secrets you need to tinker with an entire control tree using recursion.

Q2: How do I store sensitive data securely?
A2: Here's a simple way you can make your apps appreciably more secure. Simply add the following VB .NET module to your project and call the HashData function to hash any sensitive data so it is secure from prying eyes:

Imports System.Text
Imports System.Security.Cryptography

Module modEncrypt
Public Function HashData(ByVal s As String) As String
'Convert the string to a byte array
Dim bytDataToHash As Byte() = _
(New UnicodeEncoding()).GetBytes(s)

'Compute the MD5 hash algorithm
Dim bytHashValue As Byte() = _
New MD5CryptoServiceProvider().ComputeHash(bytDataToHash)

Return BitConverter.ToString(bytHashValue)
End Function

End Module

Once your string parameter is hashed, it's computationally infeasible to determine the plain-text version. It cannot be decrypted.

Of course, this works better for some kinds of data than others. It works especially well for storing passwords in databases. When a new user signs up, simply hash his or her password and store the hashed value in the database. When the user logs in next time, hash the password and compare it to the hashed value you stored in the database. If the hashes match, admit the user.

Note, however, that if your user forgets the password, even you will not be able to decipher it. Most companies deal with this situation by auto-generating a new password and sending it to the user's registered e-mail address, or by implementing a system such as password hints or secret question/answer pairs.

If you absolutely need to be able to decrypt the data then hashing won't work. In this case you'll need to to use another encryption technique. A couple such techniques (both highly respected for their security) are Triple DES and Rijndael.

Q3: Why don't any controls show up on my page at run time?
A3: A problem people often come across when setting up a machine for ASP.NET is that the controls don't show up on the page at run time. A common symptom is that Label values show up on the page, but other controls, such as Buttons and TextBoxes, do not. Most of the time the problem is that your IIS mappings aren't set. One way this can happen is by installing IIS after the .NET Framework has been installed. Luckily, this situation is easily remedied. To repair the mappings, simply run "aspnet_regiis.exe" with the "-i" command line parameter. This program can be found within the Windows directory under the Microsoft.NET\Framework\version folder. In other words, for a .NET 1.0 Web application, you'd likely need to run this from the command prompt:

C:\Windows\Microsoft.NET\Framework\v1.0.3705\ASPNET_REGIIS.EXE -i

For a .NET 1.1 Web application, the default path is:

C:\Windows\Microsoft.NET\Framework\v1.1.4322\ASPNET_REGIIS.EXE -i

For a .NET 2.0 Web application, the default path is:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\ASPNET_REGIIS.EXE -i

Q4: What is a Server Control?
A4: In short, a server control is any control that has the runat="server" attribute in the definition. The job of such a component is to render HTML to the output stream of a Web page. All server controls inherit directly or indirectly from System.Web.UI.Control. All the standard controls in the Web Forms tab of your Visual Studio.NET toolbox are server controls. This means that you can interact with them from the server-side code in the code-behind file of the page, manipulate properties of the control from code, call methods of the control, and respond to server-side events raised by the control.

A control dragged onto a form from the HTML tab of the Visual Studio.NET toolbox is not a server control by default, but can easily be turned it into one by right clicking on the control and selecting Run As Server Control. This action simply adds the runat="server" attribute to the control. Placing an HTML control on a page without the runat="server" attribute would render the control on the page at run time just the same as any control, but it could not be directly manipulated via server-side code. Performance benefits will be gained from not using a server control when a control doesn't need to be manipulated from server-side code, because that's one less control the server needs to instantiate.

Q5: What's the difference between HTML controls and Web controls?
A5: In the Visual Studio.NET toolbox there is a tab for HTML controls, and another tab for Web controls. Many of the controls found in the HTML tab have a similar, roughly equivalent, control in the Web controls tab. When an HTML control is dragged onto a Web form, it's represented in your ASPX with code such as this:

no image avialable

A Web control will look more like this:

HTML controls are quick and small and require little memory. Conversely, Web controls provide more advanced functionality than HTML controls.

You'll find the HTML controls to be intuitive and familiar if you're an old-school HTML Web developer. However, you're more likely to feel at home with the extra features that Web controls provide if you have more of a Windows/ActiveX development background. Why have two sets of similar controls? Microsoft didn't want to abandon developers with either background, so it made a set of controls for each. Freedom of choice is a wonderful thing.

Be aware that HTML controls were not designed to be especially extensible. Therefore, when making your own controls, you should instead inherit from WebControl or Control.

Q6: What's the difference between User Controls and Custom Controls?
A6: There are two main categories of creatable Web controls. User controls are more simple to create, but custom controls are more simple to use. The extra effort that goes into creating custom controls pays off for the developer using the control at design time.

User controls are the natural evolution of include files, which are well known by old school ASP developers. If you're still using server-side include files in ASP.NET, it's time to leave them behind and dig into user controls, which are better in virtually every way. User controls are little more than a snippet of a Web page that can be dragged and dropped anywhere to duplicate bits of user interface and the associated functionality. (In fact, it's not difficult to convert an entire Web page into a user control.) They are object-oriented, so public properties can be added, as well as methods and events, to allow them to easily communicate with the page and other controls on the page.

User controls are saved with an ASCX file extension. Although they can be easily reused on any of the pages within the project in which they were created, there is no good way to reuse them in a different project. Another downside to user controls is that they only appear on the page at run time; at design time they appear on the page as an ugly gray box, making it difficult to envision how they'll appear to your users.

Figure 1 shows the HTML source code for a basic sidebar menu user control. A standard control like this is a navigational staple needed by virtually every page in a Web site. It would be wasteful to manually re-create such a structure over and over again for each page. Instead, simply drag this control from the Visual Studio.NET solution explorer window onto each page as needed. This example does not include any code-behind logic, but any user control can have a code-behind file of its own to contain any necessary server-side code. Figure 2 shows how the control looks on a Web page that includes a CSS style sheet to enhance the appearance.

<.%@ Control Language="vb" AutoEventWireup="false" %.>
<.TABLE id="Table1" style="WIDTH: 136px; HEIGHT: 99px" cellSpacing="1" cellPadding="1" width="136" border="1".>
<.TD align="center".>
<.asp:HyperLink id="HyperLink1" runat="server" NavigateUrl="Home.aspx".>Home<./asp:HyperLink.>
<.TD align="center">
<.asp:HyperLink id="HyperLink2" runat="server" NavigateUrl="Products.aspx".>Products<./asp:HyperLink.>

<.TD align="center".>

<.TD align="center".>
<.asp:HyperLink id="HyperLink4" runat="server" NavigateUrl="Contact.aspx".>Contact Us

Figure 1: (Above) A basic reusable side menu user control can be as simple as this. This control was created completely by dragging and dropping controls from the Visual Studio.NET toolbar onto an .ASCX user control page.

Figure 2: (Above) User controls are great for defining the layout for standard sections of Web pages. When combined with style sheets, they can morph to match the look and feel of any Web page on which they are hosted.

Custom controls can do everything that user controls can do - and much more. The biggest drawback is that they are more challenging and complex to create. Drag and drop is not supported for the creation of these controls. Therefore, all the HTML they output must be generated via code. Pretty much all the articles & controls on this web site are examples of custom controls. In addition to run-time functionality without limits, custom controls support rich design-time functionality. They can appear on a page at design time the same way they'll appear at run time, or differently if preferred. They can be adjusted to change how they'll appear in the toolbox, and how the properties will appear in the properties window. If the properties window doesn't support the desired functionality, it can be extended with custom popup dialog boxes and other UI tricks. Custom controls are easily distributed and can be enhanced with help documentation and licensing functionality, allowing them to be sold to other developers.

Q7: How do I save the state of my controls between postbacks?
A7: In many cases no action is required to save the state of controls between postbacks. When encapsulating existing controls within a user or custom control, they'll often save their own state just as they do when using them directly on Web pages. However, there are circumstances that will require custom data to be saved between postbacks. Imagine a simple control that contains only a LinkButton and a Label control. To save the number of times a user has clicked on the LinkButton and display that count in the Label control, a block of VB.NET code like this can be used in the code-behind:

Private Sub LinkButton1_Click(ByVal sender As Object,
ByVal e As EventArgs) Handles LinkButton1.Click
Dim i As Integer = CInt(ViewState("ClickCount"))
i = i + 1
ViewState("ClickCount") = i
Label1.Text = "Clicks: " & i.ToString
End Sub

This code saves the custom data value in ViewState, indexed under the name "ClickCount". This code sample gets any existing value out of ViewState, increments it, then puts the new value back into ViewState. Finally, the value is displayed in the Label control. Because each control is automatically given a unique naming container within ViewState, control developers don't need to worry about naming collisions between multiple instances of a control on the same page. Figure 1 illustrates the fact that each instance of a control automatically has its own unique "ClickCount" ViewState value.

Figure 1: Each instance of a control automatically gets a unique ViewState, so you needn't be concerned with giving each instance of a control a unique ViewState name.

Q8: How do I set the focus to a particular control on my page?
A8: The Web browser controls this kind of functionality; therefore this must be done via client-side code that runs within the browser. The following JavaScript snippet can be added to the bottom of the HTML view of your page:

Perhaps it isn't known until run time which control should get the focus. In this case, code similar to the above example could be generated and output dynamically at run time via server-side code. For this, a subroutine like the one shown in Figure 1 could be used.
Private Sub SetFocus(ByVal ctrl As Control)
Dim sb As New System.Text.StringBuilder
RegisterStartupScript("SetFocus", sb.ToString)
End Sub

Figure 1. This VB.NET code will set the initial focus to any control that is passed to it.

To call the method, simply execute a line of code such as this from the code-behind of a Web page:


Q9: How do I send the output of a Web control via e-mail?
A9: Assuming SMTP is configured properly on the server, the output of nearly any Web control can be captured via the output of its RenderControl method, and then sent out using the MailMessage class of the System.Web.Mail namespace. Such functionality is provided by the VB.NET method shown in Figure 2, which you can call from the code-behind class of any page.
Private Sub EmailControl(ByVal ctrl As Control,
ByVal ToEmailAddress As String)

'Get the output of the control
Dim sb As New System.text.StringBuilder
Dim sw As New IO.StringWriter(sb)
Dim tw As New HtmlTextWriter(sw)

'Send the email
Dim MailMsg As New Web.Mail.MailMessage
Dim svr As Web.Mail.SmtpMail
svr.SmtpServer = ""
MailMsg.To = ToEmailAddress
MailMsg.From = ""
MailMsg.BodyFormat = Mail.MailFormat.Html
MailMsg.Subject = "Test Email"
MailMsg.Body = sb.ToString
End Sub

Figure 5: This code will grab the HTML output of a control and send it to the specified e-mail address.

This code works well primarily for read-only controls, such as Labels, DataGrids, LinkButtons, etc. Editable controls such as TextBoxes don't tend to work as well because they expect to be hosted inside a form.

Of course, any network administrator will tell you that e-mail is a complex topic involving firewalls, spam filtering systems, and more. Therefore, there are plenty of ways the code in Figure 5 could fail, so (as always) you should employ

Share this

Related Posts

There was an error in this gadget