Wednesday, May 11, 2011

Difference between SessionState and ViewState?

SessionState and ViewState are used to store data value when an respective postback occurs.

SessionState is used to store Value till the user end's the session.
ViewState is used to store Value for the current page only and when we switch to other page the data value of the previous page is lost.

Session is Server type storage whereas View is a client type storage.

Session provide higher security as compared to View as the data value is stored on server side.

How can we save all data from dataset?

Dataset has “Accept Changes” method, which commits all the changes since last time “Accept changes” has been executed.


Difference between static variable and constant in c#?

Static Variable:
1.Variable set at run time
2.Can be assigned for reference types.
3.Initialized on constructors

Constant:
1.Variable set at compile time itself.
2.Assigned for value types only
3.must be initialized at declaration time
4.only primitive data types.

Can you access controls on the Master Page without using FindControl() method?

Yes, by casting the Master to your MasterPage as shown in the below code sample.
protected void Page_Load(object sender, EventArgs e)
{
MyMasterPage MMP = this.Master;
MMP.MyTextBox.Text = "Text Box Found";
}

From the content page code how can you reference a control on the master page?

Use the FindControl() method as shown in the code sample below.
void Page_Load()
{
// Gets a reference to a TextBox control inside
// a ContentPlaceHolder
ContentPlaceHolder ContPlaceHldr = (ContentPlaceHolder)Master.FindControl ("ContentPlaceHolder1");
if(ContPlaceHldr != null)
{
TextBox TxtBox = (TextBox)ContPlaceHldr.FindControl("TextBox1");
if(TxtBox != null)
{
TxtBox.Text = "TextBox Present!";
}
}
// Gets a reference to a Label control that not in
// a ContentPlaceHolder
Label Lbl = (Label)Master.FindControl("Label1");
if(Lbl != null)
{
Lbl.Text = "Lable Present";
}
}

Can you access non public properties and non public methods of a master page inside a content page?

No, the properties and methods of a master page must be public in order to access them on the content page.