本文介绍了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 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");     
    }
}

这篇关于ASP .NET - 设置一个DetailsView文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:21