通过Web服务更新数据库?

时间:2011-05-17 13:33:56

标签: c# asp.net visual-studio-2008

我正在创建一个拥有大量数据源的Web应用程序。我一直在使用objectdatasource来允许我对数据进行一些过滤,然后绑定到控件。我还不太了解这个对象,但是我想知道当我向这个控件添加数据时,将这个传递给我期待数据集的web服务的最佳方法是什么?

也许objectdatasource也不是最好的方法。结构是,我称之为传递数据集的Web服务,然后如果对数据集进行了更改,我需要通过Web服务返回它以更新数据库。我还需要能够根据一些用户交互过滤内容,这是我传递filterexpression my filterstring以返回数据子集的地方。有没有比这更好的方法呢?

感谢您的帮助

编辑:这有帮助吗?

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class SelectAddProducts : System.Web.UI.Page
    {
        private WebService.AppSoapClient _objWebService;
        private UI.User _clsUser;


        protected override void OnInit(EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Cache.SetExpires(DateTime.MinValue);

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session.Count >= 1)
                {
                    SetupSessionVariables();
                }
                else
                {
                    if (_clsUser == null)
                    {
                        Response.Redirect("Login.aspx");
                    }
                }
            }
        }

        protected void SetupSessionVariables()
        {
            //ObjectDataSource1.
            _objWebService = new UI.WebService.AppSoapClient();
        }

        protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert" && Page.IsValid)
            {
                ObjectDataSource1.Insert();
            }
        }

        protected void btnAddProduct_Click(object sender, EventArgs e)
        {

        }

    }
}

页码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master"   AutoEventWireup="true"
CodeBehind="SelectAddProducts.aspx.cs" Inherits="UI.SelectAddProducts" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
    InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
    TypeName="DBLib.Datasets.ProductsTableAdapters.ProductsTableAdapter"
    UpdateMethod="Update">
    <DeleteParameters>
        <asp:Parameter Name="Original_ProductID" Type="Int32" />
        <asp:Parameter Name="Original_Name" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Original_ProductID" Type="Int32" />
        <asp:Parameter Name="Original_Name" Type="String" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
    </InsertParameters>
</asp:ObjectDataSource>
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" DataKeyNames="ProductID" DataSourceID="ObjectDataSource1" 
    ForeColor="#333333" GridLines="None" ShowFooter="True" 
    style="margin-right: 39px" Width="183px" 
    onrowcommand="gvProducts_RowCommand">
    <RowStyle BackColor="#EFF3FB" />
    <Columns>
        <asp:TemplateField HeaderText="ProductID" InsertVisible="False" 
            SortExpression="ProductID">
            <EditItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductID") %>'>   </asp:Label>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAddProduct" runat="server" CommandName="Insert" 
                    onclick="btnAddProduct_Click" Text="Add" />
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txtNewProductName" Display="Dynamic" 
                    ErrorMessage="You must enter a name for the new product."></asp:RequiredFieldValidator>
                <asp:TextBox ID="txtNewProductName" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>
</asp:Content>

0 个答案:

没有答案