ASP .NET - 设置一个DetailsView文本框的值文本框、ASP、NET、DetailsView

由网友(此间滋味与谁人)分享简介:我试图用code-后面(的Page_Load或preRender)在一个DetailsView设定一个日期 - 时间文本框,以便它默认为当前日期时间。I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time t...

我试图用code-后面(的Page_Load或preRender)在一个DetailsView设定一个日期 - 时间文本框,以便它默认为当前日期时间。

I am attempting to use the code-behind (Page_Load or PreRender) to set a date-time textbox in a DetailsView so that it defaults to a current date-time.

我试图为(许多变化中的一个):

What I have attempted is (one of many variations):

protected void DetailsView2_PreRender(object sender, EventArgs e)
{
        ((TextBox)DetailsView2.FindControl("date_time")).Text = 
                  DateTime.Now.ToString("d");     
}

但是,所有我得到的是一个'NullReferenceException异常'的错误。

But all that I get is a 'NullReferenceException' error.

我在做什么错了?

推荐答案

您可以使用的DetailsView控件数据绑定事件,你的DetailsView像内设定值:

You can use detailsview controls DataBound event to set a value within your detailsview like that :

<asp:Label ID="DetailsView2" runat="server" OnDataBound="DetailsView2_DataBound">
</asp:Label>

code背后:

Code Behind :

protected void DetailsView2_DataBound(object sender, EventArgs e)
{
    DetailsView myDetailsView = (DetailsView)sender;
    if(myDetailsView.CurrentMode == DetailsViewMode.Edit)
    {
    	((TextBox)myDetailsView.FindControl("date_time")).Text = DateTime.Now.ToString("d");     
    }
}
阅读全文

相关推荐

最新文章