tags:

views:

633

answers:

4

Hi,

In a detailsview, how can I prepopulate one of the textboxes on the insertcommand (When the user clicks insert and the view is insert).

I think this would work for codebehind:

Dim txtBox As TextBox = FormView1.FindControl("txtbox")

txtbox.Text = "Whatever I want"

Is this right? What do I need in the aspx (not as sure)? Also, I'm assuming the server-side code will go in the itemcommand or insertcreating event.

I have typed this in VB.NET but I am using C# (I can do both so on a language agnostic forum I might type the problem in another language). I am also using a SqlDataSource, with my parameters and insert/delete/edit commands all created.

I am trying to generate a random GUID (using the GUID object), which will be prepopulated in the textbox.

Also, is the postbackurl property of a button not another way of preserving form state?

Thanks

A: 

I'm guessing you need to use one of detailsview events. Hook up to ItemCommand, ModeChanging or ModeChanged events and fill your value there.

Alexander Taran
A: 

I am doing something like this as well. I am hiding the DetailsView and showing it when the user clicks a 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
Anthony K
A: 

Check out this article HowTo:Get a FormView to display a suggested value in Insert Mode

RichardHowells
+1  A: 

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 insert into your datasource. This may have to be done with SQL since you mentioned using SqlDataSource
  • remove the binding and access the 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
    
Mark Glorie