在visual studio中更改数据库条目

时间:2014-03-13 16:48:28

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

我正在构建一个Web应用程序,它显示健身器材表中的数据库信息。到目前为止,我能够生成一个网页,其中包含所有项目的下拉列表,并且信息会自动填充字段以供参考。

如何制作,以便查看网站的人只需添加,更新/删除/提交等几个按钮即可添加,删除和修改表格条目?

这是我到目前为止制作的C#代码:

public partial class Inventory : System.Web.UI.Page
{
Item currentInventory = new Item();

protected void Page_Load(object sender, EventArgs e)
{
    UpdateInventory(0);
}



private void UpdateInventory(int index)
{
    DataView inventoryTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    DataRowView row = (DataRowView)inventoryTable[index];

    currentInventory.fac_ID = row["fac_ID"].ToString();
    currentInventory.inv_Quantity = row["inv_Quantity"].ToString();
    currentInventory.inv_Purchase_Date = row["inv_Purchase_Date"].ToString();


    lblFacilityID.Text = currentInventory.fac_ID;
    lblQuantity.Text = currentInventory.inv_Quantity;
    lblPurchaseDate.Text
= Convert.ToDateTime(row["inv_Purchase_Date"]).ToShortDateString();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    UpdateInventory(DropDownList1.SelectedIndex);
}
}

这是我到目前为止的.aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"    CodeFile="Inventory.aspx.cs" Inherits="Inventory" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="Server">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"   DataTextField="equip_Name" DataValueField="equip_ID" AutoPostBack="True"   OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="    <%$      ConnectionStrings:ConnectionString %>" SelectCommand="SELECT Inventory.*, Equipment.* FROM      Equipment INNER JOIN Inventory ON Equipment.equip_ID = Inventory.equip_ID">   </asp:SqlDataSource>

<table>
    <tr>
        <td>Facility:
        </td>
        <td>
            <asp:Label ID="lblFacilityID" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>Quantity:
        </td>
        <td>
            <asp:Label ID="lblQuantity" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>Purchase Date:
        </td>
        <td>
            <asp:Label ID="lblPurchaseDate" runat="server"></asp:Label>
        </td>
    </tr>
</table>

<br />
<asp:DetailsView ID="ManageProducts" runat="server" Height="50px" Width="125px">
</asp:DetailsView>


</asp:Content>

2 个答案:

答案 0 :(得分:0)

如果您的要求很简单,并且您只是想让用户管理数据。

使用ASP.NET的动态数据网站分析应用程序的数据模型,并根据数据模型中的数据动态生成网页。这些自动生成的Web页面提供了显示,插入,删除和编辑每个表的数据的功能。

查看http://msdn.microsoft.com/en-us/library/cc488469.ASPX

答案 1 :(得分:0)

对此的回答可能超出了stackoverflow的范围。

我在MVC中做了很多工作,而且webforms并没有太大的不同。您想要做的是以某种方式从网页中获取这些更改(也许使用AJAX?)然后实现与您已经编写的内容不同的更改。

我的建议是查看MSDN文章,而不是希望这里有3页答案。