Home > OS >  ASP.NET GridView with DRopDown list check if user updated/ changed the dropdown selection
ASP.NET GridView with DRopDown list check if user updated/ changed the dropdown selection

Time:01-28

I have a GridView that the user can edit, in particular a datafield (MemberApproved) is displayed as a dropdown list when edited.

   <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" CssClass="gridview" AllowSorting="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" >

    
     <HeaderStyle  Font-Bold="True" ForeColor="White" />
  
    <Columns>
    <asp:CommandField ShowEditButton="True" />
    <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
    <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
    <asp:BoundField DataField="Affiliation" HeaderText="Affiliation" SortExpression="Affiliation" />
    <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ReadOnly="True" />
    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
    <asp:BoundField DataField="MembershipCategory" HeaderText="Membership Category" SortExpression="MembershipCategory" />

        <asp:TemplateField HeaderText="MemberApproved" SortExpression="MemberApproved">

              <EditItemTemplate>
                  <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("MemberApproved") %>'></asp:HiddenField>

                  <asp:DropDownList ID="ddlStatus" runat="server"
                          SelectedValue='<%# Bind("MemberApproved") %>'>

                      <asp:ListItem Value="Yes">Yes</asp:ListItem>
                      <asp:ListItem Value="No">No</asp:ListItem>
                      <asp:ListItem Value="Locked">Locked</asp:ListItem>
                  </asp:DropDownList>
              </EditItemTemplate>
              <ItemTemplate>
                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("MemberApproved") %>'></asp:Label>
              </ItemTemplate>
          </asp:TemplateField>


        <asp:BoundField DataField="SupportingMember" HeaderText="Reference Member" SortExpression="SupportingMember" />
        <asp:BoundField DataField="ReferenceEmail" HeaderText="Reference Email" SortExpression="ReferenceEmail" />

    </Columns>
    
     <HeaderStyle CssClass="fixedHeader " />
  
</asp:GridView>

I am trying to capture if the user changes the "MemberApproved" field, in the . I am able to capture the updated new value using the code below

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatus");
        string NewSelection = ddl.SelectedValue;
        
       

    }

I am however unable to hold the initial value of the dropdownlist in a variable to compare it to the NewSelection. Any thoughts or suggestion to different approaches are greatly appreciated.

CodePudding user response:

You're using a template field already. Why not store your value as part of a hiddenfield and then compare to it?

<asp:TemplateField HeaderText="MemberApproved" SortExpression="MemberApproved">

              <EditItemTemplate>
                  <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("MemberApproved") %>'></asp:HiddenField>

                  <asp:DropDownList ID="ddlStatus" runat="server"
                          SelectedValue='<%# Bind("MemberApproved") %>'>

                      <asp:ListItem Value="Yes">Yes</asp:ListItem>
                      <asp:ListItem Value="No">No</asp:ListItem>
                      <asp:ListItem Value="Locked">Locked</asp:ListItem>
                  </asp:DropDownList>
              </EditItemTemplate>
              <ItemTemplate>
                  <asp:Label ID="Label1" runat="server" Text='<%# Bind("MemberApproved") %>'></asp:Label>
                  <asp:hiddenField ID="label1History" runat="server" value='<%# Bind("MemberApproved") %>'
              </ItemTemplate>
          </asp:TemplateField>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatus");
        HiddenField labelHistory = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("label1History");
        string NewSelection = ddl.SelectedValue;

        Boolean changedValue = NewSelection = labelHistory.value;
        
       

    }
  • Related