qhnokia

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
代码
<%@ Page Language="C#" MasterPageFile="~/master/default.master" AutoEventWireup="true"
CodeFile
="details-page.aspx.cs" Inherits="Code_Sample_Details_Page" Title="Employee Details Page" %>

<%@ Register Assembly="App_Code" Namespace="Utilities" TagPrefix="CustomControl" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceholder1" runat="Server">

<script src="../client-scripts/date-picker.js" type="text/javascript">

</script>

<noscript>
</noscript>
<asp:FormView SkinID="FormView" ID="formViewEmployee" runat="server" DataSourceID="odsOrderDetails"
DataKeyNames
="EmployeeId" EnableViewState="False" OnItemUpdating="FormViewEmployee_ItemUpdating"
OnItemInserting
="FormViewEmployee_ItemInserting" OnDataBound="FormViewEmployee_DataBound">
<ItemTemplate>
<table width="560" id="tblItem">
<caption>
</caption>
<thead>
<tr>
<th class="invisible" title="Item template">
</th>
</tr>
</thead>
<tr>
<td colspan="4" class="printCommandTBL">
<asp:LinkButton ID="LinkButton1" OnClick="ButtonGoToPrintPage_Click" runat="server">[ Print Info ]</asp:LinkButton>
&nbsp;
</td>
</tr>
<tr>
<td colspan="4">
<h1>
Employee Details
</h1>
</td>
</tr>
<tr>
<td class="captionTBL">
Name
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("FirstName") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Country
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("Country") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Title
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("Title") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Title Of Courtesy
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("TitleOfCourtesy") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Birth Date
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("BirthDate") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Hire Date
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("HireDate") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Address
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("Address") %>
</td>
</tr>
<tr>
<td class="captionTBL">
City
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("City") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Region
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("Region") %>
</td>
</tr>
<tr>
<td class="captionTBL">
Postal Code
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("PostalCode")%>
</td>
</tr>
<tr>
<td class="captionTBL">
Home Phone
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("HomePhone")%>
</td>
</tr>
<tr>
<td class="captionTBL">
Extension
</td>
<td class="spacerTBL">
:
</td>
<td class="datafieldTBL">
<%# Eval("Extension")%>
</td>
</tr>
<tr>
<td class="captionTBLTop">
Notes
</td>
<td class="spacerTBLTop">
:
</td>
<td class="datafieldTBL">
<%# Eval("Notes") %>
</td>
</tr>
<tr>
<td class="captionTBLTop">
Picture
</td>
<td class="spacerTBLTop">
:
</td>
<td class="datafieldTBL">
<img id="Img1" alt="employee image" runat="server" src='<%# "image.aspx?ImageBinary=" +Eval("employeeId").ToString() %>'
width="200" />
</td>
</tr>
<tr>
<td colspan="4">
&nbsp;
</td>
</tr>
<tr>
<td class="dataCommandTBL" colspan="4">
<asp:Button CausesValidation="false" ID="btnBack" runat="server" Text="Back" OnClick="ButtonGoToListPage_Click"
SkinID
="AspButton" />
<asp:Button ID="Button2" runat="server" Text="Edit" OnClick="ButtonEdit_Click" SkinID="AspButton" />
</td>
</tr>
<tr>
<td class="printCommandTBL" colspan="4">
<asp:LinkButton ID="LinkButton4" OnClick="ButtonGoToPrintPage_Click" runat="server">[ Print Info ]</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table width="560" id="tblEdit">
<caption>
</caption>
<thead>
<tr>
<th class="invisible" title="Edit template">
</th>
</tr>
</thead>
<tr>
<td class="printCommandTBL" colspan="3">
<asp:LinkButton ID="lbPrint" OnClick="ButtonGoToPrintPage_Click" runat="server" AccessKey="P">[ Print Info ]</asp:LinkButton>
</td>
</tr>
<tr>
<td colspan="3">
<h1>
Employee Details
</h1>
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label EnableViewState="False" runat="server" ID="lblMessage" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td class="requiredLabelTBL" colspan="3" style="height: 18px">
<span>* indicates required field.</span>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>First Name:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtFirstName" Text='<%# Bind("FirstName") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ErrorMessage="Required"
ControlToValidate
="txtFirstName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>Last Name:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtLastName" Text='<%# Bind("LastName") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Required"
ControlToValidate
="txtLastName"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>Country:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL">
<CustomControl:CountryDropDown SelectedValue='<%# Bind("Country") %>' runat="server"
ID="ddlCountry">
</CustomControl:CountryDropDown>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required"
ControlToValidate
="ddlCountry" InitialValue="-1"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
Reports To:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL">
<asp:DropDownList ID="ddlReportsTo" runat="server" AppendDataBoundItems="true" DataSourceID="odsEmployeeList"
SelectedValue
='<%# Bind("ReportsTo") %>' DataTextField="FirstName" DataValueField="EmployeeId">
<asp:ListItem Text="None" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="odsEmployeeList" runat="server" TypeName="Entity.Employee"
SelectMethod
="SelectAllEmployees" />
</td>
</tr>
<tr>
<td class="captionTBL">
Title:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtTitle" Text='<%# Bind("Title") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
Title Of Courtesy:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtTitleOfCourtesy" Text='<%# Bind("TitleOfCourtesy") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
Birth Date:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtnewBirthDate" runat="server" Text='<%# Bind("BirthDate")%>'></asp:TextBox>
<a href="#" onclick="javascript:displayDatePicker('<%=formViewEmployee.FindControl("txtnewBirthDate").ClientID %>');return false;">
<img style="border: 0px none;" src="../image/cal.jpg" alt="calculator" />
</a>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>Hire Date:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtHireDate" Text='<%# Bind("HireDate") %>' runat="server"></asp:TextBox>
<a href="#" onclick="javascript:displayDatePicker('<%=formViewEmployee.FindControl("txtHireDate").ClientID %>');return false;">
<img style="border: 0px none;" src="../image/cal.jpg" alt="calculator" />
</a>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Required"
ControlToValidate
="txtHireDate"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>Address:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtAddress" Text='<%# Bind("Address") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required"
ControlToValidate
="txtAddress"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
City:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtCity" Text='<%# Bind("City") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
Region:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtRegion" Text='<%# Bind("Region") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
Postal Code:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtPostalCode" Text='<%# Bind("PostalCode") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBLTop">
Notes:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtNotes" Height="100" Width="400px" Text='<%# Bind("Notes") %>'
runat="server" TextMode="MultiLine">
</asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
<span class="requiredLabelCaptionTBL">* </span>Home Phone:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtHomePhone" Text='<%# Bind("HomePhone") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Required"
ControlToValidate
="txtHomePhone"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="captionTBL">
Extension:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="txtExtension" Text='<%# Bind("Extension") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL">
Photo Path:
</td>
<td class="spacerTBL">
</td>
<td class="datafieldTBL" align="left">
<asp:TextBox ID="TextBox2" Text='<%# Bind("PhotoPath") %>' runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="captionTBL" style="height: 24px">
Picture
</td>
<td class="spacerTBL" style="height: 24px">
</td>
<td class="datafieldTBL" style="height: 24px">
<asp:FileUpload ID="fuPhotoUpload" runat="server"></asp:FileUpload>
</td>
</tr>
<tr>
<td>
<br />
</td>
</tr>
<tr>
<td class="dataCommandTBL" colspan="3">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" SkinID="AspButton"
AccessKey
="s" />
<asp:Button ID="btnBack" CausesValidation="false" runat="server" Text="Back" OnClick="ButtonGoToListPage_Click"
SkinID
="AspButton" AccessKey="b" />
</td>
</tr>
<tr>
<td class="printCommandTBL" colspan="3">
<asp:LinkButton ID="LinkButton5" OnClick="ButtonGoToPrintPage_Click" runat="server">[ Print Info ]</asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="odsOrderDetails" runat="server" TypeName="Entity.Employee"
SelectMethod
="GetEmployeeByEmployeeId" InsertMethod="CreateNewEmployee" UpdateMethod="UpdateEmployee"
DataObjectTypeName
="Entity.Employee" OnSelecting="OdsOrderDetails_Selecting"
OnUpdated
="OdsOrderDetails_Updated" OnInserted="OdsOrderDetails_Inserted">
<UpdateParameters>
<asp:ControlParameter Name="employeeId" ControlID="formViewEmployee" />
<asp:Parameter Name="title" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="titleOfCourtesy" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="birthDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="city" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="region" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="postalCode" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="extension" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="photo" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="notes" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="reportsTo" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="photoPath" ConvertEmptyStringToNull="true" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="title" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="titleOfCourtesy" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="birthDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="city" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="region" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="postalCode" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="extension" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="photo" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="notes" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="reportsTo" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="photoPath" ConvertEmptyStringToNull="true" />
</InsertParameters>
<SelectParameters>
<asp:Parameter Name="employeeId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Content>

 

