ASP.NET Interview Questions and Answers (1544) - Page 69

What is an alternative way of assign css class to an element?

With the help of class property of controls,we can apply the same css class as with CSSClass property as:-

<asp:Button ID="Button1" runat="server" Text="Button 1" class="button_border"/>

<input type="button" name="button" id="Button2" value = "Button 2" class="button_border" />

What is the difference between class and CSSClass property?

Both properties are used to apply any CSS class to the controls or elements but there is only 1 difference between both that is we can use both properties for Server controls but only use class property for HTML controls.
How to assign a CSS class dynamically?

We can write below code in code behind as:-
Button16.CssClass = "button_border";

Where,button_border is my CSS class name.
What is an alternative way of assigning or applying CSS dynamically?

We can write below code in code behind as:-
Button17.Attributes.Add("class", "button_border");

Where,button_border is my CSS class name.
What is the difference between BorderWidth and border-width property?

BorderWidth is a control property whereas border-width is a control style property but both properties are used to to set control's or element's border width but there is only 1 difference between both that is we can use both properties for Server controls but ONLY use border-width property for HTML controls.
What is the difference between BorderStyle and border-style property?

BorderStyle is a control property whereas border-style is a control style property but both properties are used to to set control's or element's border style but there is only 1 difference between both that is we can use both properties for Server controls but ONLY use border-style property for HTML controls.
How to set control's borders through Javascript?

Place following code in Head tag and call that function in Button onClientClick client-side event as:-
<script type="text/javascript">

function setborder()
{
document.getElementById('Button19').style.borderWidth = "5px";
document.getElementById('Button19').style.borders = "dashed";
}
</script>


And call above function as:-
<asp:Button ID="Button18" runat="server" Text="Border Width 9" OnClientClick="setborder(); return false;" />

What is an alternative way of calling JS function in control definition?

We have onclick event of HTML control through which we call call JS function.
<input type="button" name="button" id="button1" onclick="setborder();" />

Where,setborder() is my Javascript function.
What is the difference between OnClientClick and onclick Client side event in control definition?

Both are Client-side Javascript event but OnClientClick is a server-side control property whereas onclick is a Client-side or HTML control Javascript event and both events are used to to call Javascript function.But there is only one difference between both that is we can use OnClientClick for Server controls and onclick event for HTML controls.
How to call JS function through Button control dynamically?

By writing following code,we can call JS function through Button control or Linkbutton,ImageButton etc..
Syntax:-
Button1.Attributes.Add("onclick", "return JSFunction();");

For Example:-
Button1.Attributes.Add("onclick", "return setborder();");

What is an alternative way of calling JS function through Button control dynamically?

We have Button i.e. Command button,Link button,Image button OnClientClick property just call it and assign any JS function to it.
For Example:-
Button1.OnClientClick = "return JS_Function();";

How to call JS function With the help of ScriptManager.RegisterStartupScript method in code behind?

RegisterStartupScript is a static method of ScriptManager class which manages AJAX script libraries and script files.

For Example:-
ScriptManager.RegisterStartupScript(this,this.GetType(), "any name", "alert('Hello');", true);

ScriptManager.RegisterStartupScript(this,this.GetType(), "any name", "any js function", true);

How to call JS function With the help of ScriptManager.RegisterClientScriptBlock method in code behind?

Refer below code:-

For Example:-
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", "alert('Hello');", true);

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "any name", "any js function", true);

What is the difference between ScriptManager.RegisterStartupScript method and ScriptManager.RegisterClientScriptBlock method?

Both are used to call any Javascript function or show MESSAGE in code-behind but we can not access any Page control through ScriptManager.RegisterClientScriptBlock method whereas we can access any control via ScriptManager.RegisterStartupScript method.So if we want to show any Messages then use RegisterClientScriptBlock method and if you want to call any page control then use RegisterStartupScript method.
How to show a calendar on the page in ASP.NET?

Dot net provides its own calendar control which is used for showing calender on the page.

This control displays a one-month calendar that allows the user to select dates and move to the next and previous months.

In other words,The Calendar Control shows the Current Month Dates and the Dates also of the previous and Next Month.

For Example:-
<asp:Calendar id="calDate" runat="server" BorderWidth="1px" NextPrevFormat="FullMonth" BackColor="LightYellow" Width="400px" ForeColor="Red" Height="170px" Font-Size="10pt" Font-Names="Verdana" BorderColor="Blue"></asp:Calendar>

What do we mean by Caption property of Asp.Net Calender control?

The Caption property of Calender control is used to get or set a text that is shown as a Text for the calendar.It's same as label text.

For Example:-
<asp:Calendar id="calDate" runat="server" Caption="Simple Example of Calender control" BorderWidth="2px" />

What is the Event of Asp.Net Calender Control?

It has OnSelectionChanged event which gets fired when user clicks on any date from calender control.

For Example:-
<asp:Calendar ID="caldate" runat="server" OnSelectionChanged = "caldate_SelectionChanged"></asp:Calendar>


protected void caldate_SelectionChanged(object sender, System.EventArgs e)

{

}

How to get the date selected by Calender control?

We have Calender control's SelectedDate property which is used to get ant selected date from calender control?

For Example:-
DateTime dt = caldate.SelectedDate;


Where,caldate is my Calender control.
How to disable Caching on the browsers?

It's better way to disable caching,so the web page must be requested fresh or new each time from the server.

On Page Load event write below code:-

Response.Expires = 0;

Response.Cache.SetNoStore();
Response.AppendHeader("Pragma","no-cache");

So whenever the Page_Load event occures, it will ask a new copy from server, so that we can write code which will check that whether the request is valid or not.
How to Disable Copy Paste and Cut in a ASP.NET Webpage TextBox?

Write oncopy,onpaste and oncut event on Textbox definition like below:-

<asp:TextBox ID = "txt_employee_code" runat = "server" oncopy = "return false" onpaste = "return false" oncut = "return false">

</asp:TextBox>

Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories