Call javascript function from code behind in C#
Suppose you want to call javascript function ParentRefresh() from code behind in C#.
<script type="text/javascript">
function ParentRefresh()
{
// javascript code
}
</script>
protected void Save_Click(object sender, EventArgs e)
{
// your save code
ScriptManager.RegisterStartupScript(this, GetType(), "message", "ParentRefresh();", true);
}
Thursday, 18 December 2014
Call javascript function from code behind in C#
Access a parent element (Button) from iframe using c#
Access parent element (Button) from iframe using c#
Suppose you have 2 pages:
1. Parent Page: On which there is a iframe. as Parent.aspx
2. Loaded page: inside the
iframe where Child.aspx is loaded
You want if you save some thing on Child.aspx (Loaded page), the grid on Parent page (Parent.aspx) will automatically refresh.
So you make a hidden button on the Parent page (Parent.aspx)
<div style="display:none">
<asp:Button ID="Done" runat="server" ClientIDMode="Static" OnClick="Done_Click" />
</div>
protected void Done_Click(object sender, EventArgs e)
{
// Refresh Grid
}
Note that the ClientIDMode="Static" because from the other page you can not know what is the rendered id, so keep it static. If you do not use dot net 4 that have the static id, then you need to also use javascript to send the rendered id to the page, or some other way to
locate the button.
Call a javascript function on Child.aspx (Loaded Page)
<script type="text/javascript">
function ParentRefresh()
{
window.parent.document.getElementById('Done').click();
}
</script>
protected void Save_Click(object sender, EventArgs e)
{
// your save code
ScriptManager.RegisterStartupScript(this, GetType(), "message", "ParentRefresh();", true);
}
Wednesday, 10 December 2014
Regular Expression for Date Format (dd/mm/yyyy) and (mm/yyyy)
Regular Expression for Date Format (dd/mm/yyyy):
(^(((0[1-9]|[12][0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)
Regular Expression for Date Format (mm/yyyy):
^((((((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$
Add a RequiredFieldValidator to DropDownList control
Add a RequiredFieldValidator to DropDownList control
ddl1.Items.Insert(0, "Please select");
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="your-dropdownlist"
InitialValue="Please select" ErrorMessage="Please select something" />
Friday, 5 December 2014
If a folder does not exist, create it in C#
If a folder does not exist, create it in C#
string subPath ="ImagesPath"; // your code goes here
bool exists = System.IO.Directory.Exists(Server.MapPath(subPath));
if(!exists)
System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
Tuesday, 18 November 2014
Difference between Array and Arraylist in C# -.Net
|
Array
|
Array List
|
|
|
|
|
Arrays are strongly typed collection of
same datatype
|
Array lists are not strongly type
collection.
|
|
It will store values of same datatype
|
It will store values of different datatypes
or same datatype
|
|
these arrays are fixed length that cannot
be changed during runtime.
|
Array list size will increase or decrease
dynamically it can take any size of values from any data type.
|
|
Arrays belong to System.Array namespace
|
Array
lists will be accessible with “System.Collections” namespace
|
|
string[] arr=new string[2];
arr[0]
= "text1";
arr[1]
= "text2 ";
|
ArrayList strarr = new ArrayList();
strarr.Add("text"); // Add string values
strarr.Add(10); // Add integer values
strarr.Add(10.05); // Add float values
|
ASP.NET vNext
ASP.NET vNext
- 1. ASP.NET vNext includes new cloud-optimized versions of MVC, Web API, Web Pages, SignalR, and Entity Framework.
- 2. MVC, Web API and Web Pages have been merged into one framework, called MVC 6. This will follow common programming approach between all these three i.e. a single programming model for Web sites and services.
- For example, there is unified controller, routing concepts, action selection, filters, model binding, and so on. In this way, You will have a single controller that returns both MVC views and formatted Web API responses, on the same HTTP verb.
- 3. MVC 6 has no dependency on System.Web since it was quite expensive. A typical HttpContext object graph can consume 30K of memory per request and working with small JSON-style requests this is very costly. With MVC 6 it is reduced to roughly 2K. The result is a leaner framework, with faster startup time and lower memory consumption.
- 4.
ASP.NET vNext has new project extension
project.jsonto list all the dependencies for the application and astartupclass in place of Global.asax. - 5. ASP.NET vNext apps are cloud ready by design. Services such as session state and caching will adjust their behavior depending on hosting environment either it is cloud or a traditional hosting environment. It uses dependency injection behind the scenes to provide your app with the correct implementation for these services for cloud or a traditional hosting environment. In this way, it will easy to move your app from on-premises to the cloud, since you need not to change your code.
- 6. .NET next version, .NET vNext is host agnostic. Hence you can host your ASP.NET vNEXT app in IIS, or self-host in a custom process.
Monday, 17 November 2014
What's the difference between String and string?
String
Stands forSystem.StringIt is a .NET Framework type
string
stringis an alias in the C# language forSystem.String
Difference Between Equality Operator ( ==) and Equals() Method in C#
Difference Between Equality Operator ( ==) and Equals() Method in C#
The Equality Operator ( ==) Equals() method
Comparison operator Compares the contents of a string
Compares the reference identity Compares only contents
MVC Tutorial Links
MVC Tutorials Links
http://www.codeproject.com/Articles/667369/ASP-NET-MVC-Learning-Roadmap
http://www.codeproject.com/Articles/667369/ASP-NET-MVC-Learning-Roadmap
Sunday, 16 November 2014
Calling A Button OnClick from a function
Suppose you have a function test() and button. You want to call button click event from function automatically, you can do as:
private void ExtractedMethod()
{
//some list of code
Button_Click(null, EventArgs.Empty);
}
protected void Button_Click(object sender, EventArgs e)
{
//some list of code }
Sunday, 9 November 2014
Update data row of dataset /datatable
If you want to update the row based on column value of dataset / datatable , you will do as:
Suppose you have a dataset named dataset1 having table with name TableName". Table have 2 column named ColumnName1, ColumnName2, ColumnName3.
You want to update Row having ColumnName1='Value1'
Suppose you have a dataset named dataset1 having table with name TableName". Table have 2 column named ColumnName1, ColumnName2, ColumnName3.
You want to update Row having ColumnName1='Value1'
DataRow[] Row = dataSet1.Tables["TableName"].Select("ColumnName1= 'Value1'"); Row [0]["ColumnName2"] = "Value2"; Row [0]["ColumnName3"] = "Value3";
Tuesday, 4 November 2014
Refresh parent window on close of child window in asp.net
Code to refresh parent window on close of child window.
Put the below code in <head> tag of aspx page of child window.
<script type="text/jscript">
window.onunload = refreshParent;
function refreshParent() {
var loc = window.opener.location;
window.opener.location = loc;
}
</script>
Put the below code in <head> tag of aspx page of child window.
<script type="text/jscript">
window.onunload = refreshParent;
function refreshParent() {
var loc = window.opener.location;
window.opener.location = loc;
}
</script>
Close the Page on a Button Click asp.net c#.
If you want to close the window after executing code behind code.
string closeWindowScript = "<script language=javascript>window.top.close();</script>"; if ((!ClientScript.IsStartupScriptRegistered("clientScript"))) { ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", closeWindowScript); }
open new browser window on button click event in asp.net
Suppose you want to open a window from code behind on button click or other event in asp.net, you just following code as per your requirement.
protected void button_Click(object sender, EventArgs e)
{
// open a pop up window at the center of the page.
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2); window.open( 'your_page.aspx', null, 'height=700,width=760, status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no, top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
}
Thursday, 18 September 2014
Disable cut, copy & paste using JQuery
- <script type="text/javascript">
- $(document).ready(function() {
- $('#TextBox1').bind('copy paste cut',function(e) {
- e.preventDefault(); //disable cut,copy,paste
- alert('cut,copy & paste options are disabled !!');
- });
- });
- </script>
- <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
Disable cut, copy & paste using Javascript
<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" onpaste="return false" oncut="return false"></asp:TextBox>
- <script language="javascript" type="text/javascript">
- function DisableCopyPaste (e)
- {
- // Message to display
- var message = "Cntrl key/ Right Click Option disabled";
- // check mouse right click or Ctrl key press
- var kCode = event.keyCode || e.charCode;
- //FF and Safari use e.charCode, while IE use e.keyCode
- if (kCode == 17 || kCode == 2)
- {
- alert(message);
- return false;
- }
- }
- </script>
- <asp:TextBox ID="TextBox1" runat="server" onKeyDown="return DisableCopyPaste(event)"onMouseDown="return DisableCopyPaste (event)"></asp:TextBox>
Subscribe to:
Posts (Atom)