Avatar of Cluskitt
CluskittFlag for Portugal

asked on 

Predefine a value in a formview insert field

Is there a way to predefine a value in the insert template of a formview with a session variable? If I use:
Text='<%# Session("ddlemp") %>'
the textbox will have the correct value written, but it doesn't get sent in the SQL. A NULL value is sent instead. But if I use:
Text='<%# Bind("FK_ID_intEmpresa") %>'
then I can't assign the value I want. I tried leaving the Bind and use the FormView1_ModeChanged event:
    If FormView1.CurrentMode = FormViewMode.Insert Then
      CType(FormView1.FindControl("FK_ID_intEmpresaTextBox"), TextBox).Text = Session.Item("ddlvalue")
    End If

That generates an error. Basically, the control isn't there yet at this point. Any ideas?
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Bob Learned
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't use the FormView enough to have any confidence in an answer, but I believe that what you are looking for is the ItemCreated event handler:

Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As EventArgs)
   If FormView1.CurrentMode = FormViewMode.Insert Then
        Dim chk As CheckBox = TryCast(FormView1.Row.FindControl("CheckBox1"), CheckBox);
        If chk IsNot Nothing Then
           chk.Checked = True
        End If
   End If
End Sub

Open in new window

Avatar of Cluskitt
Cluskitt
Flag of Portugal image

ASKER

Actually, I've found out how to do it. At form load, I just do a:

SqlDataSource2.InsertParameters("FK_ID_intEmpresa").DefaultValue = Session.Item("ddlvalue")

Out of curiosity, which control do you use for data binding?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Cluskitt
Cluskitt
Flag of Portugal image

ASKER

Actually, in this case that would be complicated. The user doesn't even get a chance to see or change the field value of one of the keys. The value to be used is in a session variable. I tried before to bind the textbox with the session, but the sqldatasource wouldn't accept that and sent it as NULL. I tried to bind to the table field and then set the text in code behind, but that didn't work either.
The only thing that worked was adding the DefaultValue in FormView1_ItemCreated.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Then, go with the ItemCreated solution...
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo