[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

Dynamically assign values to bound template fields

I have a simple detailsview control that shows the following fields:
- Username
- Address
- City
- State
- CreatedBy
- CreatedDTS

The control is bound to an Access table using fields with the same names (Username, Address, etc.)

My dilemma:
I would like the “CreatedBy” and “CreatedDTS” fields to be generated/updated based on the date/time the record is created/modified using data stored in a Session variable (Session(“Username”)) and the “Now()” function.

For example:
CreatedBy = Session(“Username”)
CreatedDTS = Now()

I don’t want the users to have the ability to update these values at all. I want this all to be done, “behind the scenes” once the select the “Insert” or “Update” commands.

I thought I could create TemplateFields for “CreatedBy” and “CreatedDTS” and then bind them to the proper fields (ie: Bind(“CreatedBy”) and Bind(“CreatedDTS”)), but then I can’t figure out how to assign the default values to these fields <  Session(“Username”) and Now()  >.

I am a novice at .net, so any help is appreciated. I’m sure the answer is right in front of me, but I just can’t seem to figure this one out.

Thanks,
Scott
0
spillsbury
Asked:
spillsbury
1 Solution
 
bele04Commented:
If your CreatedBy and CreatedDTS are template fields then you can do something like this to achieve what you want:

<asp:TemplateField HeaderText="CreatedDTS">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>"></asp:Label>
                        <asp:Label ID="Label3" runat="server" Text="<%= Session['asd'] %>"></asp:Label>
                    </ItemTemplate>
</asp:TemplateField>

Notice that I used single quotes on the Session since if you use double quotes an error will appear saying that the server tag is not well formed.  You can interchange them also if you want like:

<asp:Label ID="Label3" runat="server" Text='<%= Session["asd"] %>'></asp:Label>

hope it helps...
-bele04-
0
 
spillsburyAuthor Commented:
That worked great!

Thanks very much!
Scott
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now