I have a Web form user control and I want to add a calender to a textbox in edit form, but I get this error for txtStudentDOB in void Calendar1_SelectionChanged1(object sender, EventArgs e) . the problem is I cannot access to calender from the edit form context.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
namespace LearningSystem.Controls
{
public partial class usrStudent : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.Visible = false;
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if (Calendar1.Visible == false)
{
Calendar1.Visible = true;
}
else
{
Calendar1.Visible = true;
}
}
void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
txtStudentDOB.Text = Calendar1.SelectedDate.ToShortDateString();
Calendar1.Visible = false;
}
}
here is HTML codes
<asp:FormView ID="frmStudent" runat="server" DataSourceID="odsStudent" Width="100%" OnItemDeleted="frmStudent_ItemDeleted" DataKeyNames="StudentID" OnItemInserted="frmStudent_ItemInserted" OnItemUpdated="frmStudent_ItemUpdated" >
<EditItemTemplate>
<asp:TextBox ID="txtStudentDOB" runat="server" CssClass="form-control" Text='<%# Bind("StudentDOB" , "{0:d}") %>' />
<asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" Display="Dynamic" ControlToValidate="txtStudentDOB" ErrorMessage="Please Enter Date of Birth" ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RangeValidator ID ="rvDate" runat ="server" C
</div>
</div>
<div class="col-md-offset-2 col-md-10"">
<asp:LinkButton ID="btnUpdate" runat="server" CssClass="btn btn-success" CausesValidation="True" CommandName="Update" Text="Update" OnClick="btnUpdate_Click" />
<asp:LinkButton ID="btnCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" Font-Bold="false" />
</div>
</div>
</EditItemTemplate>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged1" > </asp:Calendar>
EditItemTemplate
? If it was inside you could do:TextBox = txtStudentDOB = (TextBox)((FormViewItem)((Calendar)sender).NamingContainer).FindControl("txtStudentDOB")
– Tim Schmelter