473,847 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help FormView Problem!! editItemTemplat e controls retain there values...

8 New Member
Hi guys,

These question is for all the experts...

Please help me before my brain explodes

The problem is again with the formview control..

I have a formview and i have to use it that when it displays it shows values from the database and i have a link button edit on the ItemTemplate that changes the mode to edit mode and allows to edit the values which can be edited...

Note that i have not bound it to a datasource control and have programmiticall y set the FormView1.DataS ource property to a datatable in a dataset and all the fields in the ITemTemplate have been manually set to the required fields with <%Eval("")%> and <#bind> in the EditItemTemplat e....

so basically i want to have my own edit template that allows for dropdownlists and checkbox controls and then it displays the values using these binding expressions.

.i would use say the SelectedValue property in a dropdownlist to a Bind() and it would show the value of the corressponding column....every thing was working fine but then this huge problem appears....

The only thing that needs to be done was to create my own UPDATE query in the FormView1_ItemU pdating event and call it to update the data......

I used the findControl method to find the control say firstname and then use it in the update query as

dim x as TextBox

x=Ctype(FormVie w1.FindControl( "first_name_txt Box"),TextBox)

and use x because x would be holding the new value which the user enters.....but the control on examining retains its old value which was data bound and not the new value whichanyone enters....so this makes my update query almost useless.......i t doesnt show any error and works fine...but since the fields retain their old value everything fails



here is the partial source code of my Codefile

Expand|Select|Wrap|Line Numbers
  1. Imports Microsoft.VisualBasic
  2. Imports System
  3. Imports System.Data
  4. Imports System.Data.OleDb
  5. Imports System.Globalization
  6.  
  7. Partial Class _Default
  8.     Inherits System.Web.UI.Page
  9.     Dim con As OleDbConnection
  10.     Dim cmd As OleDbCommand
  11.     Dim da As OleDbDataAdapter
  12.     Dim param As OleDbParameter
  13.     Dim ds As DataSet
  14.     Dim DTF As DateTimeFormatInfo
  15.     Dim dob As DateTime
  16.  
  17.  
  18.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  19.                 Call displayform()
  20.     End Sub
  21. Sub displayform()
  22.  
  23.         con = New OleDbConnection()
  24.         con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & HttpContext.Current.Server.MapPath("sandesh.mdb")
  25.         cmd = New OleDbCommand("select * from [general] where user_id=@USERID", con)
  26.         con.Open()
  27.         param = New OleDbParameter("USERID", OleDbType.VarChar, 20)
  28.         cmd.Parameters.Add(param)
  29.         param.Value = "pankajsingh5k"
  30.         da = New OleDbDataAdapter(cmd)
  31.         'da.MissingSchemaAction = MissingSchemaAction.AddWithKey
  32.         ds = New DataSet
  33.         'da.FillSchema(ds, SchemaType.Source, "general")
  34.         da.Fill(ds, "General")
  35.         con.Close()
  36.         FormView1.DataSource = ds.Tables(0)
  37.         FormView1.DataBind()
  38.         If FormView1.CurrentMode = FormViewMode.Edit Then
  39.             Call populateYear()
  40.         End If
  41.     End Sub
  42. Protected Sub FormView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles FormView1.ModeChanging
  43.         If e.NewMode = FormViewMode.Edit Then
  44.  
  45.             FormView1.ChangeMode(e.NewMode)
  46.         ElseIf e.NewMode = FormViewMode.ReadOnly Then
  47.             FormView1.ChangeMode(e.NewMode)
  48.         End If
  49.         Call displayform()
  50.     End Sub
  51. Sub edit()
  52.         Dim dt As Date
  53.                Dim email, fname, lname, city, state, country, profession, birth_place, birth_time, zipcode As TextBox
  54.         Dim v As String
  55.  
  56.         email = CType(FormView1.FindControl("email_id_txtBox"), TextBox)
  57.         fname = CType(FormView1.FindControl("fname_txtBox"), TextBox)
  58.         lname = CType(FormView1.FindControl("lname_txtBox"), TextBox)
  59.         city = CType(FormView1.FindControl("city_txtBox"), TextBox)
  60.         state = CType(FormView1.FindControl("state_txtBox"), TextBox)
  61.         country = CType(FormView1.FindControl("country_txtBox"), TextBox)
  62.         profession = CType(FormView1.FindControl("profession_txtBox"), TextBox)
  63.         birth_place = CType(FormView1.FindControl("birth_place_txtBox"), TextBox)
  64.         birth_time = CType(FormView1.FindControl("birth_time_txtBox"), TextBox)
  65.         zipcode = CType(FormView1.FindControl("zipcode_txtBox"), TextBox)
  66.         Dim Year, month, day, gender, relation, languages, belongs_to As DropDownList
  67.         Year = CType(FormView1.FindControl("dob_year"), DropDownList)
  68.         month = CType(FormView1.FindControl("dob_month"), DropDownList)
  69.         day = CType(FormView1.FindControl("dob_day"), DropDownList)
  70.         gender = CType(FormView1.FindControl("gender_DDList"), DropDownList)
  71.         relation = CType(FormView1.FindControl("relation_DDList"), DropDownList)
  72.         languages = CType(FormView1.FindControl("language_DDList"), DropDownList)
  73.         belongs_to = CType(FormView1.FindControl("belongs_to_DDList"), DropDownList)
  74.         dt = New DateTime(Year.SelectedValue, month.SelectedValue, day.SelectedValue)
  75.  
  76.     Dim updatecmd As New OleDbCommand()
  77.  
  78.  
  79.     dim v as String
  80.     v=fname.Text
  81.         MsgBox(v)
  82.     'shows the original value not the one user has entered
  83.         Dim str As String
  84.  
  85.         'was testing the 3 versions and my main problem is that it retains its old values and 2nd one works
  86.         'str = "update general set first_name='" & fname.Text.Replace("'", "''") & "', last_name ='" & lname.Text.Replace("'", "''") & "', gender ='" & gender.SelectedValue & "', date_of_birth ='" & dt & "' , birth_place ='" & birth_place.Text.Replace("'", "''") & "', birth_time ='" & birth_time.Text.Replace("'", "''") & "', city ='" & city.Text.Replace("'", "''") & "', state ='" & state.Text.Replace("'", "''") & "', zipcode =" & CInt(zipcode.Text) & ", country ='" & country.Text.Replace("'", "''") & "', profession ='" & profession.Text.Replace("'", "''") & "', relation_status ='" & relation.SelectedValue & "', languages ='" & languages.SelectedValue & "', belongs_to ='" & belongs_to.SelectedValue & "', interested_in ='hello' where user_id='pankajsingh5k'"
  87.       working  'str = "update general set first_name='pankajfgfgfbbbb' where user_id='pankajsingh5k'"
  88.         'str = "update [general] set [first_name]='" & fname.Text.Replace("'", "''") & "' where [user_id]='pankajsingh5k'"
  89.  
  90.         MsgBox(str)
  91.  
  92.         updatecmd = New OleDbCommand(str, con)
  93.     con.open()
  94.         updatecmd.ExecuteNonQuery()
  95.         con.Close()
  96.     End Sub
  97.  
  98.     Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  99.         Call edit() calls the edit function
  100.     End Sub
  101. End Class
