• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Continued Query Problems.

Alright, in followup to my last question (which was beautifully answered) at : http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21843012.html

I am having an issue with getting the cmbEditRole/cmbEditState DropDownLists to display the currently set values when they load in edit mode. Below is my ASPX code and C# code-behind.

<%@ Control Language="C#" CodeFile="CreateEmployee.ascx.cs" Inherits="System.Web.Html.CreateEmployee" %>
<asp:SqlDataSource ID="employeeData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     DeleteCommand="DELETE FROM [Employees] WHERE [id] = @id" InsertCommand="INSERT INTO [Employees] ([firstname], [lastname], [role], [phone], [address], [city], [state], [zipcode], [password]) VALUES (@firstname, @lastname, @role, @phone, @address, @city, @state, @zipcode, @password)"
     SelectCommand="SELECT Employees.id, Employees.firstname, Employees.lastname, Employees.role, Employees.phone, Employees.address, Employees.city, Employees.state, Employees.zipcode, Employees.password, Roles.roleName, States.stateName FROM Employees INNER JOIN Roles ON Employees.role = Roles.roleid INNER JOIN States ON Employees.state = States.stateid"
     UpdateCommand="UPDATE [Employees] SET [firstname] = @firstname, [lastname] = @lastname, [role] = @role, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zipcode] = @zipcode, [password] = @password WHERE [id] = @id">
     <DeleteParameters>
          <asp:Parameter Name="id" Type="Int32" />
     </DeleteParameters>
     <UpdateParameters>
          <asp:Parameter Name="firstname" Type="String" />
          <asp:Parameter Name="lastname" Type="String" />
          <asp:Parameter Name="role" Type="Int32" />
          <asp:Parameter Name="phone" Type="String" />
          <asp:Parameter Name="address" Type="String" />
          <asp:Parameter Name="city" Type="String" />
          <asp:Parameter Name="state" Type="Int32" />
          <asp:Parameter Name="zipcode" Type="Int32" />
          <asp:Parameter Name="password" Type="String" />
          <asp:Parameter Name="id" Type="Int32" />
     </UpdateParameters>
     <InsertParameters>
          <asp:Parameter Name="firstname" Type="String" />
          <asp:Parameter Name="lastname" Type="String" />
          <asp:Parameter Name="role" Type="Int32" />
          <asp:Parameter Name="phone" Type="String" />
          <asp:Parameter Name="address" Type="String" />
          <asp:Parameter Name="city" Type="String" />
          <asp:Parameter Name="state" Type="Int32" />
          <asp:Parameter Name="zipcode" Type="Int32" />
          <asp:Parameter Name="password" Type="String" />
     </InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="roleData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     SelectCommand="SELECT [roleid], [roleName], [roleLevel] FROM [Roles]"></asp:SqlDataSource>
<asp:SqlDataSource ID="stateData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     SelectCommand="SELECT [stateid], [stateName] FROM [States]"></asp:SqlDataSource>
<asp:DetailsView ID="dataView" runat="server" AllowPaging="True" AutoGenerateRows="False"
     DataKeyNames="id" DataSourceID="employeeData" Height="50px" Width="125px" OnItemUpdating="OnItemUpdate">
     <Fields>
          <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
               SortExpression="id" />
          <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
          <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
          <asp:TemplateField HeaderText="role" SortExpression="role">
               <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" >
                    </asp:DropDownList>
               </EditItemTemplate>
               <InsertItemTemplate>
                    <asp:DropDownList ID="cmbInsertRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" >
                    </asp:DropDownList>
               </InsertItemTemplate>
               <ItemTemplate>
                    <asp:Label ID="lbTextRoleName" runat="server" Text='<%# Bind("roleName") %>'></asp:Label>
               </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
          <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
          <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
          <asp:TemplateField HeaderText="state" SortExpression="state">
               <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditState" runat="server" AutoPostBack="True" DataSourceID="stateData"
                         DataTextField="stateName" DataValueField="stateid" >
                    </asp:DropDownList>
               </EditItemTemplate>
               <InsertItemTemplate>
                    <asp:DropDownList ID="cmbInsertState" runat="server" AutoPostBack="True" DataSourceID="stateData"
                         DataTextField="stateName" DataValueField="stateid" >
                    </asp:DropDownList>
               </InsertItemTemplate>
               <ItemTemplate>
                    <asp:Label ID="lbTextStateName" runat="server" Text='<%# Bind("stateName") %>'></asp:Label>
               </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="zipcode" HeaderText="zipcode" SortExpression="zipcode" />
          <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
          <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
     </Fields>
</asp:DetailsView>


using System;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace System.Web.Html
{
      /// <summary>
      /// Represents the usercontrol to create a new employee.
      /// </summary>
      public partial class CreateEmployee : System.Web.UI.UserControl
      {
            /// <summary>
            /// Raises the DetailsView.ItemUpdate event.
            /// </summary>
            /// <param name="sender">Object that raised the event.</param>
            /// <param name="e">DetailsViewUpdateEventArgs for handling the event.</param>
            public void OnItemUpdate(object sender, DetailsViewUpdateEventArgs e)
            {
                  //employeeData is the name of your sqlDataSource bounded to the detailsview
                  employeeData.UpdateParameters["role"].DefaultValue = ((DropDownList)dataView.Rows[3].Cells[1].Controls[1]).SelectedValue;
                  employeeData.UpdateParameters["state"].DefaultValue = ((DropDownList)dataView.Rows[7].Cells[1].Controls[1]).SelectedValue;
            }

            /// <summary>
            /// Raises the DetailsView.ItemInsert event.
            /// </summary>
            /// <param name="sender">Object that raised the event.</param>
            /// <param name="e">DetailsViewInsertEventArgs for handling the event.</param>
            protected void OnItemInsert(object sender, DetailsViewInsertEventArgs e)
            {
                  //employeeData is the name of your sqlDataSource bounded to the detailsview
                  employeeData.UpdateParameters["role"].DefaultValue = ((DropDownList)dataView.Rows[3].Cells[1].Controls[2]).SelectedValue;
                  employeeData.UpdateParameters["state"].DefaultValue = ((DropDownList)dataView.Rows[7].Cells[1].Controls[2]).SelectedValue;
            }
      }
}
0
developernetwork
Asked:
developernetwork
1 Solution
 
bele04Commented:
Hi,

you can try this:
 <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" SelectedValue='<%# Eval("roleID")%>' >
                    </asp:DropDownList>
 </EditItemTemplate>

and change the SelectCommand of your main sqlDatasource (employeeData)...add the roleID and stateid column to your selectcommand.  So what this does is it'll select the item in the dropdownlist whose value is equal to the current role or state.  if the current role or state is not found in the dropdownlist it will default to the first item in the dropdownlist.

hope it helps...
-bele04-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now