c# - 单击按钮后更改 SQL 数据网格视图
问题描述
我正在尝试更改我的网格视图中显示的 SQL 中的状态数据,但我无法做到。我在这里要做的是能够在按下批准按钮时将所选行的状态值更改为“A”。我可以知道怎么做吗?
这是我的界面预览,下面是我的代码:
.aspx 文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/AdminTemplate.Master" AutoEventWireup="true" CodeBehind="ViewRequests.aspx.cs" Inherits="webAssignment_P05_Group2.ViewRequests" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.auto-style2 {
height: 388px;
color: #000000;
background-color: #FFFFFF;
}
.auto-style3 {
width: 96%
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table class="auto-style3">
<tr>
<td class="auto-style2">
<asp:GridView ID="gv_ParentRequests" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Height="263px" Width="819px" AutoGenerateSelectButton="True" OnSelectedIndexChanged="gv_ParentRequests_SelectedIndexChanged" DataKeyNames="ViewingRequestID">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ViewingRequestID" HeaderText="Request ID" />
<asp:BoundField DataField="ParentName" HeaderText="ParentName" />
<asp:BoundField DataField="StudentName" HeaderText="StudentName" />
<asp:BoundField DataField="StudentID" HeaderText="StudentID" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
</Columns>
<EmptyDataTemplate>
No record found!<br />
</EmptyDataTemplate>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn_ApproveViewRequest" runat="server" Text="Approve" Width="143px" OnClick="btn_ApproveViewRequest_Click" />
<asp:Button ID="btn_RejectViewRequest" runat="server" Text="Reject" Width="143px" OnClick="btn_ApproveViewRequest_Click" />
</td>
</tr>
</table>
</asp:Content>
Aspx.cs 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using web_assignment_P05_group2;
namespace webAssignment_P05_Group2
{
public partial class ViewRequests : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
displayParentReq();
}
}
protected void displayParentReq()
{
string strConn = ConfigurationManager.ConnectionStrings["StudentEPortfolioConnectionString"].ToString();
// Instantiate a SqlCOnnection object with the connection string read.
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("SELECT ViewingRequestID, ParentName, StudentName," +
"StudentID, Status, DateCreated" +
" FROM ViewingRequest INNER JOIN Parent" +
" ON ViewingRequest.ParentID = Parent.ParentID" +
" ORDER BY DateCreated DESC", conn);
SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
DataSet result = new DataSet();
conn.Open();
daRequest.Fill(result, "viewingRequest");
conn.Close();
gv_ParentRequests.DataSource = result.Tables["viewingRequest"];
gv_ParentRequests.DataBind();
}
protected void gv_ParentRequests_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Set the page index to the page clicked by user.
gv_ParentRequests.PageIndex = e.NewPageIndex;
// display records on the new page.
displayParentReq();
}
protected void gv_ParentRequests_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedRequestNo = Convert.ToInt32(gv_ParentRequests.SelectedDataKey[0]);
ParentRequests objRequest = new ParentRequests();//
DataSet result = new DataSet();
objRequest.viewrequestid = selectedRequestNo;
}
}
}
父请求.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace web_assignment_P05_group2
{
public class ParentRequests
{
public int viewrequestid;
public int parentid;
public string studentname;
public int studentid;
public char status;
public DateTime datecreated;
public int ApproveReq(ref Dataset result)
{
string strConn = ConfigurationManager.ConnectionStrings
["StudentEPortfolioConnectionString"].ToString();
// Instantiate a SqlCOnnection object with the connection string read.
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("UPDATE ViewingRequest " +
"SET Status = 'A' WHERE ViewingRequestID = @selectedViewingRequestID ", conn);
SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
DataSet requests = new DataSet();
conn.Open();
daRequest.Fill(requests, "viewingRequest");
conn.Close();
return 0;
}
}
}
解决方案
尝试这个:
public int ApproveReq(ref Dataset result)
{
string strConn = ConfigurationManager.ConnectionStrings
["StudentEPortfolioConnectionString"].ToString();
// Instantiate a SqlCOnnection object with the connection string read.
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("UPDATE ViewingRequest " +
"SET Status = 'A' WHERE ViewingRequestID = @selectedViewingRequestID ", conn);
cmd.parameters.add("@selectedViewingRequestID",Pass_selectedid);
SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
DataSet requests = new DataSet();
conn.Open();
daRequest.Fill(requests, "viewingRequest");
conn.Close();
return 0;
}
推荐阅读
- c# - 在 WPF 中将数据网格与 SQL Server 数据库表绑定的两种方法
- django-cms - django-cms:我可以一步发布所有页面吗
- c - 两个指针位于同一个地址但指向不同的东西
- ios - 在封闭的文本视图中使用动态字体自动调整视图
- c# - 如何在回收站视图中实现片段事务
- javascript - 使用 PHP 中的 JavaScript 更改 HTML 属性
- amazon-web-services - 每天运行少于 24 小时时预留实例
- android - Urban Airship Android GCM 未在 Android Oreo 和 Pie 上收到推送通知
- node.js - 如何修改使用 TypeScript 构建的 npm 包
- kubernetes - 如何在内部 Kubernetes 集群上创建持久卷