gpt4 book ai didi

c# - 当前上下文中不存在名称 'txtStudentDOB'

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:57 25 4
gpt4 key购买 nike

我有一个 Web 表单用户控件,我想在编辑表单中将日历添加到文本框,但是我在 void Calendar1_SelectionChanged1(object sender, EventArgs e ) 。问题是我无法从编辑表单上下文访问日历。

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;
}
}

这里是HTML代码

 <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
&nbsp;</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" />
&nbsp;<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>

最佳答案

您不能直接访问窗体 View 中的控件。您需要在表单 View 中找到它。试试下面的代码

void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
TextBox txtStudentDOB = frmStudent.FindControl("txtStudentDOB") as TextBox;
if(txtStudentDOB != null)
{
txtStudentDOB.Text = Calendar1.SelectedDate.ToShortDateString();
Calendar1.Visible = false;
}
}

关于c# - 当前上下文中不存在名称 'txtStudentDOB',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25766230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com