Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Passing data from one web user control to another in asp.net c#

Avatar of CharlieDev
CharlieDevFlag for United Kingdom of Great Britain and Northern Ireland asked on
ASP.NET
6 Comments1 Solution1433 ViewsLast Modified:
Hi,
I am trying to pass the value of a selected droplist thats selected on one control to another control.
I want to pass CalendarSelectedDate, this variable changes three times on the page. Once on page load when it is set to the dateTimeNow, later on a click which activates method protected void Date_Selected(object sender, EventArgs e), and again on a button click which activates method public void DropDwnDateChanged(object sender, EventArgs e). For each of these i want the CalendarSelectedDate to be passed to my second control ( LargeCalenderViewControl)
smallCalendarView.ascx.cs
*******************************************
 
public partial class smallCalendarView : System.Web.UI.UserControl
{
    private DateTime CalendarSelectedDate;
    controls_user_smallCalendarView scv = (controls_user_smallCalendarView)Parent.FindControl("smallCalendarView");
    scv.Attributes.Add("CalendarSelectedDate", "new_value");
 
   /* public event PassSelectedValues DatePassed;*/
    public delegate void PassSelectedValues(DateTime[] DateSelected);
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CalendarSelectedDate = DateTime.Now;
            DateTimeFormatInfo dfi = new DateTimeFormatInfo();
            dfi.MonthDayPattern = "dd MMMM yyyy";
 
            DateTime now = DateTime.Now;
 
            string dateNow = now.ToString("m", dfi);
 
            string strDay = dateNow.Split(' ')[0];
            string strMonth = dateNow.Split(' ')[1];
            string strYear = dateNow.Split(' ')[2];
 
            string dateID = strMonth + " " + strYear;
            string todayDate = strDay;
 
            Day.Text = strDay;
            Month.Text = strMonth;
            Year.Text = strYear;
            DateSelected = CalendarSelectedDate;
 
        }
    }
 
    protected void calEvents_DayRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
        DateTime DayOfWeekPassed = CalendarSelectedDate;
        HighlightWeek(DayOfWeekPassed, e);
        
    }
    protected void Date_Selected(object sender, EventArgs e)
    {
        CalendarSelectedDate = Calendar1.SelectedDate;
        DateTimeFormatInfo dfi = new DateTimeFormatInfo();
        dfi.MonthDayPattern = "MMMM yy";
        DateTimeFormatInfo dtf = new DateTimeFormatInfo();
        dtf.MonthDayPattern = "dd";
 
        DateTime CalendarSelectedYear = Calendar1.SelectedDate;
        DateTime NewCalendarSelectedDate = Calendar1.SelectedDate;
 
        string convertedSelectedYear = CalendarSelectedYear.ToString("yyyy", dfi);
        string convertedSelectedMonth = NewCalendarSelectedDate.ToString("MM", dfi);
        string convertedSelectedDate = NewCalendarSelectedDate.ToString("dd", dtf);
 
        string splitdateday = convertedSelectedDate;
        string splitdatemonth = convertedSelectedMonth;
        string splitdateyear = convertedSelectedYear;
        
 
 
        Day.Text = convertedSelectedDate;
        Month.Text = splitdatemonth;
        Year.Text = splitdateyear;
 
        DateSelected = CalendarSelectedDate;
 
    }
 
    public void HighlightWeek(DateTime DayOfWeekPassed, System.Web.UI.WebControls.DayRenderEventArgs e)
    {
        string[] weekdayList = DateFunctions.GetDate(DayOfWeekPassed);
 
        string MondayWeekDay = weekdayList[0];
        string SundayWeekDay = weekdayList[6];
        DateTime MondayDate = Convert.ToDateTime(MondayWeekDay);
        DateTime SundayDate = Convert.ToDateTime(SundayWeekDay);
 
        DateTime dateInCalendar = e.Day.Date;
 
        if (dateInCalendar == MondayDate || (dateInCalendar > MondayDate && dateInCalendar < SundayDate) || dateInCalendar == SundayDate)
        {
            e.Cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#eeeeee");
            e.Cell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#999999");
        }
 
      
    }
 
    public void DropDwnDateChanged(object sender, EventArgs e)
    {
        string stringDay = Day.SelectedValue;
        string stringMonth = Month.SelectedValue;
        string stringYear = Year.SelectedValue;
 
        String StringNewSelectedDate = stringDay + '/' + stringMonth + '/' + stringYear;
 
        DateTime NewSelectedDate = Convert.ToDateTime(StringNewSelectedDate);
 
        CalendarSelectedDate = NewSelectedDate;
        Calendar1.VisibleDate = CalendarSelectedDate;
        DateSelected = CalendarSelectedDate;
 
    }
 
     
 
 
 
}
*********************************************
smallCalendarView.ascx
**********************************************
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="smallCalendarView.ascx.cs" Inherits="smallCalendarView" %>
 
 
 <div>
        <asp:DropDownList ID="Day" runat="server">
             <asp:ListItem Value="01" Text="01"  />
                          <asp:ListItem Value="02" Text="02"  />
                          <asp:ListItem Value="03" Text="03" />
                          <asp:ListItem Value="04" Text="04"  />
                          <asp:ListItem Value="05" Text="05"  />
                          <asp:ListItem Value="06" Text="06" />
                          <asp:ListItem Value="07" Text="07"  />
                          <asp:ListItem Value="08" Text="08" />
                          <asp:ListItem Value="09" Text="09" />
                          <asp:ListItem Value="10" Text="10" />
                          <asp:ListItem Value="11" Text="11" />
                          <asp:ListItem Value="12" Text="12"/>
                          <asp:ListItem Value="13" Text="13"  />
                          <asp:ListItem Value="14" Text="14"  />
                          <asp:ListItem Value="15" Text="15" />
                          <asp:ListItem Value="16" Text="16"  />
                          <asp:ListItem Value="17" Text="17"  />
                          <asp:ListItem Value="18" Text="18" />
                          <asp:ListItem Value="19" Text="19"  />
                          <asp:ListItem Value="20" Text="20" />
                          <asp:ListItem Value="21" Text="21" />
                          <asp:ListItem Value="22" Text="22" />
                          <asp:ListItem Value="23" Text="23" />
                          <asp:ListItem Value="24" Text="24"/>
                          <asp:ListItem Value="25" Text="25"  />
                          <asp:ListItem Value="26" Text="26"  />
                          <asp:ListItem Value="27" Text="27" />
                          <asp:ListItem Value="28" Text="28"  />
                          <asp:ListItem Value="29" Text="29"  />
                          <asp:ListItem Value="30" Text="30" />
                          <asp:ListItem Value="31" Text="31"  />
        </asp:DropDownList>
        <asp:DropDownList ID="Month" runat="server">
           
                           <asp:ListItem Value="01" Text="January"  />
                           <asp:ListItem Value="02" Text="February"  />
                           <asp:ListItem Value="03" Text="March" />
                           <asp:ListItem Value="04" Text="April"  />
                           <asp:ListItem Value="05" Text="May"  />
                           <asp:ListItem Value="06" Text="June" />
                           <asp:ListItem Value="07" Text="July"  />
                           <asp:ListItem Value="08" Text="August" />
                           <asp:ListItem Value="09" Text="September" />
                           <asp:ListItem Value="10" Text="October" />
                           <asp:ListItem Value="11" Text="November" />
                           <asp:ListItem Value="12" Text="December"/>
        </asp:DropDownList>
        <asp:DropDownList ID="Year" runat="server">
                           <asp:ListItem Value="2008" Text="2008" />
                           <asp:ListItem Value="2009" Text="2009" />
                           <asp:ListItem Value="2010" Text="2010" />
                           <asp:ListItem Value="2011" Text="2011"/>
        </asp:DropDownList>
        <asp:Button ID="DateChangeButton" runat="server" Text="GO" OnClick="DropDwnDateChanged"/>
        
         <asp:Calendar id="Calendar1" CssClass="fixturesCalendar" 
                             OnSelectionChanged="Date_Selected"
                             OnDayRender="calEvents_DayRender"
                             NextPrevFormat="CustomText"
                             NextMonthText="<img src='../images/arrowR.gif' />"
                             PrevMonthText="<img src='../images/arrowL.gif' />"
                             DayStyle-CssClass="dayItem"
                             BackColor ="White"
                             SelectedDayStyle-CssClass="none"
                             SelectedDayStyle-BackColor="White"
                             SelectedDayStyle-ForeColor="Black"
                             runat="server">
                           <TodayDayStyle />
                
                           <DayHeaderStyle Font-Bold="True"/>
                           <OtherMonthDayStyle ForeColor="gray"/>
                           <TitleStyle BackColor="#ffffff"
                                       ForeColor="#000"
                                       Font-Bold="True"
                                       />
 
                           <NextPrevStyle ForeColor="#000"
                                          Font-Size="12px"/>
                           <SelectorStyle BackColor="#336633" 
                                          ForeColor="navy"
                                          Font-Size="9px"/>
           </asp:Calendar>
                  
    </div>
