(C#) ASP.NET DetailsView Control - Oracle Database - System.Data.OracleClient |
(C#) ASP.NET DetailsView & Oracle (Framework 2.0,3.5) เป็นตัวอย่างบทความการเรียกใช้งาน DetailsView กับฐานข้อมูล Oracle ผ่าน NameSpace ชื่อ System.Data.OracleClient ซึ่งไว้สำหรับจัดการกับฐานข้อมูล Oracle โดยเฉพาะ ในตัวอย่างนี้ผมได้ยกการเขียนร่วมกับ Visual Studio 2005,2008 ซึ่ง Run บน Framework 2.0,3.5 ในรูปแบบของ Code-Behind แยกในส่วนของ Tag และ Code ไว้คนล่ะส่วน และใช้การจัดการ Control ผ่าน Code-Behind ทั้งหมด
Language Code : VB.NET || C#
Framework : 2,3,4
DetailsView1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsView1.aspx.cs" Inherits="DetailsView1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ThaiCreate.Com ASP.NET - DetailsView & Oracle</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DetailsView id="myDetailsView" runat="server"
AutoGenerateRows="False"
OnItemInserting="myDetailsView_ItemInserting"
OnItemUpdating="myDetailsView_ItemUpdating"
OnModeChanging="myDetailsView_ModeChanging">
<Fields>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<asp:Label id="lblCustomerID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCustomerID" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerID") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddCustomerID" size="5" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditName" size="10" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddName" size="10" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditEmail" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddEmail" size="20" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CountryCode">
<ItemTemplate>
<asp:Label id="lblCountryCode" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditCountryCode" size="2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CountryCode") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddCountryCode" size="2" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label id="lblBudget" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditBudget" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Budget") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddBudget" size="6" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Used">
<ItemTemplate>
<asp:Label id="lblUsed" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="txtEditUsed" size="6" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Used") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox id="txtAddUsed" size="6" runat="server"></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
</form>
</body>
</html>
DetailsView1.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient;
public partial class DetailsView1 : System.Web.UI.Page
{
OracleConnection objConn;
OracleCommand objCmd;
String strSQL;
String strCusID = "C001"; //*** Request.QueryString["CusID"] ***//
protected void Page_Load(object sender, EventArgs e)
{
String strConnString;
strConnString = "Data Source=TCDB;User Id=myuser;Password=mypassword;";
objConn = new OracleConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM customer WHERE CustomerID = '" + strCusID + "' ";
OracleDataReader dtReader;
objCmd = new OracleCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to DetailsView ***//
myDetailsView.DataSource = dtReader;
myDetailsView.DataBind();
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
protected void myDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)(myDetailsView.FindControl("txtAddCustomerID"));
//*** Name ***//
TextBox txtName = (TextBox)(myDetailsView.FindControl("txtAddName"));
//*** Email ***//
TextBox txtEmail = (TextBox)(myDetailsView.FindControl("txtAddEmail"));
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)(myDetailsView.FindControl("txtAddCountryCode"));
//*** Budget ***//
TextBox txtBudget = (TextBox)(myDetailsView.FindControl("txtAddBudget"));
//*** Used ***//
TextBox txtUsed = (TextBox)(myDetailsView.FindControl("txtAddUsed"));
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " +
" VALUES ('" + txtCustomerID.Text + "','" + txtName.Text + "','" + txtEmail.Text + "' " +
" ,'" + txtCountryCode.Text + "','" + txtBudget.Text + "','" + txtUsed.Text + "') ";
objCmd = new OracleCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
strCusID = txtCustomerID.Text;
myDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
BindData();
}
protected void myDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
//*** CustomerID ***//
TextBox txtCustomerID = (TextBox)(myDetailsView.FindControl("txtEditCustomerID"));
//*** Name ***//
TextBox txtName = (TextBox)(myDetailsView.FindControl("txtEditName"));
//*** Email ***//
TextBox txtEmail = (TextBox)(myDetailsView.FindControl("txtEditEmail"));
//*** CountryCode ***//
TextBox txtCountryCode = (TextBox)(myDetailsView.FindControl("txtEditCountryCode"));
//*** Budget ***//
TextBox txtBudget = (TextBox)(myDetailsView.FindControl("txtEditBudget"));
//*** Used ***//
TextBox txtUsed = (TextBox)(myDetailsView.FindControl("txtEditUsed"));
strSQL = "UPDATE customer SET CustomerID = '" + txtCustomerID.Text + "' " +
" ,Name = '" + txtName.Text + "' " +
" ,Email = '" + txtEmail.Text + "' " +
" ,CountryCode = '" + txtCountryCode.Text + "' " +
" ,Budget = '" + txtBudget.Text + "' " +
" ,Used = '" + txtUsed.Text + "' " +
" WHERE CustomerID = '" + strCusID + "'";
objCmd = new OracleCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
myDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
BindData();
}
protected void myDetailsView_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
switch (e.NewMode)
{
case DetailsViewMode.Edit:
myDetailsView.ChangeMode(DetailsViewMode.Edit);
break;
case DetailsViewMode.ReadOnly:
myDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
break;
case DetailsViewMode.Insert:
myDetailsView.ChangeMode(DetailsViewMode.Insert);
break;
}
BindData();
}
}
Screenshot
ASP.NET & System.Data.OracleClient
ASP.NET & Oracle
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
|
|
|
Create/Update Date : |
2009-09-22 19:33:13 /
2017-03-28 21:34:52 |
|
Download : |
|
|
Sponsored Links / Related |
|
|
|
|
|
|
|