Auto fill textboxes on FormView

Is there a way to autofill text boxes when you are inserting a record with FormView in VS-2008?

I Basically want to click insert, and have The customer number auto populated, as well as todays date etc...

Thanks
Jeff
LVL 1
Wildone63Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
This would work only in edit mode so put the call to the function in button which puts the formview into edit mode instead of page load.
0
 
Wildone63Author Commented:
That is Very Close but I am embarrassed to admit I do not know C# I tried to convert it to VB but no luck.

I did find this on here but I am having trouble understanding how to implement it.

    Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
        Dim Box As TextBox = TryCast(Source, TextBox)
        If Box IsNot Nothing Then
            Box.Text = 'TJS-01'
        End If
    End Sub

And I tried this too.

    Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
        Dim Box As TextBox = FormView1.FindControl("CustomerTextBox")
        Box.Text = "TJS-01"


    End Sub

But I again I do not know how to implement it or fire it?

Thanks
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CodeCruiserCommented:
In the article, the setdefaultvalue is being called in page load. Did you try that?
0
 
Wildone63Author Commented:
Yes

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        setdefaultvalue()

    End Sub

But I am missing something here, Argument not specified for parameter 'e'
0
 
CodeCruiserCommented:
Change this

Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)

to
Public Sub setdefaultvalue()
0
 
Wildone63Author Commented:
If I do that then I get an error that Source is not defined.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        setdefaultvalue()

    End Sub

    Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
        Dim Box As TextBox = TryCast(Source, TextBox)

        If Box IsNot Nothing Then
            Box.Text = "TJS-01"
        End If
    End Sub
0
 
CodeCruiserCommented:
Change this

Dim Box As TextBox = TryCast(Source, TextBox)

to the name of the textbox that you want to set the value for.

0
 
Wildone63Author Commented:
I am sorry, I do not follow.
0
 
CodeCruiserCommented:
>Dim Box As TextBox = TryCast(Source, TextBox)

The above approach is when you use a single method for handling multiple controls (such as a single method for handling keypress event of many textboxes). As a test, first try to set value of one textbox in this method to see if it works

textbox1.text = "sometext"
0
 
Wildone63Author Commented:
Well the problem is I do not know how to access the textbox control because it is inside of the formview.

        Dim Box As TextBox = FormView1.FindControl("CustomerTextBox")
        Box.Text = "TJS-01"
0
 
CodeCruiserCommented:
Does the above work?
0
 
Wildone63Author Commented:
No because of the error Argument not specified for parameter 'e' and if I take out the (ByVal sender as object, byVal e as System.eventARgs) I get the error Source is not defined.
0
 
CodeCruiserCommented:
You are mixing things up. Try this function

Public Sub setdefaultvalue()
        Dim Box As TextBox = FormView1.FindControl("CustomerTextBox")
        Box.Text = "TJS-01"
End Sub


Then simply call it in page load
setdefaultvalue()
0
 
Wildone63Author Commented:
I set it exactly and Now I get the error

Object reference not set to an instance of an object

And the Box.Text="TJS-01" is highlighted.
0
 
Wildone63Author Commented:
I did that and I got the same error

Object reference not set to an instance of an object
0
 
Wildone63Author Commented:
I got it! :)

    Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
        Dim Box As TextBox = FormView1.FindControl("CustomerTextBox")
        Box.Text = "TJS-01"
    End Sub

Then add OnPreRender='SetDefaultValue' to the asp:TextBox Like This

                        <td style="text-align: left">
                            <asp:TextBox ID="CustomerTextBox" runat="server"
                                ontextchanged="CustomerTextBox_TextChanged" style="text-align: left"
                                Text='<%# Bind("Customer") %>' OnPreRender='SetDefaultValue'/>
                            &nbsp;</td>

0
 
Wildone63Author Commented:
Thank You for all of your patience and help with this.

0
 
CodeCruiserCommented:
Great :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.