Solved

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

Posted on 2014-02-21
4
1,757 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
  • 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 350 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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