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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
CodeCruiserCommented:
In the article, the setdefaultvalue is being called in page load. Did you try that?
0
CompTIA Cloud+

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

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
CodeCruiserCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.