Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get selected value in Formview when formviewcurrentmode FormViewMode.Edit

Posted on 2014-02-21
4
Medium Priority
?
1,909 Views
Last Modified: 2014-02-28
Hi!

Yesterday I received excellent help from guru_sami so that in my formview,=Formview1, when Formview.currentmode = FormViewMode.Insert I get a dropdownlist which datasource is created in the FormView1.DataBound event.

I get all the correct values and they are all inserted into the database.

Now, to my remaining problem:

When formviewcurrentmode FormViewMode.Edit, I am also able to retrieve the dropdownlist, but not the item that was selected when the insert was done appears by default in the dropdownlist. Instead the first value of the dropdownlist appears.

What I want is: When I update the a record in the formview, the preselected value in the dropdownlist should be the value thats associated with this record in the database, but I should also be able to select from the complete list in the dropdownlist to be able to change the value.

This works fine for med when I use SQL-datasource control.


This snippets show all of my subs
<script runat="server">

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        FormView1.PageIndex = GridView1.SelectedIndex
    End Sub
    
    Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs) Handles FormView1.ItemInserting
        Dim ddl As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
        AccessDataSource1.InsertParameters("Lank").DefaultValue = ddl.SelectedValue
    End Sub
 
    Protected Sub FormView1_Itemupdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
        Dim ddl As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
        AccessDataSource1.UpdateParameters("Lank").DefaultValue = ddl.SelectedValue
    End Sub
    
    Protected Sub FormView1_DataBoundedit(sender As Object, e As EventArgs) Handles FormView1.DataBound
        If FormView1.CurrentMode = FormViewMode.Edit Then
            Dim dropdownlist2 As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
            Dim list As New Generic.List(Of String)
            list.Add("test23")
            list.Add("testny")
            list.Add("testny140221")
            dropdownlist2.DataSource = list
            dropdownlist2.DataBind()
        End If
    End Sub
    
    
        
    Protected Sub FormView1_DataBound(sender As Object, e As EventArgs) Handles FormView1.DataBound
        If FormView1.CurrentMode = FormViewMode.Insert  Then
            Dim dropdownlist2 As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
            Dim list As New Generic.List(Of String)
            list.Add("test2")
            list.Add("testny")
            list.Add("testny140221")
            dropdownlist2.DataSource = list
            dropdownlist2.DataBind()
        End If
    End Sub
</script>

Open in new window


I, realize I have to change the sub FormView1_DataBoundedit but I dont know how to. Please give my a clue so that I can try another solution.
formview-problem-in-edit-mode.jpg
0
Comment
Question by:marcgu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39876960
Add an ElseIf to see if the formview is in Edit mode. Then set the SelectedIndex or SelectedItem of the dropdownlist.
0
 

Author Comment

by:marcgu
ID: 39877071
I guess your right, so I changed the sub to this


This last line, is now set to a fixed value =2 (for test reasons). How do I find the correct selectedindex

  dropdownlist2.SelectedIndex = 2


Protected Sub FormView1_DataBound(sender As Object, e As EventArgs) Handles FormView1.DataBound
        If FormView1.CurrentMode = FormViewMode.Insert  Then
            Dim dropdownlist2 As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
            
            Dim list As New Generic.List(Of String)
            list.Add("test2")
            list.Add("testny")
            list.Add("testny140221")
            dropdownlist2.DataSource = list
            dropdownlist2.DataBind()
            
        ElseIf FormView1.CurrentMode = FormViewMode.Edit Then
            Dim dropdownlist2 As DropDownList = CType(FormView1.FindControl("DropDownList2"), DropDownList)
            Dim list As New Generic.List(Of String)
            list.Add("test2")
            list.Add("testny")
            list.Add("testny140221")
            dropdownlist2.DataSource = list
            dropdownlist2.DataBind()
            dropdownlist2.SelectedIndex = 2
            
        End If

Open in new window

       
       
       
    End Sub
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1400 total points
ID: 39885348
Use the DataItem property of the formview. Check example at following page

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.dataitem(v=vs.110).aspx
0
 

Author Closing Comment

by:marcgu
ID: 39896097
Thank you!
Now it works
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question