I am trying to pass a preselected value to a boundfield within a DetailsView, but I cannot figure out how to do this. Here is the scenario:
A user needs to add an employment history for one of our clients. The user selects the client and is taken to the clients details page. From here, the user selects 'Employment History', and is taken to the a page with a DetailsView control for adding employment history.
The client ID (WhoID) must be added to the list.
I would prefer a boundfield, or read-only text box that would recieve the WhoID from the previous page, but I cannot get this to work inside a DetailsView control. As a result, the only way I can get the WhoID into the control is make one of the fields a dropdown list. This works fine for Insert mode even though it is redundant in that the user has already 'selected' the client. But in Update mode, since the WhoID is already present, I get errors. If I include the WhoID as a parameter passed, I get errors in SQL server. If I exclude the WhoID, I get ASP.NET errors, presumably because the dropdown list is enabled, so ASP.NET is attempting to pass a parameter not asked for.
Here is the code as it stands now:
A user needs to add an employment history for one of our clients. The user selects the client and is taken to the clients details page. From here, the user selects 'Employment History', and is taken to the a page with a DetailsView control for adding employment history.
The client ID (WhoID) must be added to the list.
I would prefer a boundfield, or read-only text box that would recieve the WhoID from the previous page, but I cannot get this to work inside a DetailsView control. As a result, the only way I can get the WhoID into the control is make one of the fields a dropdown list. This works fine for Insert mode even though it is redundant in that the user has already 'selected' the client. But in Update mode, since the WhoID is already present, I get errors. If I include the WhoID as a parameter passed, I get errors in SQL server. If I exclude the WhoID, I get ASP.NET errors, presumably because the dropdown list is enabled, so ASP.NET is attempting to pass a parameter not asked for.
Here is the code as it stands now:
Code:
<asp:DetailsView ID="dvwEmpHistItem" runat="server" AutoGenerateDeleteButton="True" DefaultMode="Insert"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="ObjEmpHistItem"
OnModeChanged="dvwEmpHistItem_ModeChanged" Height="50px" Width="100%">
<FieldHeaderStyle Width="20%" />
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID:" ReadOnly="True" InsertVisible="False" />
<asp:BoundField DataField="AddedDate" HeaderText="AddedDate:" InsertVisible="False" ReadOnly="True"/>
<asp:BoundField DataField="AddedBy" HeaderText="AddedBy:" InsertVisible="False" ReadOnly="True"/>
<asp:TemplateField HeaderText="Client">
<ItemTemplate>
<asp:Label ID="lblClientName" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="WhoID" runat="server" DataSourceID="ObjSelectClient"
DataTextField="Title" DataValueField="ID" SelectedValue='<%# Bind("WhoID") %>' Width="100%"
AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="0">Select a Client</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjSelectClient" runat="server" SelectMethod="GetClients"
TypeName="NWC.CSWeb.BLL.Program.Client"> </asp:ObjectDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employer" SortExpression="Employer">
<ItemTemplate>
<asp:Label ID="lblEmployer" runat="server" Text='<%# Eval("Employer") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmployer" runat="server" Text='<%# Bind("Employer") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireEmployer" runat="server" ControlToValidate="txtEmployer" SetFocusOnError="true"
Text="The Employer field is required." ToolTip="The Employer field is required." Display="Dynamic"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supervisor" SortExpression="Supervisor">
<ItemTemplate>
<asp:Label ID="lblSupervisor" runat="server" Text='<%# Eval("Supervisor") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtSupervisor" runat="server" Text='<%# Bind("Supervisor") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireSupervisor" runat="server" ControlToValidate="txtSupervisor" SetFocusOnError="true"
Text="The Supervisor field is required." ToolTip="The Supervisor field is required." Display="Dynamic"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Job Title" SortExpression="JobTitle">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%# Eval("JobTitle") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireJobTitle" runat="server" ControlToValidate="txtJobTitle" SetFocusOnError="true"
Text="The Job Title field is required." ToolTip="The Job Title field is required." Display="Dynamic"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone Number" SortExpression="PhoneNo">
<ItemTemplate>
<asp:Label ID="lblPhoneNo" runat="server" Text='<%# Eval("PhoneNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPhoneNo" runat="server" Text='<%# Bind("PhoneNo") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequirePhoneNo" runat="server" ControlToValidate="txtPhoneNo" SetFocusOnError="true"
Text="The Phone Number field is required." ToolTip="The Phone Number field is required." Display="Dynamic"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monthly Hours" SortExpression="AvgMthHours">
<ItemTemplate>
<asp:Label ID="lblAvgMthHours" runat="server" Text='<%# Eval("AvgMthHours", "{0:N2}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAvgMthHours" runat="server" Text='<%# Bind("AvgMthHours", "{0:N2}") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireAvgMthHours" runat="server" ControlToValidate="txtAvgMthHours" SetFocusOnError="true"
Text="The Monthly Hours field is required." ToolTip="The Monthly Hours field is required." Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="valUnitPriceType" runat="server" Operator="DataTypeCheck" Type="Double"
ControlToValidate="txtAvgMthHours" Text="The Monthly Hours value must be numeric."
ToolTip="he Monthly Hours value must be numeric." Display="dynamic" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weekly Schedule" SortExpression="WkSched">
<ItemTemplate>
<asp:Label ID="lblWkSched" runat="server" Text='<%# Eval("WkSched") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWkSched" runat="server" Text='<%# Bind("WkSched") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireWkSched" runat="server" ControlToValidate="txtWkSched" SetFocusOnError="true"
Text="The Weekly Schedule field is required." ToolTip="The Weekly Schedule field is required." Display="Dynamic"></asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wage or Salary" SortExpression="WageSalary">
<ItemTemplate>
<asp:Label ID="lblWageSalary" runat="server" Text='<%# Eval("WageSalary", "{0:N2}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtWageSalary" runat="server" Text='<%# Bind("WageSalary", "{0:N2}") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator ID="valRequireWageSalary" runat="server" ControlToValidate="txtWageSalary" SetFocusOnError="true"
Text="The Wage or Salary field is required." ToolTip="The Wage or Salary field is required." Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="valWageSalary" runat="server" Operator="DataTypeCheck" Type="Currency"
ControlToValidate="txtWageSalary" Text="The Wage or Salary must be in dollars-decimal format."
ToolTip="The Wage or Salary must be in dollars-decimal format." Display="dynamic" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employment From Date">
<ItemTemplate>
<asp:Label ID="lblEmpFromDate" runat="server" Text='<%# Eval("EmpFromDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpFromDate" runat="server" Text='<%# Bind("EmpFromDate", "{0:d}") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:CompareValidator ID="valLastEmpFromDateType" runat="server" Operator="DataTypeCheck" Type="Date"
ControlToValidate="txtEmpFromDate" Text="The format of the Employment From Date field is not valid."
ToolTip="The format of the Employment From Date field is not valid." Display="dynamic" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employment To Date">
<ItemTemplate>
<asp:Label ID="lblEmpToDate" runat="server" Text='<%# Eval("EmpToDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmpToDate" runat="server" Text='<%# Bind("EmpToDate", "{0:d}") %>' Width="100%" MaxLength="256"></asp:TextBox>
<asp:CompareValidator ID="valLastEmpToDateType" runat="server" Operator="DataTypeCheck" Type="Date"
ControlToValidate="txtEmpToDate" Text="The format of the Employment To Date field is not valid."
ToolTip="The format of the Employment To Date field is not valid." Display="dynamic" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OT Available">
<ItemTemplate>
<asp:CheckBox ID="chkOTAVail" runat="server" Checked='<%# Eval("OTAVail") %>' Enabled="False" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkOTAVail" runat="server" Checked='<%# Bind("OTAVail") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description of Job" SortExpression="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Description") %>' Width="100%" MaxLength="256"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ObjEmpHistItem" runat="server" DeleteMethod="DeleteEmploymentHistory"
InsertMethod="InsertEmploymentHistory" SelectMethod="GetEmploymentHistoryByID"
TypeName="NWC.CSWeb.BLL.Program.EmploymentHistory" UpdateMethod="UpdateEmploymentHistory">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="whoID" Type="Int32" />
<asp:Parameter Name="employer" Type="String" />
<asp:Parameter Name="supervisor" Type="String" />
<asp:Parameter Name="jobTitle" Type="String" />
<asp:Parameter Name="phoneNo" Type="String" />
<asp:Parameter Name="avgMthHours" Type="Decimal" />
<asp:Parameter Name="wkSched" Type="String" />
<asp:Parameter Name="wageSalary" Type="Decimal" />
<asp:Parameter Name="oTAvail" Type="Boolean" />
<asp:Parameter Name="empFromDate" Type="DateTime" />
<asp:Parameter Name="empToDate" Type="DateTime" />
<asp:Parameter Name="description" Type="String" ConvertEmptyStringToNull="false" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="empHistID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="whoID" Type="Int32" />
<asp:Parameter Name="employer" Type="String" />
<asp:Parameter Name="supervisor" Type="String" />
<asp:Parameter Name="jobTitle" Type="String" />
<asp:Parameter Name="phoneNo" Type="String" />
<asp:Parameter Name="avgMthHours" Type="Decimal" />
<asp:Parameter Name="wkSched" Type="String" />
<asp:Parameter Name="wageSalary" Type="Decimal" />
<asp:Parameter Name="oTAvail" Type="Boolean" />
<asp:Parameter Name="empFromDate" Type="DateTime" />
<asp:Parameter Name="empToDate" Type="DateTime" />
<asp:Parameter Name="description" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>