?
Solved

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

Posted on 2014-02-21
4
Medium Priority
?
1,862 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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

770 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