代码
using System;
using System.Web.UI.WebControls;
using Utilities;

public partial class Code_Sample_Details_Page : System.Web.UI.Page
{
#region "Data Operation and Navigation Command Handlers"
protected void ButtonSave_Click(object sender, EventArgs e)
{
if (formViewEmployee.CurrentMode == FormViewMode.Insert)
{
formViewEmployee.InsertItem(
true);
}
else
{
formViewEmployee.UpdateItem(
true);
}
}


protected void ButtonEdit_Click(object sender, EventArgs e)
{
formViewEmployee.ChangeMode(FormViewMode.Edit);
}

protected void ButtonGoToListPage_Click(object sender, EventArgs e)
{
Response.Redirect(
"listing-page.aspx");
}

protected void ButtonGoToPrintPage_Click(object sender, EventArgs e)
{
if (formViewEmployee.CurrentMode != FormViewMode.Insert)
{
string script = "window.open('print-page.aspx','')";
if (!ClientScript.IsClientScriptBlockRegistered("NewWindow"))
{
ClientScript.RegisterClientScriptBlock(
this.GetType(), "NewWindow", script, true);
}
}
}
#endregion

#region "Select handlers"
protected void OdsOrderDetails_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
{
//since the select parameter contains a special type of parameter, rather than the primitive type
//we can't use the built-in asp:paramter in object data-source, as it supports only primitive data types
//for this purpose we have performed the required casting and assignment (if necessary) in this event handler method
//however alternatively we can use 'control-parameter' in object-data-source for this type of special data type, which performs the casting automatically
//e.InputParameters("employeeID") = CType(1,Int32)

//saving page-mode and item id
//int empId = (int)uint.MinValue;
//if(e.InputParameters["employeeId"]!=null)
// empId =Int32.Parse(e.InputParameters["employeeId"].ToString());

//empId = WebManager.StoreValue(this, ViewState, empId, formViewEmployee);

// [ Added: Jalal On 03/17/2008 ]
//e.InputParameters["employeeId"] = Int32.Parse(ViewState["Id"].ToString(), System.Globalization.CultureInfo.CurrentCulture);
e.InputParameters["employeeId"] = WebManager.ParseItemId(this,formViewEmployee);
//e.InputParameters["employeeId"] = Convert.ToInt32(WebManager.ParseItemId(this, formViewEmployee));
//if the form view mode is in insert model, then we'll cancel the selection by object data source
if (formViewEmployee.CurrentMode == FormViewMode.Insert)
e.Cancel
= true;
}
#endregion

#region "Update handlers"

protected void FormViewEmployee_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (!String.IsNullOrEmpty(((FileUpload)this.formViewEmployee.FindControl("fuPhotoUpload")).FileName))
e.NewValues[
"photo"] = ((FileUpload)this.formViewEmployee.FindControl("fuPhotoUpload")).FileBytes;
else
e.NewValues[
"photo"] = null;
}

