locked
DetailsView DropDownList on update loses value RRS feed

  • Question

  • User-2075826951 posted

    My last post dealt with data entry on initial input.  This is the same issue but on changing or updating the data.

    I have a single DropDownList in a TemplateField in a DetailsView that is populated by one or the other of two different tables based on the setting of a switch.  If the switch is set to 'SCANG' the DropDownList shows Air Guard rank.  If set to 'SCARNG' the DropDownList shows Army Guard rank.

    When I input the record and then bring it up on update to change the record data all is well.  When I change the rank, and the listing is still appropriate for Air or Army, the new rank is lost on update.  When I bring the record up for the third time, the rank is blank or null even though the DropDownList still shows the appropriate table.  I have tried 'SelectedValue', 'SelectedValue.ToString', 'SelectedItem.Text' and 'SelectedItem.Value' but all show up as Null in debug of the CodeBehind.

    Display Code:

                    <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT">
                        <ItemTemplate>
                            <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rank" SortExpression="RANK" ControlStyle-Width="45%" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblRANK" runat="server" Text='<%# Bind("RANK")%>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rank" SortExpression="RANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlRANK" runat="server"/>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Highest Rank" SortExpression="HRANK" ControlStyle-Width="45%" Visible="false">
                        <ItemTemplate>
                            <asp:Label ID="lblHRANK" runat="server" Text='<%# Bind("HRANK")%>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Highest Rank" SortExpression="HRANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlHRANK" runat="server"/>
                        </EditItemTemplate>
                    </asp:TemplateField>
    

    CodeBehind Code:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
            Dim lblRnk As Label = DetailsView1.FindControl("lblRank")
            Dim lblHRnk As Label = DetailsView1.FindControl("lblHRank")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            If lblComp.Text = "SCANG" Then
                ddlRnk.DataSourceID = "SqlDataSource3"
                ddlHRnk.DataSourceID = "SqlDataSource3"
            Else
                ddlRnk.DataSourceID = "SqlDataSource4"
                ddlHRnk.DataSourceID = "SqlDataSource4"
            End If
    
            ddlRnk.SelectedValue = lblRnk.Text
            ddlRnk.DataValueField = "Abbreviation"
            ddlRnk.DataTextField = "Rank"
            ddlRnk.DataBind()
            ddlHRnk.SelectedValue = lblHRnk.Text
            ddlHRnk.DataValueField = "Abbreviation"
            ddlHRnk.DataTextField = "Rank"
            ddlHRnk.DataBind()
    
        End Sub
    
        Protected Sub DetailsView1_ItemUpdating(sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
    
            Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
            Dim lblRnk As Label = DetailsView1.FindControl("lblRank")
            Dim lblHRnk As Label = DetailsView1.FindControl("lblHRank")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            If lblComp.Text = "SCANG" Then
                ddlRnk.DataSourceID = "SqlDataSource3"
                ddlHRnk.DataSourceID = "SqlDataSource3"
            Else
                ddlRnk.DataSourceID = "SqlDataSource4"
                ddlHRnk.DataSourceID = "SqlDataSource4"
            End If
    
            'lblRnk.Text = ddlRnk.SelectedValue
            lblRnk.Text = ddlRnk.SelectedValue.ToString
            'lblRnk.Text = ddlRnk.SelectedItem.Text
            'lblRnk.Text = ddlRnk.SelectedItem.Value
            ddlRnk.DataValueField = "Abbreviation"
            ddlRnk.DataTextField = "Rank"
            ddlRnk.DataBind()
            'lblHRnk.Text = ddlHRnk.SelectedValue
            lblHRnk.Text = ddlHRnk.SelectedValue.ToString
            'lblHRnk.Text = ddlHRnk.SelectedItem.Text
            'lblHRnk.Text = ddlHRnk.SelectedItem.Value
            ddlHRnk.DataValueField = "Abbreviation"
            ddlHRnk.DataTextField = "Rank"
            ddlHRnk.DataBind()
    
        End Sub
    

    Thursday, May 25, 2017 8:33 PM

Answers

  • User-1716253493 posted

    In the 'DetailsView1_ItemUpdating' event where should the 'ddlRnk.DataBind()' and 'ddlhRnk.DataBind()' go; before or after the 'Value', 'DataValueField' and 'DataTextField'?

    I repeat, "Now when I Update / change the Rank in the record, the same value as was originally Inserted is displayed instead of Null.  This feels like progress."

    Thanks for your ideas but I'm not quite there yet.

    AFAIK, ddl.DataBind() is used to populate ddl items and in itemupdating no need to repopulate ddl items because it's already populated in databound event.

    Way 1 : Because you bind hiddenfield value directly. You only need to change the hiddenfield value in itemupdating event, don't rebind the ddl because will ddl reset selection.

    Way 2 : Use eval() in hiddenfield, then set NewValues("rank") in updating event.

    Note : NewValues("paramname") is usefull when you need to pass calculated value. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 1, 2017 1:45 AM

All replies

  • User-707554951 posted

    Hi tagtech,

    Please try to bind the data in OnDataBound event.

    For example:

    In aspx:

              <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px"
                    DataSourceID="SqlDataSource1" HorizontalAlign="Center"
                    AutoGenerateRows="False" AllowPaging="true" OnPageIndexChanging="DetailsView1_PageIndexChanging"
                     AutoGenerateEditButton="true"
                    OnDataBound="DetailsView1_DataBound" OnItemInserted="DetailsView1_ItemInserted" EnableModelValidation="True">

    In behind code:

       Protected Sub DetailsView1_DataBound(sender As Object, e As EventArgs)
            Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
            Dim lblRnk As Label = DetailsView1.FindControl("lblRank")
            Dim lblHRnk As Label = DetailsView1.FindControl("lblHRank")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
            If ddlRnk IsNot Nothing Or ddlHRnk IsNot Nothing Then
                If lblComp.Text = "SCANG" Then
                    ddlRnk.DataSourceID = "SqlDataSource3"
                    ddlHRnk.DataSourceID = "SqlDataSource3"
                Else
                    ddlRnk.DataSourceID = "SqlDataSource4"
                    ddlHRnk.DataSourceID = "SqlDataSource4"
                End If
                ddlRnk.SelectedValue = lblRnk.Text
                ddlRnk.DataValueField = "Abbreviation"
                ddlRnk.DataTextField = "Rank"
                ddlRnk.DataBind()
                ddlHRnk.SelectedValue = lblHRnk.Text
                ddlHRnk.DataValueField = "Abbreviation"
                ddlHRnk.DataTextField = "Rank"
                ddlHRnk.DataBind()
            End If
        End Sub

    Best Regards

    Cathy

    Friday, May 26, 2017 9:08 AM
  • User-1716253493 posted

    try remove page_load event binding, i guess it will reset the dropdown selection eveytime.

    Friday, May 26, 2017 8:55 PM
  • User-2075826951 posted

    CZ,

    Thank you for responding. 

    I have other code in the 'DetailsView1_ItemUpdating' event that I did not show here as being irrelevant to my problem.  When I implement the 'DetailsView1_DataBound' event you suggested, I get the same result on update. On initially invoking the update page, the DropDownList shows the appropriate table data , Air or Army, and the appropriate 'SelectedValue'.  However, when I change the rank and/or highest rank of the person, update the page and display it again, the the DropDownList shows the appropriate table data , Air or Army, but the 'SelectedValue' is Null both in debug and the SQL table. It does not matter whether I delete the 'DetailsView1_ItemUpdating' event entirely, keep both that event as is and add the 'DetailsView1_DataBound' event code you suggested or split my code between the two events, keeping your code as is and only keeping the code that is "irrelevant to my problem" in my old event routine.

    Again, thank you for your response.  I'm looking around but not seeing anything that explains to me why a 'DetailsView1_DataBound' event would work differently or be preferable to the 'ddlRnk.DataBind()' and 'ddlHRnk.DataBind()' statements in my original 'DetailsView1_ItemUpdating' event.

    Thank you for your interest in helping me.

    Tuesday, May 30, 2017 7:09 PM
  • User-2075826951 posted

    oned_gk,

    Thank you for your response.  When I delete the 'Page_Load' event entirely and invoke the update page/routine to display the data record initially, both DropDownLists are blank and do not display any table data or the 'SelectedValue'.  I cannot therefore see the person's rank as initially input into the record or make a change to the record changing the rank.

    Thank you for your input.

    Tuesday, May 30, 2017 7:25 PM
  • User475983607 posted

    The ASP web forms convention is to initialize the member server controls when the page first loads.  Bind the drop downs in the Page_Load event when the state is NOT Page.IsPostBack.  View state will manage the state for each post back so you do not need to rebind the drop downs.

    If Page.IsPostBack Then
      'Do stuff related to post backs
    Else
      'First time the page is requested.  Initialize the page members.
    End If
    

    View State Reference.

    https://msdn.microsoft.com/en-us/library/ms972976.aspx

    Page.IsPostBack Reference

    https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx

    Tuesday, May 30, 2017 7:36 PM
  • User-2075826951 posted

    mgebhard,

    Thank you, especially for the links. 

    I implemented what I think you said and the results were/are the same; initial display of the record in the update page shows the correct rank in the correct table drop down.  Updating the record with different ranks for Rank and Highest Rank shows the correct tables in both drop downs but the Rank and Highest Rank are Null in both debug and SQL Management Studio 2008 R2.

    I know that knowledge of View State is one among many deficiencies in my programming expertise.  I look forward to following the links you offered.

    What I think you said:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
                Dim lblRnk As Label = DetailsView1.FindControl("lblRank")
                Dim lblHRnk As Label = DetailsView1.FindControl("lblHRank")
                Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
                Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
                If lblComp.Text = "SCANG" Then
                    ddlRnk.DataSourceID = "SqlDataSource3"
                    ddlHRnk.DataSourceID = "SqlDataSource3"
                Else
                    ddlRnk.DataSourceID = "SqlDataSource4"
                    ddlHRnk.DataSourceID = "SqlDataSource4"
                End If
    
                ddlRnk.SelectedValue = lblRnk.Text
                ddlRnk.DataValueField = "Abbreviation"
                ddlRnk.DataTextField = "Rank"
                ddlRnk.DataBind()
                ddlHRnk.SelectedValue = lblHRnk.Text
                ddlHRnk.DataValueField = "Abbreviation"
                ddlHRnk.DataTextField = "Rank"
                ddlHRnk.DataBind()
            End If
    
        End Sub
    

    Tuesday, May 30, 2017 8:15 PM
  • User475983607 posted

    I'm guessing you are binding to a data bound control.  And are having difficulty getting the edit template working?  

    See the following SO thread.

    https://stackoverflow.com/questions/7329224/how-to-bind-a-dropdownlist-in-gridview

    https://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx

    https://forums.asp.net/t/1333162.aspx?Binding+dropdownlist+in+gridview

    Tuesday, May 30, 2017 9:25 PM
  • User-1716253493 posted

    try move it from page_load to dv databound event

    Wednesday, May 31, 2017 1:53 AM
  • User-2075826951 posted

    If you will look at my response to Cathy Zou, you will see that I did that.

    Wednesday, May 31, 2017 3:07 PM
  • User-1716253493 posted

    Appologize about that,

    Intead using a label to bind the data, try like this

    Remove label text binding (you can use Eval() instead)

    Ensure "rank" updateparameter is exist in the dv datasource

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            UpdateCommand="Update .... set RANK=@rank ...">
            <UpdateParameters>
                <asp:Parameter Name="rank" />
            </UpdateParameters>
        </asp:SqlDataSource>

    And use the parameter in updatecommand also

    Then set the value in updating event

        Protected Sub DetailsView1_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) 
        Handles DetailsView1.ItemUpdating
            e.NewValues("rank") = "abc" 'get it from ddl selectedvalue
        End Sub

    Wednesday, May 31, 2017 4:42 PM
  • User-1716253493 posted

    Hi, you can't get controls from itemtemplate and edititemtemplate at same time.

    In readonly mode, you only able to get controls in itemtemplate

    In edit mode, you only able to get controls in edititemtemplate.

    So, I modify your templatefield like this

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="lblRANK" runat="server" Text='<%# Bind("RANK")%>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:HiddenField id="hdnRANK" runat="server" Value='<%# Bind("RANK")%>'></asp:HiddenField>
                        <asp:DropDownList ID="ddlRANK" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateField>

    In updating event, you can get the hiddenfield and the ddl, but not the label

    set the hdnRANK.Value = ddlRANK.SelectedValue in updating event

    Binding ddl

        Protected Sub DetailsView1_DataBound(sender As Object, e As EventArgs) Handles DetailsView1.DataBound
            If DetailsView1.CurrentMode = DetailsViewMode.Edit Then
                'get ddl and hiddenfield
                'bind the ddl
                'set ddl.selectedvalue=hdn.value
            End If
        End Sub

    Wednesday, May 31, 2017 5:01 PM
  • User-2075826951 posted

    I know how to set up Display code, I think.

    DisplayCode:

        <div style="text-align:center">
            <br />
            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                    Select Air or Army Branch:&nbsp;
                    <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_Selected">
                        <asp:ListItem Text="" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="Air"></asp:ListItem>
                        <asp:ListItem Text="Army"></asp:ListItem>
                    </asp:DropDownList>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%" DataSourceID="SqlDataSource1" DefaultMode="Insert"
                        HorizontalAlign="Center" DataKeyNames="SSN" AutoGenerateRows="False" EnableModelValidation="True" OnItemInserting="Record_Inserting" >
                        <CommandRowStyle HorizontalAlign="Center" />
                        <Fields>
                            <asp:TemplateField HeaderText="Social Security Number" SortExpression="SSN">
                                <InsertItemTemplate>
                            <asp:BoundField DataField="NAMEL" HeaderText="Name Last" SortExpression="NAMEL" />
                            <asp:BoundField DataField="NAMEF" HeaderText="Name First" SortExpression="NAMEF" />
                            <asp:BoundField DataField="NAMEM" HeaderText="Name Middle" SortExpression="NAMEM" />
                            <asp:BoundField DataField="ADDR" HeaderText="Address" SortExpression="ADDR" />
                            <asp:BoundField DataField="ADDR2" HeaderText="Address 2" SortExpression="ADDR2" />
                            <asp:BoundField DataField="CITY" HeaderText="City" SortExpression="CITY" />
                            <asp:TemplateField HeaderText="State" SortExpression="STATE">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlState" runat="server" Text='<%# Bind("STATE") %>'
                                        DataSourceID="SqlDataSource2" DataTextField="State" DataValueField="Abbreviation" >
                                    </asp:DropDownList>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblState" runat="server"  Text='<%# Bind("STATE") %>'>
                                    </asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ZIP" HeaderText="Zip Code" SortExpression="ZIP" />
                            <asp:BoundField DataField="EMAIL" HeaderText="E-Mail Address" SortExpression="EMAIL" />
                            <asp:BoundField DataField="DOB" HeaderText="Date of Birth" SortExpression="DOB" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:BoundField DataField="DISCHDT" HeaderText="Discharge Date" SortExpression="DISCHDT" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtCmpt" runat="server" Text='<%# Bind("COMPONENT") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="RANK" SortExpression="RANK">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlRANK" runat="server" Text='<%# Bind("RANK")%>'
                                        DataValueField="Abbreviation" DataTextField="Rank" DataSourceId = "SqlDataSource3"></asp:DropDownList>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblRANK" runat="server"  Text='<%# Bind("RANK")%>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Edit Date" SortExpression="EDITDT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Edit User ID" SortExpression="EDITUSERID" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditID" runat="server" Text='<%# Bind("EDITUSERID") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditID" runat="server" Text='<%# Bind("EDITUSERID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <RowStyle HorizontalAlign="Left" />
                    </asp:DetailsView>
                </asp:View>
            </asp:MultiView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:connString %>"
                SelectCommand="SELECT * FROM [Pensions]"
                InsertCommand="INSERT INTO [Pensions] ([NAMEL], [NAMEF], [NAMEM], [ADDR], [ADDR2], [CITY], [STATE], [ZIP], [EMAIL], [DOB], [DISCHDT], [COMPONENT], [RANK], [EDITDT], [EDITUSERID]) VALUES (@NAMEL, @NAMEF, @NAMEM, @ADDR, @ADDR2, @CITY, @STATE, @ZIP, @EMAIL, @DOB, @DISCHDT, @COMPONENT, @RANK, @EDITDT, @EDITUSERID)" >
                <InsertParameters>
                    <asp:Parameter Name="NAMEL" Type="String" />
                    <asp:Parameter Name="NAMEF" Type="String" />
                    <asp:Parameter Name="NAMEM" Type="String" />
                    <asp:Parameter Name="ADDR" Type="String" />
                    <asp:Parameter Name="ADDR2" Type="String" />
                    <asp:Parameter Name="CITY" Type="String" />
                    <asp:Parameter Name="STATE" Type="String" />
                    <asp:Parameter Name="ZIP" Type="String" />
                    <asp:Parameter Name="EMAIL" Type="String" />
                    <asp:Parameter Name="DOB" Type="String" />
                    <asp:Parameter Name="DISCHDT" Type="String" />
                    <asp:Parameter Name="COMPONENT" Type="String" />
                    <asp:Parameter Name="RANK" Type="String" />
                    <asp:Parameter Name="EDITDT" Type="DateTime" />
                    <asp:Parameter Name="EDITUSERID" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                ConnectionString="<%$ ConnectionStrings:connString %>"
                SelectCommand="SELECT * FROM [States]" >
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource3" runat="server"
                ConnectionString="<%$ ConnectionStrings:connString %>"
                SelectCommand="SELECT * FROM [AirRank]" >
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource4" runat="server"
                ConnectionString="<%$ ConnectionStrings:connString %>"
                SelectCommand="SELECT * FROM [ArmyRank]" >
            </asp:SqlDataSource>
        </div>

    The problem we are discussing is that I am unable to get a new SelectedValue after changing / updating the record.  It comes up Null in the DropDownList, in debug in the CodeBehind and in SQL Management Studio.

    Wednesday, May 31, 2017 8:30 PM
  • User-2075826951 posted

    Hi, you can't get controls from itemtemplate and edititemtemplate at same time.

    In readonly mode, you only able to get controls in itemtemplate

    In edit mode, you only able to get controls in edititemtemplate.

    That is useful news to me.  I have modified my display and markup(?) to delete the Labels and use HiddenFields instead.  Now when I Update / change the Rank in the record, the same value as was originally Inserted is displayed instead of Null.  This feels like progress.

    Display:

                    <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT">
                        <ItemTemplate>
                            <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'/>
                        </ItemTemplate>
                    <asp:TemplateField HeaderText="Rank" SortExpression="RANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:HiddenField ID="hdnRank" runat="server" Value='<%# Bind("RANK")%>'/>
                            <asp:DropDownList ID="ddlRANK" runat="server"/>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Highest Rank" SortExpression="HRANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:HiddenField ID="hdnHRank" runat="server" Value='<%# Bind("HRANK")%>'/>
                            <asp:DropDownList ID="ddlHRANK" runat="server"/>
                        </EditItemTemplate>
                    </asp:TemplateField>
    

    Markup (is this another word for CodeBehind?):

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
                Dim hdnRnk As HiddenField = DetailsView1.FindControl("hdnRank")
                Dim hdnHRnk As HiddenField = DetailsView1.FindControl("hdnHRank")
                Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
                Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
                If lblComp.Text = "SCANG" Then
                    ddlRnk.DataSourceID = "SqlDataSource3"
                    ddlHRnk.DataSourceID = "SqlDataSource3"
                Else
                    ddlRnk.DataSourceID = "SqlDataSource4"
                    ddlHRnk.DataSourceID = "SqlDataSource4"
                End If
    
                ddlRnk.SelectedValue = hdnRnk.Value
                ddlRnk.DataValueField = "Abbreviation"
                ddlRnk.DataTextField = "Rank"
                ddlRnk.DataBind()
                ddlHRnk.SelectedValue = hdnHRnk.Value
                ddlHRnk.DataValueField = "Abbreviation"
                ddlHRnk.DataTextField = "Rank"
                ddlHRnk.DataBind()
            End If
    
        End Sub
    
        Protected Sub DetailsView1_ItemUpdating(sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
    
            Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
            Dim hdnRnk As HiddenField = DetailsView1.FindControl("hdnRank")
            Dim hdnHRnk As HiddenField = DetailsView1.FindControl("hdnHRank")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            If lblComp.Text = "SCANG" Then
                ddlRnk.DataSourceID = "SqlDataSource3"
                ddlHRnk.DataSourceID = "SqlDataSource3"
            Else
                ddlRnk.DataSourceID = "SqlDataSource4"
                ddlHRnk.DataSourceID = "SqlDataSource4"
            End If
    
            ddlRnk.DataBind()
            hdnRnk.Value = ddlRnk.SelectedValue
            ddlRnk.DataValueField = "Abbreviation"
            ddlRnk.DataTextField = "Rank"
            'ddlRnk.DataBind()
            ddlHRnk.DataBind()
            hdnHRnk.Value = ddlHRnk.SelectedValue
            ddlHRnk.DataValueField = "Abbreviation"
            ddlHRnk.DataTextField = "Rank"
            'ddlHRnk.DataBind()
    
        End Sub
    

    In the 'DetailsView1_ItemUpdating' event where should the 'ddlRnk.DataBind()' and 'ddlhRnk.DataBind()' go; before or after the 'Value', 'DataValueField' and 'DataTextField'?

    I repeat, "Now when I Update / change the Rank in the record, the same value as was originally Inserted is displayed instead of Null.  This feels like progress."

    Thanks for your ideas but I'm not quite there yet.

    Wednesday, May 31, 2017 8:50 PM
  • User-1716253493 posted

    In the 'DetailsView1_ItemUpdating' event where should the 'ddlRnk.DataBind()' and 'ddlhRnk.DataBind()' go; before or after the 'Value', 'DataValueField' and 'DataTextField'?

    I repeat, "Now when I Update / change the Rank in the record, the same value as was originally Inserted is displayed instead of Null.  This feels like progress."

    Thanks for your ideas but I'm not quite there yet.

    AFAIK, ddl.DataBind() is used to populate ddl items and in itemupdating no need to repopulate ddl items because it's already populated in databound event.

    Way 1 : Because you bind hiddenfield value directly. You only need to change the hiddenfield value in itemupdating event, don't rebind the ddl because will ddl reset selection.

    Way 2 : Use eval() in hiddenfield, then set NewValues("rank") in updating event.

    Note : NewValues("paramname") is usefull when you need to pass calculated value. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 1, 2017 1:45 AM
  • User-2075826951 posted

    Thank you all so very much. 

    Cathy Zou, thank you for making me look closer at databound concepts. 

    mgebhard, thank you for the 'If Not IsPostBack' statement that is crucial in making this work and for all of the links that I will be following up on for some time yet to come, especially the View State stuff.

    oned_gk, thank you profoundly for the education you have given me.  Your insights into 'HiddenField' vs 'Label' and when 'ddl.DataBound()' is and is not appropriate have made for much leaner code that is easier to read both in my Initial Data Entry code and my Update Data code.

    Initial Data Entry display:

            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                    Select Air or Army Branch:&nbsp;
                    <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_Selected">
                        <asp:ListItem Text="" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="Air"></asp:ListItem>
                        <asp:ListItem Text="Army"></asp:ListItem>
                    </asp:DropDownList>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%" DataSourceID="SqlDataSource1" DefaultMode="Insert"
                        HorizontalAlign="Center" DataKeyNames="SSN" AutoGenerateRows="False" EnableModelValidation="True" OnItemInserting="Record_Inserting" >
                        <CommandRowStyle HorizontalAlign="Center" />
                        <Fields>
                            <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT">
                                <InsertItemTemplate>
                                    <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'></asp:Label>
                                </InsertItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Rank" SortExpression="RANK" ControlStyle-Width="45%">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlRANK" runat="server" DataTextField="Rank" DataValueField="Abbreviation"/>
                                </InsertItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Highest Rank" SortExpression="HRANK" ControlStyle-Width="45%">
                                <InsertItemTemplate>
                                    <asp:DropDownList ID="ddlHRANK" runat="server" DataTextField="Rank" DataValueField="Abbreviation"/>
                                </InsertItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <RowStyle HorizontalAlign="Left" />
                    </asp:DetailsView>
                </asp:View>
            </asp:MultiView>
    
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [Pensions]" 
                InsertCommand="INSERT INTO [Pensions] ([COMPONENT], [RANK], [HRANK]) VALUES (@COMPONENT, @RANK, @HRANK)" >
                <InsertParameters>
                    <asp:Parameter Name="COMPONENT" Type="String" />
    <asp:Parameter Name="RANK" Type="String" /> <asp:Parameter Name="HRANK" Type="String" /> </InsertParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT * FROM [States]" > </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT * FROM [AirRank]" > </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT * FROM [ArmyRank]" > </asp:SqlDataSource> </div> <br /> &nbsp; <br /> </asp:Content>

    Initial Data Entry markup:

        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    
            Dim strBranch As String = ddlBranch.SelectedItem.Text
            Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            If strBranch <> "" Then
                If strBranch = "Air" Then
                    lblComp.Text = "SCANG"
                    ddlRnk.DataSourceID = "SqlDataSource3"
                    ddlHRnk.DataSourceID = "SqlDataSource3"
                Else
                    lblComp.Text = "SCARNG"
                    ddlRnk.DataSourceID = "SqlDataSource4"
                    ddlHRnk.DataSourceID = "SqlDataSource4"
                End If
                MultiView1.SetActiveView(View2)
            End If
    
        End Sub
    
        Protected Sub Record_Inserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
    
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            e.Values("Rank") = ddlRnk.SelectedValue
            e.Values("HRank") = ddlHRnk.SelectedValue
    
        End Sub
    

    Update display:

            <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%"
                AutoGenerateRows="False" DataSourceID="SqlDataSource1" HorizontalAlign="Center"
                DataKeyNames="SSN" EnableModelValidation="True" DefaultMode="Edit" >
                <Fields>
                    <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT">
                        <ItemTemplate>
                            <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rank" SortExpression="RANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:HiddenField ID="hdnRank" runat="server" Value='<%# Bind("RANK")%>'/>
                            <asp:DropDownList ID="ddlRANK" runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Highest Rank" SortExpression="HRANK" ControlStyle-Width="45%">
                        <EditItemTemplate>
                            <asp:HiddenField ID="hdnHRank" runat="server" Value='<%# Bind("HRANK")%>'/>
                            <asp:DropDownList ID="ddlHRANK" runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" ItemStyle-HorizontalAlign="Center" >
                    </asp:CommandField>
                    <asp:TemplateField ShowHeader="False" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:LinkButton ID="DelButton1" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this entry?')" ></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Fields>
                <RowStyle HorizontalAlign="Left" />
            </asp:DetailsView>
    
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connString %>" 
            SelectCommand="SELECT * FROM [Pensions] WHERE [SSN] = @SSN" 
            DeleteCommand="INSERT INTO [RecDel] SELECT * FROM [Pensions] WHERE [SSN] = @SSN; DELETE FROM [Pensions] WHERE [SSN] = @SSN"
            UpdateCommand="UPDATE [Pensions] SET [COMPONENT] = @COMPONENT, [RANK] = @RANK, [HRANK] = @HRANK  WHERE SSN = @SSN" >
            <SelectParameters>
                <asp:QueryStringParameter Name="SSN" QueryStringField="SSN" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="COMPONENT" Type="String" />
                <asp:Parameter Name="RANK" Type="String" />
                <asp:Parameter Name="HRANK" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connString %>" 
            SelectCommand="SELECT * FROM [States]" >
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connString %>" 
            SelectCommand="SELECT * FROM [AirRank]" >
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connString %>" 
            SelectCommand="SELECT * FROM [ArmyRank]" >
        </asp:SqlDataSource>
        </div>
        <br />
            &nbsp;
        <br />
    </asp:Content>
    
    

    Update markup:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                Dim lblComp As Label = DetailsView1.FindControl("lblCmpt")
                Dim hdnRnk As HiddenField = DetailsView1.FindControl("hdnRank")
                Dim hdnHRnk As HiddenField = DetailsView1.FindControl("hdnHRank")
                Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
                Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
                If lblComp.Text = "SCANG" Then
                    ddlRnk.DataSourceID = "SqlDataSource3"
                    ddlHRnk.DataSourceID = "SqlDataSource3"
                Else
                    ddlRnk.DataSourceID = "SqlDataSource4"
                    ddlHRnk.DataSourceID = "SqlDataSource4"
                End If
    
                ddlRnk.SelectedValue = hdnRnk.Value
                ddlRnk.DataValueField = "Abbreviation"
                ddlRnk.DataTextField = "Rank"
                ddlRnk.DataBind()
                ddlHRnk.SelectedValue = hdnHRnk.Value
                ddlHRnk.DataValueField = "Abbreviation"
                ddlHRnk.DataTextField = "Rank"
                ddlHRnk.DataBind()
            End If
    
        End Sub
    
        Protected Sub DetailsView1_ItemUpdating(sender As Object, ByVal e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
    
            Dim ddlRnk As DropDownList = DetailsView1.FindControl("ddlRank")
            Dim ddlHRnk As DropDownList = DetailsView1.FindControl("ddlHRank")
    
            e.NewValues("RANK") = ddlRnk.SelectedValue
    
            e.NewValues("HRANK") = ddlHRnk.SelectedValue
    
    
        End Sub
    

    There is obviously more code than I am showing but the above is pertinent to my issue, selecting between two tables to populate one DropDownList.

    Again, thank you all so very much; works like a charm.

    Thursday, June 1, 2017 8:58 PM