Mega Code Archive

 
Categories / ASP.Net / Asp Control
 

Embed Javascript to C# code to create a popup window

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">     <title>Select Date</title>     <script language="javascript">         function SetDate(dateToSet)         {             controlName = window.location.search.substr(1).substring(8);             window.opener.document.forms[0].elements[controlName].value = dateToSet;             self.close();         }     </script> </head> <body>     <form id="form1" runat="server">     <div>     <asp:Calendar ID="calDefault"                    runat="server"                    BackColor="White"                    BorderColor="#999999"                    CellPadding="4"                    DayNameFormat="Shortest"                    Font-Names="Verdana"                    Font-Size="8pt"                    ForeColor="Black"                    Height="100%"                    Width="100%"                    OnDayRender="calDefault_DayRender" >         <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />         <SelectorStyle BackColor="#CCCCCC" />         <WeekendDayStyle BackColor="#FFFFCC" />         <OtherMonthDayStyle ForeColor="#808080" />         <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />         <NextPrevStyle VerticalAlign="Bottom" />         <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />         <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />     </asp:Calendar>     </div>     </form> </body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.Page {   protected void calDefault_DayRender(object sender, DayRenderEventArgs e)   {     HyperLink link = new HyperLink();     LiteralControl lc = (LiteralControl)e.Cell.Controls[0];     link.Text = lc.Text;     link.NavigateUrl = "javascript:SetDate('" + e.Day.Date.ToShortDateString() + "');";     e.Cell.Controls.Clear();     e.Cell.Controls.Add(link);   } }