Jeg var nød til at finde en anden løsning til mit projekt, men jeg skal finde ud af det her, koste hvad det koste vil. Så jeg har skåret ned, så kun det vigtige er med, så håber jeg en eller anden kan hjælpe på vej.
Problemet igen:
På siden er der et DetailsView og en kalender. Når jeg klikker på Ny eller Rediger, skal en kalender magen til vises i mit DetailsView.
Jeg har uden held forsøgt med at adressere ned igennem siden og endda igennem master siden med FindControl, men alle forsøg giver samme fejl som tidligere; System.NullReferenceException: Objektreferencen er ikke indstillet til en forekomst af et objekt.
Jeg har to filer med tilhørende codebehind: MasterPage.master og Default.aspx, samt en simpel database.
*****MasterPage.master:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
<asp:Label ID="LabelMasterDate" runat="server" Text="Label" Width="390px"></asp:Label>
</div>
</form>
</body>
</html>
*****MasterPage.master.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 MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
*****Default.aspx:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="CalendarTest" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
**************************************************
Udvidet kalender
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4"
DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
Height="50px" Width="125px">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Navn" HeaderText="Navn" SortExpression="Navn" />
<asp:TemplateField HeaderText="Tid" SortExpression="Tid">
<EditItemTemplate>
<asp:DropDownList ID="DropDownListCalMonth" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalMonth_SelectedIndexChanged"
Width="101px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownListCalYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalYear_SelectedIndexChanged"
Width="104px">
</asp:DropDownList> <br />
<br />
<asp:Calendar ID="CalendarSource" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" OnSelectionChanged="CalendarSource_SelectionChanged"
SelectedDate='<%# Bind("Tid") %>'>
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownListCalMonth" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalMonth_SelectedIndexChanged"
Width="101px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownListCalYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalYear_SelectedIndexChanged"
Width="104px">
</asp:DropDownList> <br />
<br />
<asp:Calendar ID="CalendarSource" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" OnSelectionChanged="CalendarSource_SelectionChanged"
SelectedDate='<%# Bind("Tid") %>'>
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Tid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Table1] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Table1] ([Navn], [Tid]) VALUES (@Navn, @Tid)"
SelectCommand="SELECT [ID], [Navn], [Tid] FROM [Table1]" UpdateCommand="UPDATE [Table1] SET [Navn] = @Navn, [Tid] = @Tid WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Navn" Type="String" />
<asp:Parameter Name="Tid" Type="DateTime" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Navn" Type="String" />
<asp:Parameter Name="Tid" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
<br />
**************************************************
Oprindelig kalender
<br />
<asp:DropDownList ID="DropDownListCalMonth" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalMonth_SelectedIndexChanged"
Width="101px">
</asp:DropDownList>
<asp:DropDownList ID="DropDownListCalYear" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListCalYear_SelectedIndexChanged"
Width="104px">
</asp:DropDownList> <br />
<br />
<asp:Calendar ID="CalendarSource" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66"
BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"
ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px" OnSelectionChanged="CalendarSource_SelectionChanged">
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
<SelectorStyle BackColor="#FFCC66" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
</asp:Calendar>
<br />
<asp:Label ID="LabelDate" runat="server" Text="Label" Width="390px"></asp:Label><br />
**************************************************
</asp:Content>
*****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 CalendarTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Hide the title of the calendar control
CalendarSource.ShowTitle = false;
//Calendar test = Master.FindControl("ContentPlaceHolder1").FindControl("DetailsView1").FindControl("CalendarSource") as Calendar;
//test.ShowTitle = false;
//Populate month and year dropdown list boxes which
//replace the original calendar title
if (!Page.IsPostBack)
{
Populate_MonthddList();
Populate_YearddList();
}
LabelDate.Text = "Current date: " + CalendarSource.TodaysDate;
}
private void Populate_MonthddList()
{
DropDownListCalMonth.Items.Add("Januar");
DropDownListCalMonth.Items.Add("Februar");
DropDownListCalMonth.Items.Add("Marts");
DropDownListCalMonth.Items.Add("April");
DropDownListCalMonth.Items.Add("Maj");
DropDownListCalMonth.Items.Add("Juni");
DropDownListCalMonth.Items.Add("Juli");
DropDownListCalMonth.Items.Add("August");
DropDownListCalMonth.Items.Add("September");
DropDownListCalMonth.Items.Add("Oktober");
DropDownListCalMonth.Items.Add("November");
DropDownListCalMonth.Items.Add("December");
// DropDownListCalMonth.Items.FindByValue((objdate.Month.ToString())).Selected = true;
// DropDownListCalYear.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
}
private void Populate_YearddList()
{
//Year list can be extended
int intYear;
for (intYear = Convert.ToInt16(DateTime.Now.Year) - 120; intYear <= DateTime.Now.Year + 0; intYear++)
{
DropDownListCalYear.Items.Add(Convert.ToString(intYear));
}
DropDownListCalYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
}
protected void CalendarSource_SelectionChanged(object sender, EventArgs e)
{
if (CalendarSource.SelectedDates.Count == 1)
// If one date is selected, display it.
LabelDate.Text = "Selected date: " + CalendarSource.SelectedDate;
else
// If multiple dates are selected, display them.
LabelDate.Text = "Selected dates: " + CalendarSource.SelectedDates[0] + " to " + CalendarSource.SelectedDates[CalendarSource.SelectedDates.Count - 1];
}
protected void DropDownListCalMonth_SelectedIndexChanged(object sender, EventArgs e)
{
CalendarSource.TodaysDate = Convert.ToDateTime(DropDownListCalMonth.SelectedItem.Value + " 1, " + DropDownListCalYear.SelectedItem.Value);
}
protected void DropDownListCalYear_SelectedIndexChanged(object sender, EventArgs e)
{
CalendarSource.TodaysDate = Convert.ToDateTime(DropDownListCalMonth.SelectedItem.Value + " 1, " + DropDownListCalYear.SelectedItem.Value);
}
}
Databasen har primærnøglen ID der bliver auto incrementeret, en varchar Navn og en datetime Tid.