Loading a value in an insert command in the detail view
In a detailed view, how can I pre-fill one of the textboxes in the insert command (when the user clicks on insert and the view is inserted).
I think this will work for codebehind:
Dim txtBox As TextBox = FormView1.FindControl ("txtbox")
txtbox.Text = "Whatever I Want"
Is it correct? What do I need in aspx (not so exact)? Also, I am assuming the server side code will be included in the item element or insert an event.
I typed this in VB.NET, but I am using C # (I can do so in the forum agnostic language too, I can inject the problem in another language). I am also using SqlDataSource, with my parameters and all nested / deleted / editable commands.
I am trying to generate a random GUID (using a GUID object) that will be prepopulated in a textbox.
Also, the postbackurl property of the button isn't another way to save the state of the form?
thank
source to share
I would update the field in the DetailsView to a TemplateField:
<asp:TemplateField>
<InsertItemTemplate>
<asp:TextBox ID="txtField" runat="server" Text='<%# Bind("GUID") %>'/>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblField" runat="server" Text='<%# Bind("GUID") %>'/>
</ItemTemplate>
</asp:TemplateField>
Then you have two options:
- generate your GUID and paste into your datasource. This can be done with SQL as you mentioned using SqlDataSource
-
remove binding and access controls from code in the DataBound Event of your DetailsView
Private Sub dv_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles dv.DataBound dim txt as Textbox = dv.FindControl("txtField") txt.Text = GenerateGUID() End Sub
source to share
I am doing something similar too. I am hiding the DetailsView and showing it when the user clicks the button.
dvDetails.ChangeMode(DetailsViewMode.Insert)
pnlDetailMenu.Visible = True
Dim ColumnTextBox As TextBox
ColumnTextBox = dvDetails.Rows(0).Cells(1).Controls(0)
If Not ColumnTextBox Is Nothing Then
ColumnTextBox.Text = "Initial Value"
End If
source to share