********************************************************
LargeCalendarViewControl.ascx.cs
********************************************************
public partial class LargeCalendarViewControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // create empty dataset 
        DataSet ds = new DataSet();
        ds.Tables.Add("CalendarTable");
 
        DataColumn column = new DataColumn("Year");
        ds.Tables[0].Columns.Add(column);
 
        // for each Day in week create a new column
        string stringdate = "05/12/2008";
        DateTime DayOfWeekPassed = Convert.ToDateTime(stringdate);
        string[] weekdayList = DateFunctions.GetDate(DayOfWeekPassed);
        string ThursdayDate = weekdayList[3];
 
        for (int k = 0; k <= 4; k++) //for each day of week
        {
            // create the headings for the columns
            DataColumn Daycolumn = new DataColumn(weekdayList[k]);
            ds.Tables[0].Columns.Add(Daycolumn);
        }
 
        // create three rows in the table
        int[] blankArray = new int[ds.Tables[0].Columns.Count];
        for (int i = 0; i < blankArray.Length; i++)
        {
            blankArray[i] = 0;
        }
 
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
        ds.Tables[0].Rows.Add(blankArray);
 
        ds.Tables[0].Rows[0][0] = "9AM";
        ds.Tables[0].Rows[1][0] = "10AM";
        ds.Tables[0].Rows[2][0] = "11AM";
        ds.Tables[0].Rows[3][0] = "12AM";
        ds.Tables[0].Rows[4][0] = "1PM";
        ds.Tables[0].Rows[5][0] = "2PM";
        ds.Tables[0].Rows[6][0] = "3PM";
        ds.Tables[0].Rows[7][0] = "4PM";
        ds.Tables[0].Rows[8][0] = "5PM";
        ds.Tables[0].Rows[9][0] = "6PM";
        ds.Tables[0].Rows[10][0] = "7PM";
 
 
        gvwCalendarView.DataSource = ds;
 
        gvwCalendarView.DataBind();
    }
 
 
 
********************************************************
LargeCalendarViewControl.ascx
********************************************************
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LargeCalendarViewControl.ascx.cs" Inherits="LargeCalendarViewControl" %>
   <asp:GridView ID="gvwCalendarView" runat="server" AutoGenerateColumns="True" >
                    
       </asp:GridView>
ASKER CERTIFIED SOLUTION
Avatar of naspinski
naspinskiFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers