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
I Basically want to click insert, and have The customer number auto populated, as well as todays date etc...
Thanks
Jeff
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("Cus tomerTextB ox")
Box.Text = "TJS-01"
End Sub
But I again I do not know how to implement it or fire it?
Thanks
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("Cus
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?
ASKER
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'
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()
Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
to
Public Sub setdefaultvalue()
ASKER
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
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.
Dim Box As TextBox = TryCast(Source, TextBox)
to the name of the textbox that you want to set the value for.
ASKER
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"
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"
ASKER
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("Cus tomerTextB ox")
Box.Text = "TJS-01"
Dim Box As TextBox = FormView1.FindControl("Cus
Box.Text = "TJS-01"
Does the above work?
ASKER
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("Cus tomerTextB ox")
Box.Text = "TJS-01"
End Sub
Then simply call it in page load
setdefaultvalue()
Public Sub setdefaultvalue()
Dim Box As TextBox = FormView1.FindControl("Cus
Box.Text = "TJS-01"
End Sub
Then simply call it in page load
setdefaultvalue()
ASKER
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.
Object reference not set to an instance of an object
And the Box.Text="TJS-01" is highlighted.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did that and I got the same error
Object reference not set to an instance of an object
Object reference not set to an instance of an object
ASKER
I got it! :)
Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
Dim Box As TextBox = FormView1.FindControl("Cus tomerTextB ox")
Box.Text = "TJS-01"
End Sub
Then add OnPreRender='SetDefaultVal ue' to the asp:TextBox Like This
<td style="text-align: left">
<asp:TextBox ID="CustomerTextBox" runat="server"
ontextchanged="CustomerTex tBox_TextC hanged" style="text-align: left"
Text='<%# Bind("Customer") %>' OnPreRender='SetDefaultVal ue'/>
</td>
Public Sub setdefaultvalue(ByVal Source As Object, ByVal e As EventArgs)
Dim Box As TextBox = FormView1.FindControl("Cus
Box.Text = "TJS-01"
End Sub
Then add OnPreRender='SetDefaultVal
<td style="text-align: left">
<asp:TextBox ID="CustomerTextBox" runat="server"
ontextchanged="CustomerTex
Text='<%# Bind("Customer") %>' OnPreRender='SetDefaultVal
</td>
ASKER
Thank You for all of your patience and help with this.
Great :-)
http://dotnetspidor.blogspot.com/2009/05/fill-aspnet-formview-textbox-to-initial.html