Domanda

I have got below error message:

Object reference not set to an instance of an object.

Code-behind:

public partial class Edit : System.Web.UI.Page
{
    private TextBox updated_time;

    protected void Page_Load(object sender, EventArgs e)
    {
        updated_time = (TextBox)ABC_DV.FindControl("txt_updated_time");
        updated_time.Text = DateTime.Now.ToString();
    }
}

how could i solve this ?

UPDATED

<asp:DetailsView ID="ABC_DV" runat="server" AutoGenerateRows="False"
        DefaultMode="Edit" DataKeyNames="TYPE_ID" DataSourceID="ABC_EDS">
        <Fields>
            <asp:TemplateField HeaderText="Type Id" SortExpression="TYPE_ID">
                <EditItemTemplate>
                    <asp:TextBox ID="txt_type_id" Width="200" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("TYPE_ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>        
            <asp:TemplateField HeaderText="Updated Time" SortExpression="UDPATED_TIME">
                <EditItemTemplate>
                    <asp:TextBox ID="txt_updated_time" Width="200" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Labe2" runat="server" Text='<%# Bind("UDPATED_TIME") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>            
        </Fields>
    </asp:DetailsView>
È stato utile?

Soluzione

Okay you need to take into consideration the mode the DetailsView is in when attempting to access a control, it will not exist in the hierarchy if it isn't in the edit mode causing the Page_Load to explode when it is called without the DetailsView in edit mode. Add some checks to your code to properly handle the control state.

protected void Page_Load(object sender, EventArgs e)
{
    if (ABC_DV.CurrentMode == DetailsViewMode.Edit) {
      updated_time = (TextBox)ABC_DV.FindControl("txt_updated_time");
      if(null != updated_time)
        updated_time.Text = DateTime.Now.ToString();
    }
}

Altri suggerimenti

TextBox updated_time = ABC_DV.FindControl("txt_updated_time") as TextBox;
if (updated_time  != null)
{
    updated_time.Text = DateTime.Now.ToString();
}

By the time you hit the Page_Load method your controls should have been reconstructed and added back into the page. The fact that you are getting an error at that point indicates that particular control doesn't exist - at least not with the ID that you've specified.

Try moving the code into your PreRender() - this is the method that is executed just before the page is rendered to the response stream, if you have added dynamic controls or messed with the IDs of controls then that should have happened well before this stage.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top