please help me i have tried everything that comes to my mind....if this works my work will be finished...why this is happening.....i f u guys ask i can post the whole source code
Feb 11 '09 #1
4 8573
Frinavale
9,735 Recognized Expert Moderator Expert
I see your problem, it's on line 37 of the above posted code.
You're calling DataBind in your Page Load event (well in the method that you're calling in your Page Load).

What's happening is that you are binding the old data to your FormView before you can access what the user entered.

Move the DataBind to your PreRender event.
You can set the DataSource for the control, just don't bind the data to it until the very end of your page's life cycle.
Feb 11 '09 #2
pankajsingh5k
8 New Member
Thanks a lot....

i did as you asked and it works


Thanx for ur time
Feb 13 '09 #3
saegen
1 New Member
I'm sort of a noob and had the same problem. Finding this info took me hours of reading msdn and a bunch of other sites.
T.Saxeggen
Dec 8 '09 #4
Frinavale
9,735 Recognized Expert Moderator Expert
I hope it helped you!
It's a common mistake that many people make and it's not well documented because the problem has to do with binding and the asp.net page life cycle.

-Frinny
Dec 8 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2417
by: Penny Bond | last post by:
Hi, Any help or suggestions on this one would be gratefully appreciated: I have 2 aspx pages one is called 'Query' and the other 'Details'. Query page has a number of text boxes and drop down's for parameter selection, a 'Search' button and a grid that displays the results (the results contain links to 'Details' page with query string params). The 'Details' Page allows user to see detailed information on the selected product.
9
2822
by: Dick | last post by:
How do I set the focus to a control that is a child to a FormView? I've tried the obvious (below) and lots of variations but none seam to work! Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.FormView1.Row.FindControl("TextBox1").Focus() End Sub
0
1186
by: Hawk | last post by:
Hello, this is my first post hope to get some good answers. I am using asp.net 2.0 and I am trying to pass the value entered in the formview textbox in insert mode "in this case it is the default mode" to a label or hidden field outside of the formview when the insert button is pushed. Is there a way to do this? The App I am creating using many interrelated tables by forgiegn keys and in this case I need to insert a record in one...
3
4774
by: Geek | last post by:
Guys, I am working on a project, and I thought using FormView will help good but I am running into a issue. What I have done is that in Form View in Footer template icluded a Link Button which is servind as an edit command. When user click it should change the Formview to Edit mode. But when I click first on it is changing the mode to edit, it is not diplaying the Edit Item template, I have to click again in order to get into Edit Item...
1
1285
by: pkirk25 | last post by:
I have a program written in C++ with STL that does work. If I double click on the executable, it examines a data source and write a report to a file and writes useful messages to cout. I want to be able to call this from another program, for example, a GUI. So I write a small test application that only calls the working program.
3
1427
by: darkmage1975 | last post by:
Hi to all! I'm using a FormView control heavily customized and I need to enable some inner textboxes when the user write something in another inner textbox. So I need to use inner controls events but ... they seem not to work! I wrote something like that: <asp:FormView ID="fv" runat="server" AllowPaging="True" DataKeyNames="id" DataSourceID="dsMain" DefaultMode="Insert" Width="760px"> <EditItemTemplate> .... </EditItemTemplate>
10
10934
SaiRockinGuy
by: SaiRockinGuy | last post by:
Hi, How to retain values in jsp using struts2 tags. I am using <s:select></s:select> which has inbuilt properties in it. But the values are not retained when the page gets refreshed.Can anyone help me in this. Thanks in advance!
0
1273
by: rosiemac | last post by:
Please delete - problem fixed
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9734
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9493
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5729
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4130
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.