Search code examples
asp.netvb.netlinq-to-entitiesdetailsview

Setting Values for Invisible Fields/Properties in DetailsView


I have a DetailsView which has two fields - one that is visible, one that is not. The first the user fills out, the second I want to auto-populate. Unfortunately, I haven't been able to find a way to set the value in this second invisible field. I've tried adding code like this to the Page_Load:

 If Not IsPostBack Then
        DetailsView1.DefaultMode = DetailsViewMode.Insert
        Dim txt1 As TextBox = DirectCast(DetailsView1.FindControl("Type"), TextBox)
        txt1.Text = "administrator"
 End If

But this returns an error of "Object reference not set to an instance of an object." Any ideas on how to accomplish this - either using the method above or another method?

The hoped for end result is that when a new record is inserted via the DetailsView that this record will include the username (entered by the user) as well as the "type" of "administrator"


Solution

  • You should set the style of the controls :

    style="visibility: hidden; height: 0"
    

    That way the browser will not show them.

    If you set the controls on the server to invisible, there will be no html rendered.

    Edit: You could also use a hidden field:

    <asp:HiddenField runat=server Value="SomeValue" />
    

    But, it may be better to keep the information on the server, in a session variable or something. Information in hidden fields on the client can be compoimised).