Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... LogonUser Function : impersonate a windows user // This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT NOTES:  // This sample can be run only ...
Blog Archive
Stats
ObjectDataSource
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ObjectDataSourceSample.aspx.cs"
    Inherits="superspace.control.info.ObjectDataSourceSample" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HiddenField ID="hdnContentId" runat="server" />
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
            DeleteMethod="Delete"
            InsertMethod="Insert"
            SelectMethod="Select"
            UpdateMethod="Update"
            OldValuesParameterFormatString="{0}"
            TypeName="SuperspaceLib.VoteOption">
            <DeleteParameters>
                <asp:Parameter Name="ContentId" Type="String" />
                <asp:Parameter Name="OptionID" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="ContentId" Type="String" />
                <asp:Parameter Name="OptionID" Type="String" />
                <asp:Parameter Name="OptionName" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="hdnContentId" Name="ContentId" PropertyName="Value"
                    Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="ContentId" Type="String" />
                <asp:Parameter Name="OptionID" Type="String" />
                <asp:Parameter Name="OptionName" Type="String" />
            </UpdateParameters>
        </asp:ObjectDataSource>
        <asp:TextBox ID="txtVoteOption" runat="server" MaxLength="50" Style="width: 100%;" />
        <asp:Button ID="btnAddOption" runat="server" Text="新增" OnClick="btnAddOption_Click" />
        <asp:GridView ID="gvOptions" runat="server" AutoGenerateColumns="False" GridLines="None"
            ShowHeader="False" DataKeyNames="ContentId,OptionID" EnableModelValidation="True"
            DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:BoundField DataField="OptionName" HeaderText="OptionName" SortExpression="OptionName" />
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="編輯"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                            Text="刪除"></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                            Text="更新"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                            Text="取消"></asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Web;

namespace SuperspaceLib
{
    [DataObject(true)]
    public class VoteOption
    {
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataTable Select(string ContentId)
        {
            DataTable dtOption = null;
            if (HttpContext.Current.Session[ContentId] == null)
            {
                dtOption = new DataTable("VoteOption");
                dtOption.Columns.Add("ContentId");
                dtOption.Columns.Add("OptionID");
                dtOption.Columns.Add("OptionName");
            }
            else
            {
                dtOption = HttpContext.Current.Session[ContentId] as DataTable;
            }
            return dtOption;
        }

        [DataObjectMethod(DataObjectMethodType.Update)]
        public int Update(string ContentId, string OptionID, string OptionName)
        {
            DataTable dtOption = null;
            if (HttpContext.Current.Session[ContentId] == null)
            {
                dtOption = new DataTable("VoteOption");
                dtOption.Columns.Add("ContentId");
                dtOption.Columns.Add("OptionID");
                dtOption.Columns.Add("OptionName");
            }
            else
            {
                dtOption = HttpContext.Current.Session[ContentId] as DataTable;
            }

            int afftected = 0;
            foreach (DataRow row in dtOption.Rows)
            {
                if (row["ContentId"].Equals(ContentId) && row["OptionID"].Equals(OptionID))
                {
                    row["OptionName"] = OptionName;
                    afftected++;
                }
            }
            HttpContext.Current.Session[ContentId] = dtOption;

            return afftected;
        }

        [DataObjectMethod(DataObjectMethodType.Insert)]
        public int Insert(string ContentId, string OptionID, string OptionName)
        {
            DataTable dtOption = null;
            if (HttpContext.Current.Session[ContentId] == null)
            {
                dtOption = new DataTable("VoteOption");
                dtOption.Columns.Add("ContentId");
                dtOption.Columns.Add("OptionID");
                dtOption.Columns.Add("OptionName");
            }
            else
            {
                dtOption = HttpContext.Current.Session[ContentId] as DataTable;
            }

            DataRow row = dtOption.Rows.Add(ContentId, OptionID, OptionName);
            HttpContext.Current.Session[ContentId] = dtOption;

            return 1;
        }

        [DataObjectMethod(DataObjectMethodType.Delete)]
        public int Delete(string ContentId, string OptionID)
        {
            DataTable dtOption = null;
            if (HttpContext.Current.Session[ContentId] == null)
            {
                dtOption = new DataTable("VoteOption");
                dtOption.Columns.Add("ContentId");
                dtOption.Columns.Add("OptionID");
                dtOption.Columns.Add("OptionName");
            }
            else
            {
                dtOption = HttpContext.Current.Session[ContentId] as DataTable;
            }

            int afftected = 0;
            for (int i = dtOption.Rows.Count - 1; i >= 0; i--)
            {
                DataRow row = dtOption.Rows[i];
                if (row["ContentId"].Equals(ContentId) && row["OptionID"].Equals(OptionID))
                {
                    dtOption.Rows.Remove(row);
                    afftected++;
                }
            }
            HttpContext.Current.Session[ContentId] = dtOption;

            return afftected;
        }
    }
}
HINT:
  • OldValuesParameterFormatString="{0}" means parameter will take value of old value by this name format
  • Parameter name must compeletly match the param of method