Cannot access attributes control of servercontrol page calendar in default.aspx

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dipti Dhiman
    New Member
    • Jan 2015
    • 6

    Cannot access attributes control of servercontrol page calendar in default.aspx

    I cannot access attributes control of server control page calendar in default.aspx

    Dipti_Calendar. cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    
    namespace Dipti_Calendar
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:Dipti_Calendar runat=server></{0}:Dipti_Calendar>")]
        public class Dipti_Calendar : Calendar
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null)? "[" + this.ID + "]" : s);
                }
     
                set
                {
                    ViewState["Text"] = value;
                }
            }
    
            public Color h_color
            {
                get
                {
                    // look for h_color in ViewState
                    object o = ViewState["h_color"];
                    if (o == null)
                        return Color.Empty;
                    else
                        return (Color)o;
                }
                set
                {
                    ViewState["h_color"] = value;
                }
            }
    
            protected override void OnLoad(EventArgs e)
            {
                base.PrevMonthText = "Prev";
                base.NextMonthText = "Next";
                base.SelectedDate = System.DateTime.Now;           
                base.OnLoad(e);
            }
            protected override void OnDayRender(TableCell c, CalendarDay d)
            {
                HtmlGenericControl div = new HtmlGenericControl("DIV");
                Label lb = new Label();
                lb.Text = d.Date.ToString("dd");
                lb.ForeColor = System.Drawing.Color.ForestGreen;
                string month = base.SelectedDate.ToString("MM");
                int year=base.SelectedDate.Year;
    
                TextBox tb = new TextBox();
                tb.ID = "tb" + "-" + d.Date.ToString("dd") + "-" + d.Date.ToString("MM") + "-" + d.Date.ToString("yyyy");           
                tb.CssClass = "hoteldetail_cont";            
                tb.Attributes.Add("runat", "server");
    
                if (year.ToString() != d.Date.ToString("yyyy") && month.ToString() != d.Date.ToString("MM"))
                {
                    tb.Attributes.Add("readonly", "true");
                    tb.Attributes.Add("style", "background-color:#999999");
                }         
                div.Controls.Add(lb);
                div.Controls.Add(tb);
    
                c.Attributes.Add("Class", "col-sm-2 dates_cont");
                c.Controls.Add(div);
                base.OnSelectionChanged();
                base.OnDayRender(c, d);           
            }
            
     
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
        }
    }
    Default.aspx
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
     <%@ Register Assembly="Dipti_Calendar" TagPrefix="DiptiCalendar" Namespace="Dipti_Calendar"  %>
     <!DOCTYPE html>
     <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
     <meta name="author" content=""> 
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head runat="server"> 
    <title></title> 
    <link href="http://bytes.com/css/bootstrap.min.css" rel="stylesheet" /> 
    <link href="http://bytes.com/css/bootstrap.css" rel="stylesheet" />
     <link href="http://bytes.com/css/calendar.css" rel="stylesheet" />
    
     <script src="http://bytes.com/js/jquery-1.11.0.js" type="text/javascript"></script> 
    <script src="http://bytes.com/js/bootstrap.min.js" type="text/javascript"></script>
     </head>
    
     <body>
     <form id="form1" runat="server">
     <div class="container">
    [B] <DiptiCalendar:Dipti_Calendar ID="DC" runat="server" OnDayRender="DC_DayRender" TitleStyle-CssClass="month_cont" DayHeaderStyle-CssClass="weekname_cont" > 
    </DiptiCalendar:Dipti_Calendar> [/B]
    </div>
    
     <div class="btnsave"> 
    <asp:ImageButton ID="btnsave" runat="server"  ImageUrl="http://bytes.com/~/Images/sale-save.png" Width="80px" OnClick="btnsave_Click" />
     </div> 
    <asp:Label ID="lbldate" runat="server" Text="1"></asp:Label>
     <asp:Label ID="lblcelldata" runat="server" Text="2"></asp:Label>
     </form>
     </body>
     </html>
    Default.aspx.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Data;
    using Dipti_Calendar;
    //using System.Web.UI.ControlCollection;
    
    public partial class _Default : System.Web.UI.Page
    {
        Dipti_Calendar.Dipti_Calendar dcal = new Dipti_Calendar.Dipti_Calendar();
    
        protected void Page_Load(object sender, EventArgs e)
        {        
        }
    
        protected void DC_DayRender(object sender, DayRenderEventArgs e)
        {
           
              
        }
        protected void btnsave_Click(object sender, ImageClickEventArgs e)
        {
        }
       
    }
    Last edited by Frinavale; Jan 20 '15, 03:25 PM. Reason: Added the question/problem posted in the thread's title to the body of the thread to give context to the pure code posted. And then split the different code files into their own code blocks.
  • Dipti Dhiman
    New Member
    • Jan 2015
    • 6

    #2
    Dont know how to access lb and tb control in default.aspx.cs ... :-(

    on save button click, i want to access data of all textbox for a given month and to save it into database

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Huh?
      You have a Dipti_Calendar defined in your ASP.NET code for the Default.aspx page:
      Code:
      <DiptiCalendar:Dipti_Calendar ID="DC" runat="server" OnDayRender="DC_DayRender" TitleStyle-CssClass="month_cont" DayHeaderStyle-CssClass="weekname_cont" > 
      </DiptiCalendar:Dipti_Calendar>
      But I don't see you using that control in your C#, server-side code....

      Have you tried something like:
      Code:
      protected void Page_Load(object sender, EventArgs e)
      {
        DC.h_color = Color.Pink;
      }
      I really don't understand where the problem is...

      -Frinny

      Comment

      • Dipti Dhiman
        New Member
        • Jan 2015
        • 6

        #4
        I m getting the control of lb and tb in DC_DayRender function but i want to access the same on btnsave_Click.. ..
        Here is my Code
        Code:
            protected void DC_DayRender(object sender, DayRenderEventArgs e)
            {
                Label x = (Label)DC.Labelx;
                var lbltext = DC.lb.Text;
        
                TextBox t = (TextBox)DC.tb;
                var textboxid = DC.tb.ID;
                var textboxdata = DC.tb.Text;
                
              
            }
            protected void btnsave_Click(object sender, ImageClickEventArgs e)
            {
                string x=DC.lb.ID.ToString();         
            }
        and what i want to do is :
        1.Here is my stored procedure
        Code:
        ALTER PROCEDURE antilogin.calendarinsert
        	@date nvarchar(50),
        	@value int
        AS
        	
        	insert into Calendar(Cal_date,Cal_value)
        	 values(@date,@value)
        
        	 select * from Calendar
        	RETURN
        2.I want to check each date(to store as Cal_Date) and respected value stored in textbox(to store as Cal_value),of Calendar, on btnsave_Click..
        as
        Code:
         protected void btnsave_Click(object sender, ImageClickEventArgs e)
            {
                string x=DC.lb.ID.ToString();     
          [I][B]  //  lbldate.Text= [/B][/I]
                [B][I]// lblcelldata.Text=[/I][/B]
                //dbclass db = new dbclass();
                //db.cmd.Connection = db.conr;
                //db.cmd.CommandType = CommandType.StoredProcedure;
                //db.cmd.CommandText = "calendarinsert";
                ////db.cmd.Parameters.Add("@date", SqlDbType.NVarChar).Value = lbldate.Text;
                ////db.cmd.Parameters.Add("@value", SqlDbType.NVarChar).Value = lblcelldata.Text;
                //db.da.SelectCommand = db.cmd;
                //try
                //{
                //    db.con.Open();
                //    db.cmd.ExecuteNonQuery();
                //}
        
                //catch
                //{
        
                //}
        
                //finally
                //{
                //    db.con.Close();
                //}
            }
        Last edited by Frinavale; Jan 21 '15, 06:39 PM. Reason: Added code tags.

        Comment

        • Dipti Dhiman
          New Member
          • Jan 2015
          • 6

          #5
          Also i perform some changes in Dipti_Calendar. cs to access controls as:
          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          using System.Web;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.HtmlControls;
          using System.Drawing;
          
          namespace Dipti_Calendar
          {
              [DefaultProperty("Text")]
              [ToolboxData("<{0}:Dipti_Calendar runat=server></{0}:Dipti_Calendar>")]
              public class Dipti_Calendar : Calendar
              {
                 [B] public TextBox tb = new TextBox();
                  public Label lb = new Label();      [/B]
                  
          [Bindable(true)]
                  [Category("Appearance")]
                  [DefaultValue("")]
                  [Description("Description for text")]
                  [Localizable(true)]
                 
                  public string TextBoxx
                  {
                      get
                      {
                          String s = (String)ViewState["Text"];
                          return ((s == null) ? "[" + this.ID + "]" : s);
                          //object tbx = ViewState["TextBoxx"];
                          //return (Control)tbx;
                      }
                      set
                      {
                          ViewState["TextBoxx"] = value;
                      }
                  }
                  [
                  Bindable(true),
                  Category("Appearance"),
                  DefaultValue(""),
                  Description("The text for the name label.")
                  ]
                  public Object Labelx
                  {
                      get
                      {                
                         // object lb = ViewState["Label"];
                          return (Object)lb;
                          //return lb.Text;
                      }
          
                      set
                      {
                          ViewState["Label"] =value;
                      }
                  }
          
                  public Color h_color
                  {
                      get
                      {
                          // look for h_color in ViewState
                          object o = ViewState["h_color"];
                          if (o == null)
                              return Color.Empty;
                          else
                              return (Color)o;
                      }
                      set
                      {
                          ViewState["h_color"] = value;
                      }
                  }
          
                  protected override void OnLoad(EventArgs e)
                  {
                      base.PrevMonthText = "Prev";
                      base.NextMonthText = "Next";
                      base.SelectedDate = System.DateTime.Now;
                      base.OnLoad(e);
                  }
                 
                  protected override void OnDayRender(TableCell c, CalendarDay d)
                  {
                      HtmlGenericControl div = new HtmlGenericControl("DIV");       
                      lb.Text = d.Date.ToString("dd");
                      lb.ID = "lb-" + d.Date.ToString("dd") + "-" + d.Date.ToString("MM") + "-" + d.Date.ToString("yyyy");
                      lb.ForeColor = System.Drawing.Color.ForestGreen;
                      string month = base.SelectedDate.ToString("MM");
                      int year=base.SelectedDate.Year;
                     
                      tb.ID = "tb" + "-" + d.Date.ToString("dd") + "-" + d.Date.ToString("MM") + "-" + d.Date.ToString("yyyy");           
                      tb.CssClass = "hoteldetail_cont";            
                      tb.Attributes.Add("runat", "server");            
          
                       
                      div.Controls.Add(lb);
                      div.Controls.Add(tb);
          
                      c.Attributes.Add("Class", "col-sm-2 dates_cont");
                      c.Controls.Add(div);
                      
                      base.OnSelectionChanged();            
                      base.OnDayRender(c, d);           
                  }
          
                  
                  
          
                  
           
                  protected override void RenderContents(HtmlTextWriter output)
                  {
                      output.Write(TextBoxx);
                      output.Write(Labelx);
                  }
              }
          }
          Last edited by Frinavale; Jan 21 '15, 06:41 PM. Reason: Added code tags.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            You need to use using public properties in your Dipti_Calendar to expose things that you require in your ASPX page to do what you need to do.

            For example, your Dipti_Calendar control should contain a public "Date" property so that you can access the selected date in the Button Click event code that is part of your ASPX page....this way you can call your stored procedure and pass it the appropriate value as the date parameter. You will also need to have a "CalendarVa lue" property that will give you access to the information that should be provided to your stored procedure as the value parameter.

            As for your second question....

            You should probably not be adding controls dynamically to your page unless you fully understand what is entailed with this process.

            Instead you should just open the Dipti_Calendar. aspx file and add the controls where every you want them to be.

            -Frinny

            Comment

            Working...