0
MS SQL Serverデータベースには、注文情報が格納されていますが、これを行うにはGUIを使用してデータベース内のエントリを更新する必要があります。.NET DataListの編集と更新
ユーザーが適切な資格情報を使用してログインすると、ページ上のすべてのデータを表示でき、編集用と編集用の2つのボタンが表示されます。しかし、編集モードに入るために編集ボタンを押すたびに、ページは単に更新され、データリストのエントリは編集できません。
質問:私のデータリストからレンダリングされたアイテムを編集し、GUIを使用してデータベースで更新できるようにするにはどうすればよいですか?
マークアップ:背後
<%@ Page Title="Login" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Lab3.Login" %>
<asp:Content runat="server" ContentPlaceHolderID="MainContent">
<table>
<p>User username 'admin' and password 'admin' to view all orders</p>
<tr>
<td>
username
</td>
<td>
<asp:TextBox ID="userText" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
password
</td>
<td>
<asp:TextBox ID="passwordText" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="submitButton" runat="server" Text ="Login" OnClick="submitClick"/>
</td>
</tr>
</table>
<asp:DataList id="DataList1" runat="server" EnableViewState="true" OnEditCommand="DataList1_EditCommand" OnUpdateCommand="DataList1_UpdateCommand">
<ItemTemplate>
<b>First Name:</b>
<asp:Label id="FirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>'></asp:Label><br />
<b>Last Name:</b>
<asp:Label id="LastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>'></asp:Label><br />
<b>Food Item:</b>
<asp:Label id="FoodItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FoodItem") %>'></asp:Label><br />
<b>Address:</b>
<asp:Label id="Address" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Address") %>'></asp:Label><br />
<b>Phone Number:</b>
<asp:Label id="PhoneNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PhoneNumber") %>'></asp:Label><br />
<asp:Button ID="Edit" runat="server" CommandName="Edit" Text="Edit"/>
<asp:Button ID="Update" runat="server" CommandName="Update" Text="Update"/>
</ItemTemplate>
<HeaderTemplate>
<asp:Label id="lblHeader" runat="server" Font-Names="Tahoma" ><h1>List of orders:</h1></asp:Label>
<hr />
</HeaderTemplate>
<FooterTemplate>
<hr />
</FooterTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
</asp:Content>
コード:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Providers.Entities;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Lab3
{
public partial class Login : Page
{
string connect = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|food-truck.mdf;Integrated Security=True";
SqlCommand getAllcomand = new SqlCommand();
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bind();
}
}
protected void submitClick(object sender, EventArgs e)
{
SqlConnection foodDB = new SqlConnection(connect);
string Username = userText.Text;
string Password = passwordText.Text;
SqlCommand compare = new SqlCommand();
compare.Connection = foodDB;
compare.CommandText = "select * from Users where Username='" + Username + "' and Password='" + Password + "'";
foodDB.Open();
SqlDataAdapter da = new SqlDataAdapter(compare);
ds = new DataSet();
da.Fill(ds);
bool loginSuccessful = ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0));
if (loginSuccessful)
{
Console.WriteLine("Success!");
bind();
}
else
{
Response.Write("Authentication failed!");
Console.WriteLine("Invalid username or password");
}
}
protected void bind()
{
string getAllQuery = "SELECT * from dbo.OrderTable";
using (SqlConnection con = new SqlConnection(connect))
{
string[] checkarray = new string[100];
SqlCommand command = new SqlCommand(getAllQuery, con);
con.Open();
SqlDataReader reader = command.ExecuteReader();
DataList1.DataSource = reader;
DataList1.DataBind();
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
bind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
bind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
}
}
}