Forgive me, but I'm a .NET newbie. I know there has to be a way t开发者_JAVA技巧o do this.
I have a details view with it's default mode set to Insert. This is to be used as a form for users to enter records into a database. For two of the fields, I want to pre-populate the field with information. Specifically, I have a vb script with a few functions to query Active Directory to return the user's name based upon their user id. This script works fine.
I call the functions for the user name like this:
<InsertItemTemplate>
<asp:TextBox ID="txtUserID" runat="server" Text='<%#CurrentUID() %>'></asp:TextBox>
</InsertItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# GetUserName(CurrentUID()) %>'></asp:TextBox>
</InsertItemTemplate>
When the page loads, this populates the text boxes with the user id and the user name.
But when the form is submitted, the values in the text boxes are not inserted in to the database.
How can I set default values for the fields when the values come from functions?
Thanks for any help!
The detailsview doesn't know which database field to bind to. I would change the textbox in the template to bind to a field and set the default value in code:
<InsertItemTemplate>
<asp:TextBox ID="txtUserID" runat="server" Text='<%#Bind("UserIDField") %>'></asp:TextBox>
</InsertItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%#Bind("UserNameField") %>'></asp:TextBox>
</InsertItemTemplate>
Protected Sub DetailsView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView.DataBound
If DetailsView.CurrentMode = DetailsViewMode.Insert Then
DirectCast(DetailsView.FindControl("txtUserID"), TextBox).Text=CurrentUID()
DirectCast(DetailsView.FindControl("txtUserName"), TextBox).Text=GetUserName(CurrentUID())
End If
End Sub
精彩评论