实现一个客户端的DataSet-----index.htm

80酷酷网    80kuku.com

  客户端<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<TITLE>客户端DataSet</TITLE>
<SCRIPT>
var iRowIdx = 0;

function init()
{
    services.useService("ClientDataSetDataProvider.asmx?WSDL", "clientdatasetdataprovider");
    loadData();
}
function loadData()
{
    services.clientdatasetdataprovider.callService(loadresult, "GetPubs");
}
function loadresult(result)
{
    if (result.error) {
        alert(result.errorDetail.string);
    }
    else {
        dsPubs.ReadXml(result.value);
        iRowIdx = 0;
        updateButtons();
        dataBind();
    }
}
function dataBind()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var oRow = oTable.GetRow(iRowIdx);
        if (oRow) {
            txtAuthorID.value    = oRow.GetValue("au_id");
            txtFirstName.value    = oRow.GetValue("au_fname");
            txtLastName.value    = oRow.GetValue("au_lname");
            txtPhone.value        = oRow.GetValue("phone");
            txtAddress.value    = oRow.GetValue("address");
            txtCity.value        = oRow.GetValue("city");
            txtState.value        = oRow.GetValue("state");
            txtZip.value        = oRow.GetValue("zip");
            chkContract.checked    = (oRow.GetValue("contract") == "true") ? true : false;
            return;
        }
    }

    txtAuthorID.value    = "";
    txtFirstName.value    = "";
    txtLastName.value    = "";
    txtPhone.value        = "";
    txtAddress.value    = "";
    txtCity.value        = "";
    txtState.value        = "";
    txtZip.value        = "";
    chkContract.value    = false;
}
function text_onBlur()
{
    var sDataFld = event.srcElement.getAttribute("datafld");
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var oRow = oTable.GetRow(iRowIdx);
        if (oRow) {
            oRow.SetValue(sDataFld, event.srcElement.value);
        }
    }
}
function checkbox_onBlur()
{
    var sDataFld = event.srcElement.getAttribute("datafld");
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var oRow = oTable.GetRow(iRowIdx);
        if (oRow) {
            oRow.SetValue(sDataFld, event.srcElement.checked.toString().toLowerCase());
        }
    }
}
function updateButtons()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var cRows = oTable.GetRowCount();
        if (iRowIdx == 0 || cRows == 0) {
            btnFirst.disabled = true;
            btnPrev.disabled = true;
        }
        else {
            btnFirst.disabled = false;
            btnPrev.disabled = false;
        }
        
        if (iRowIdx < cRows-1) {
            btnNext.disabled = false;
            btnLast.disabled = false;
        }
        else {
            btnNext.disabled = true;
            btnLast.disabled = true;
        }
        
        if (cRows > 0) {
            btnDelete.disabled = false;
        }
        else {
            btnDelete.disabled = true;
        }
    }
    else {
        btnFirst.disabled = true;
        btnPrev.disabled = true;
        btnNext.disabled = true;
        btnLast.disabled = true;
        btnAdd.disabled = true;
        btnDelete.disabled = true;
    }
}
function btnSave_onClick()
{
    var dsChanges = dsPubs.GetChanges();
    services.clientdatasetdataprovider.callService(saveresult, "SaveChanges", dsChanges);
}
function saveresult(result)
{
    if (result.error) {
        alert(result.errorDetail.string);
        dsPubs.RejectChanges();
    }
    else {
        alert("数据已保存!");
        dsPubs.AcceptChanges();
    }
    
    updateButtons();
    dataBind();
}
function btnFirst_onClick()
{
    iRowIdx = 0;
    updateButtons();
    dataBind();
}
function btnPrev_onClick()
{
    iRowIdx--;
    if (iRowIdx < 0) iRowIdx = 0;
    updateButtons();
    dataBind();
}
function btnNext_onClick()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var cRows = oTable.GetRowCount();
        iRowIdx++;
        if (iRowIdx >= cRows) iRowIdx = cRows - 1;
        updateButtons();
        dataBind();
    }
}
function btnLast_onClick()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var cRows = oTable.GetRowCount();
        iRowIdx = cRows - 1;
        updateButtons();
        dataBind();
    }
}
function btnAdd_onClick()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        var oRow = oTable.AddRow();
        iRowIdx = oTable.GetRowCount()-1;
        updateButtons();
        dataBind();
    }
}

