Translate

Sunday, May 22, 2011

How to pass parameters from a page to a popup window and back (pass parameter popup window javascript)


Using window.showModalDialog

To send parameters to the child page, use query string.
If the window is opened using window.showModalDialog (instead of window,open), then parameters can be sent back to the parent page using window.returnValue.


Parent page javascript (send values using query string) :


url = "/myChild?param1=hello&param2=28";

var x = window.showModalDialog(url);

if (x) {
    alert(x.Age);
}
else {
  alert('no return data');
}


[Note: This will not work with window.open]
Child page C# code (to accept query string parameters):


string param1 = Request.QueryString[" param1"].ToString();
int param2 = int.Parse(Request.QueryString[" param2"]);




Child page javascript that returns the values:


var x = new Object();
x.name = 'Sam';
x.Age = 37;

window.returnValue = x;
window.close();



Using window.open

If you are using window.open(url), in that case write a javascript function in the PARENT page that you want to call from the child page. Suppose the function is called Hello. This is how you will invoke it from child page.

window.opener.Hello();




Sunday, May 8, 2011

Datetime ToString C#

The example below shows the different ways, to format datetime into a string in C#


using System;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            string s;

            DateTime dt = System.DateTime.Now;
            Console.WriteLine(dt);//Displays 5/8/2011 8:50:40 AM

            s = dt.ToString("d");
            Console.WriteLine(s);//Displays 5/8/2011

            s = dt.ToString("MM-dd-yyyy");
            Console.WriteLine(s);//Displays 05-08-2011

            s = dt.ToString("MM/dd/yy"); //month date year
            Console.WriteLine(s);//Displays 05/08/11

            s = dt.ToString("MM-dd-yy");
            Console.WriteLine(s);//Displays 05-08-11

            s = dt.ToString("dd/MM/yy");//date moth year
            Console.WriteLine(s);//Displays 08/05/11

            s = dt.ToString("dd-MM-yy");
            Console.WriteLine(s);//Displays 08-05-11

            s = dt.ToString("dd");
            Console.WriteLine(s);//Displays 08

            s = dt.ToString("MM");
            Console.WriteLine(s);//Displays 05

            s = dt.ToString("yy");
            Console.WriteLine(s);//Displays 11

            s = dt.ToString("yyyy");
            Console.WriteLine(s);//Displays 2011

            s = dt.ToString("m");
            Console.WriteLine(s);//Displays May 08

            s = dt.ToString("y");
            Console.WriteLine(s);//Displays May, 2011



            //Do this to get the month name
            char comma = ',';
            s = dt.ToString("y").Split(comma)[0];
            Console.WriteLine(s);//Displays May


            //Do this to get the day name
            s = dt.ToString("dddd");
            Console.WriteLine(s);//Displays Sunday



            s = dt.ToString("mm");
            Console.WriteLine(s);//Displays 50 (minutes)

            s = dt.ToString("ss");
            Console.WriteLine(s);//Displays 40 (seconds)

        }
    }
}

Sunday, May 1, 2011

object sender, EventArgs e

The event handlers for most .net generated events meet the definition of the kind


protected void EventName(object sender, EventArgs e)
 {
          

}


sender is a reference to the object that raised the event

On a button click if you check the value of sender.GetType() , you would see something like
Web.UI.WebControls.Button

e contains the parameters passed by the event

For instance if you are consuming a user created custom event, you could have parameters like
e.pageNumber, e.pageSize etc.

asp.net user control raise event parent form


Before I get into this topic, lets look at a few definitions

Event: An event in C# is an occurrence. For example if you click a button, that is a button click event.

Subscribing to an Event: Stating which method needs to be called when an event occurs

Event Handler: The method that will be executed when an event occurs (or raised).

Delegate: Delegate is a function pointer. Check this out for a discussion on delegates.


How to subscribe to an event

If you want to subscribe to the SelectedIndexChanged event of a dropdown (ddlDept) in the code behind, this is the syntax to do it

ddlDept.SelectedIndexChanged+=new EventHandler(ddlDept_SelectedIndexChanged);

In the line of code above:

Code
What it means

ddlDept.SelectedIndexChanged
The event that you are subscribing to
EventHandler
A delegate that is defined in the .net framework
ddlDept_SelectedIndexChange
The name of the method  (or function) you want to call when the event occurs


This is the general syntax for consuming any event

EventName+=new DelegateName(MethodName)

The publisher defines the delegate and the event. The subscriber, defines the event handler (a method).

How to create and raise an event and consume it somewhere else

Suppose you have a user control. In this user control you want to raise an event on a button click,  which you want to handle in the page containing the user control. This is a way to do it.


This is your code in the usercontrol ascx (Publisher)


<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl.ascx.cs" Inherits="MyUserControl" %>


<asp:Button ID="Button1" runat="server" Text="ClickMe"
    onclick="Button1_Click"  />


The code behind for the user control


using System;
using System.Web;


public partial class MyUserControl : System.Web.UI.UserControl
{
    //Define the delagate (The method handling this event would need to have the same signature)
    public delegate void myDelegate(string s);

    //Define the event that needs to be raised
    public event myDelegate myEvent;

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        if (myEvent!=null)
       {
           string s = "Hello from usercontrol!";

           //The event myEvent is raised in the next line
           myEvent.Invoke(s);
       }
    }
}


The aspx page that holds user control (Subscriber)

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/MyUserControl.ascx" TagPrefix="UserControl" TagName="EventEg" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Event example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <UserControl:EventEg id="ucEventEg" runat="server" />
    </div>
    </form>
</body>
</html>



The code behind for the aspx

 using System;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //Subscribe to the user control's event named myEvent
        ucEventEg.myEvent += new MyUserControl.myDelegate(UserCortolButtonClicked);
     

    }

    private void UserCortolButtonClicked(string s)
    {
        Response.Write(s);
    }
}




When you run the web application, this is how the page would look like




















When you click the button this is what happens