Avatar of CharlieDev
CharlieDevFlag for United Kingdom of Great Britain and Northern Ireland asked on

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

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>

Open in new window

ASP.NET

Avatar of undefined
Last Comment
CharlieDev

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
naspinski

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
CharlieDev

Thanks !! I am getting an error when I put "  ((LargeCalendarViewControl)Parent.FindControl
           ("LargeCalendarViewControl")).Attributes["date"] = CalendarSelectedDate;   " at the end of mypageload on the smallcalendarcontrol

error " The type or namespace name 'LargeCalendarViewControl' could not be found (are you missing a using directive or an assembly reference?)"

do I need a new namespace?
other than the following that I have:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
nmarun

Use QueryString parameters. This way you'll pass the calendar date through the address bar and when the new control is being loaded, it'll check to see if there is a date parameter and load it accordingly.
naspinski

QueryStrings might be easier, but if you are using postbacks, that wont work.

You need to figure out what the Object Name is for your controls.

In your code view(.cs file), type out the id of one of your controls, it should show up in intellisense.  Then, hover over it, it should tell you the name of the object.

For example, if you hover over an integer, it will say 'Struct System.Int32' so you know it is an integer
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
CharlieDev

I dont know an id of my control!!! Sorry, I'm not making it easy for you am I!!   What are the ids for my controls?
Thanks
ASKER
CharlieDev

Thanks