protected void OdsOrderDetails_Updated(object sender, ObjectDataSourceStatusEventArgs e)
{

//getting the result
bool result = Convert.ToBoolean(e.ReturnValue, System.Globalization.CultureInfo.CurrentCulture.NumberFormat);

if (result)
{
Message
= "Update successful.";
//'We can ignore the line below, if we need to return just after the updation operation
formViewEmployee.DefaultMode = FormViewMode.Edit;
}
else
{
Message
= "Error while updating.";
}
}

#endregion

#region "Insert handlers"

protected void FormViewEmployee_ItemInserting(object sender, FormViewInsertEventArgs e)
{
if (!String.IsNullOrEmpty(((FileUpload)this.formViewEmployee.FindControl("fuPhotoUpload")).FileName))
{
e.Values[
"photo"] = ((FileUpload)this.formViewEmployee.FindControl("fuPhotoUpload")).FileBytes;
}
else
{
e.Values[
"photo"] = null;
}
}

protected void OdsOrderDetails_Inserted(object sender, System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs e)
{
//getting the result
int result = Convert.ToInt32(e.ReturnValue,System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
if (result != 0)
{
Message
= "Insert successful.";
//We can ignore the line below, if we need to return just after the insertion operation
WebManager.UpdateItemId(result.ToString(System.Globalization.CultureInfo.CurrentCulture.NumberFormat), formViewEmployee);
}
else
Message
= "Error while inserting.";
}

#endregion

#region "Populating and binding special controls"

//customzed data operations over databound controls in form view
protected void FormViewEmployee_DataBound(object sender, System.EventArgs e)
{
TextBox txtFirstName
= (TextBox)formViewEmployee.FindControl("txtFirstName");
TextBox txtLastName
= (TextBox)formViewEmployee.FindControl("txtLastName");
TextBox txtHireDate
= (TextBox)formViewEmployee.FindControl("txtHireDate");
TextBox txtAddress
= (TextBox)formViewEmployee.FindControl("txtAddress");
TextBox txtHomePhone
= (TextBox)formViewEmployee.FindControl("txtHomePhone");
DropDownList ddlCountry
= (DropDownList)formViewEmployee.FindControl("ddlCountry");

//since in read-only mode there is no text box control
if (formViewEmployee.CurrentMode != FormViewMode.ReadOnly)
{

//using data value in form in custom way
if (formViewEmployee.CurrentMode == FormViewMode.Edit)
{
Entity.Employee employee
= (Entity.Employee)formViewEmployee.DataItem;
}

//populating per-fill data
if (formViewEmployee.CurrentMode == FormViewMode.Insert)
{
txtFirstName.Text
= "Ashraful";
txtLastName.Text
= "Alam";
txtHireDate.Text
= DateTime.Now.ToString();
txtAddress.Text
= "One Microsoft Way";
txtHomePhone.Text
= "912200022";
ddlCountry.Items.FindByText(
"USA").Selected = true;
}
}

//binding last data operation message
Label lblMessage = (Label)formViewEmployee.FindControl("lblMessage");
if (lblMessage != null)
{
lblMessage.Text
= Message;
}
}
#endregion

string _message = String.Empty;
public string Message
{
get { return _message; }
set { _message = value; }
}


}

 

posted on 2010-02-24 16:40  其乐无穷  阅读(257)  评论(0编辑  收藏  举报