Link to home
Start Free TrialLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

how to put the current logged user on databound field

I have a details view with all databound fields

there is one field named ModifiedBy that I want to assign the current logged user to
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" ReadOnly="true" />

Open in new window


I am trying to do it this way in the code behind:

    Private Sub dvRecipeItem_DataBound(sender As Object, e As EventArgs) Handles dvRecipeItem.DataBound

        Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
        CType(Me.dvRecipeItem.Rows(0).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy
    End Sub

Open in new window


but I get a:

System.NullReferenceException: Object reference not set to an instance of an object

on line

CType(Me.dvRecipeItem.Rows(0).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy


Would anyone be able to offer some help on what is my problem?

thank you
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

I would imagine that it isn't able to find a control called "ModifiedBy" in the first row. Either that or it isn't able to convert it to a textbox.

Can you post the markup that is produced for that field at runtime? Either that or you could try something like:
CType(Me.dvRecipeItem.Rows(0).Cells(IndexOfYourBoundCell).Controls(0), TextBox).Text = strModifiedBy

Open in new window

Avatar of metropia

ASKER

here is the html:
<table id="ContentPlaceHolder1_dvRecipeItem">
	<tr><td>Id</td><td>8</td></tr>
	<tr><td>Item Number</td><td>10441</td></tr>
	<tr><td>VersionNumber</td><td>6</td></tr>
	<tr><td>Modified Date Time</td><td>7/12/2013 17:00:00</td></tr>
	<tr><td>Modified By</td><td>enard@clove.net</td></tr>
</table>

Open in new window


the aspx (right now the value comes from the entity data source
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number" SortExpression="ItemNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="VersionNumber" HeaderText="VersionNumber" SortExpression="VersionNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ModifiedDateTime" HeaderText="Modified Date Time" SortExpression="ModifiedDateTime" ApplyFormatInEditMode="True" ReadOnly="true"/>
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" ReadOnly="true" />
</Fields>

Open in new window

vb.net code
Private Sub dvRecipeItem_DataBound(sender As Object, e As EventArgs) Handles dvRecipeItem.DataBound

Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
CType(Me.dvRecipeItem.Rows(4).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy

End Sub

Open in new window


Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that was the fix. although I had to replace .Value with .Text at the end

Thank you!