What is the use of params keyword? Give an example.

Tuesday, July 14, 2009


params is used on such situations where the number of arguments to a method is not known until runtime. If you prefix the params keyword followed by the type as array without size, then you can pass as many arguments as you want.


Ex:
using System;
using System.Collections.Generic;
using System.Text;

namespace ParamSample
{
class Program
{
public static void PrintWords(params String[] p)
{
for (int i = 0; i < p.GetLength(0); i++)
{
System.Console.WriteLine(p[i]);
System.Console.Read();
}
}

static void Main(string[] args)
{
PrintWords("1","2","3");
}
}
}



www.codecollege.NET

What is the use of the AssociatedControlID?


If you want to associate a control with Label, then you can give an access key to the label control and set the AssociatedControlID property of the Label to the control to which you want the label to associate. When you do so, upon clicking the AccessKey Focus will be set to the Associated Control.


Ex:
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" AccessKey="N" AssociatedControlID="TextBox1"
Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" AccessKey="B" OnClick="Button1_Click" /></div>
</form>



www.codecollege.NET

How will set an Accesskey for a button?


You can do that by setting the AccessKey attribute of the button.


Ex:
<asp:Button ID="btnTest" runat="server" AccessKey="T" Text="Test" OnClick="btnTest_Click" />



www.codecollege.NET

How will you make a button the default button?


You can do that by setting the DefaultButton attribute of the form in which the button is present.


Ex:
<form id=f1 defaultbutton="btnSubmit" runat="server">
<asp:textbox id="textbox1" runat="server"/>
<asp:button id="button1" text="btnSubmit" runat="server"/>
</form>



www.codecollege.NET

What is the purpose of AutoEventWireup Page directive?


When set to true , it ensures that the event handlers for the page will be automatically called when the events occur on the page.



www.codecollege.NET

What is the difference between the Write and Warn methods of the TraceContext Class?


Write - Writes the message in default colour.
Warn - writes the message in red colour.



www.codecollege.NET

How will you enable tracing in a asp.net page?


By setting the following page directive,
Trace=True


Ex:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Trace="true" %>



www.codecollege.NET
Blog Widget by LinkWithin