Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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