当前上下文中不存在名称“txtStudentDOB”
Posted
技术标签:
【中文标题】当前上下文中不存在名称“txtStudentDOB”【英文标题】:The name 'txtStudentDOB' does not exist in the current context 【发布时间】:2014-11-04 03:36:21 【问题描述】:我有一个 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
</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
之外?如果它在里面,你可以这样做:TextBox = txtStudentDOB = (TextBox)((FormViewItem)((Calendar)sender).NamingContainer).FindControl("txtStudentDOB")
没关系。当我把它放在里面时,我再次收到错误,上面写着“当前上下文中不存在名称'Calendar1'”,但是当我把它放在外面时,我得到了文本框的这个错误。
【参考方案1】:
您不能直接访问表单视图中的控件。您需要在表单视图中找到它。试试下面的代码
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;
【讨论】:
当我从日历中选择一个日期时,它无法显示在文本框中,并且我在此行中收到此错误 Object reference not set to an instance: txtStudentDOB1.Text = Calendar1.SelectedDate.ToShortDateString(); 是 txtStudentDOB1 null 还是 Calendar1 null 还是 Calendar1.SelectedDate? 你能在设计器和 FindControl 函数中检查名称 txtStudentDOB 匹配吗? @TimSchmelter,没错,tx 用于指向,文本框仅在编辑模板中。当然需要检查null 完全匹配。以上是关于当前上下文中不存在名称“txtStudentDOB”的主要内容,如果未能解决你的问题,请参考以下文章