Link to home
Start Free TrialLog in
Avatar of Wildone63
Wildone63

asked on

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of Wildone63
Wildone63

ASKER

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
In the article, the setdefaultvalue is being called in page load. Did you try that?
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'
Change this

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

to
Public Sub setdefaultvalue()
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
Change this

Dim Box As TextBox = TryCast(Source, TextBox)

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

I am sorry, I do not follow.
>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"
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"
Does the above work?
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.
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()
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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did that and I got the same error

Object reference not set to an instance of an object
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>

Thank You for all of your patience and help with this.

Great :-)