Solved

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

Posted on 2014-02-21
4
1,727 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you!
Now it works
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now