function btnDelete_onClick()
{
    var oTable = dsPubs.GetTable("authors");
    if (oTable) {
        oTable.GetRow(iRowIdx).Delete();
        oTable = dsPubs.GetTable("authors");
        var cRows = oTable.GetRowCount();
        if (iRowIdx >= cRows) iRowIdx = cRows - 1;
        updateButtons();
        dataBind();
    }
}

function btnGetChanges_onClick()
{
    var oDoc = dsPubs.GetChanges();
    var oDiff = oDoc.selectSingleNode("//diffgr:diffgram");
    txtDebug.value = oDiff.xml;
}

function btnAcceptChanges_onClick()
{
    dsPubs.AcceptChanges();
    txtDebug.value = dsPubs.XmlData.xml;
}

function btnRejectChanges_onClick()
{
    dsPubs.RejectChanges();
    txtDebug.value = dsPubs.XmlData.xml;
}

window.attachEvent("onload", init);
</SCRIPT>
</HEAD>
<BODY>

<table cellspacing=0 cellpadding=10 border=0>
<tr>
    <td align=left valign=top>
        <b>Author ID:</b>

        <input id=txtAuthorID type=text datafld="au_id" onblur="text_onBlur()">



        <b>First Name:</b>

        <input id="txtFirstName" type=text datafld="au_fname" onblur="text_onBlur()">



        <b>Last Name:</b>

        <input id="txtLastName" type=text datafld="au_lname" onblur="text_onBlur()">


    </td>
    <td align=left valign=top>
        <b>Phone:</b>

        <input id="txtPhone" type=text datafld="phone" onblur="text_onBlur()">



        <b>Address:</b>

        <input id="txtAddress" type=text datafld="address" onblur="text_onBlur()">



        <b>City:</b>

        <input id="txtCity" type=text datafld="city" onblur="text_onBlur()">


    </td>
    <td align=left valign=top>
        <b>State:</b>

        <input id="txtState" type=text datafld="state" onblur="text_onBlur()">



        <b>Zip:</b>

        <input id="txtZip" type=text datafld="zip" onblur="text_onBlur()">



        <b>Contract:</b><input id="chkContract" type=checkbox datafld="contract" onblur="checkbox_onBlur()">


    </td>
</tr>
</table>

<hr>

<table width="100%">
<tr>
    <td align=left valign=top>
        <input id="btnFirst" type=button value="First" onclick="btnFirst_onClick()">
        <input id="btnPrev" type=button value="Prev" onclick="btnPrev_onClick()">
        <input id="btnNext" type= button value="Next" onclick="btnNext_onClick()">
        <input id="btnLast" type= button value="Last" onclick="btnLast_onClick()">
        <input id="btnAdd" type=button value="Add" onclick="btnAdd_onClick()">
        <input id="btnDelete" type=button value="Delete" onclick="btnDelete_onClick()">
    </td>
    <td align=right valign=top>
        <input id="btnGetChanges" type=button value="GetChanges" onclick="btnGetChanges_onClick()">
        <input id="btnAcceptChanges" type=button value="AcceptChanges" onclick="btnAcceptChanges_onClick()">
        <input id="btnRejectChanges" type=button value="RejectChanges" onclick="btnRejectChanges_onClick()">
        <input id="btnSave" type=button value="Save" onclick="btnSave_onClick()">
    </td>
</tr>
</table>

<hr>

<textarea id=txtDebug rows=20 id="services" showprogress=true></div>
<div id="dsPubs"

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: