我创建了一个中继器控件,该控件显示数据库中的数据。当用户单击删除按钮时,它应该从数据库中获取相应的列数据并将其存储在实际上没有发生的字段中。
我在整个代码中附加了这个问题。
任何人请帮助我!
所有其他可用的解决方案仅基于jQuery。但是我想通过c#做到这一点。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater.aspx.cs" Inherits="intern4.Repeater" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="repeaterstylesheet.css" />
</head>
<body>
<form id="form1" runat="server" >
<div>
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate>
<div class="container align" >
<div class="row header">
<div class="col-sm-2">ID</div>
<div class="col-sm-2">Name</div>
<div class="col-sm-2">Class</div>
<div class="col-sm-2">Edit</div>
<div class="col-sm-2">Delete</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="row data">
<div class="col-sm-2"> <asp:Label ID="lblId" runat="server" Text='<%# Eval("StudentID") %>' /></div>
<div class="col-sm-2"><asp:Label ID="lblName" runat="server" Text='<%# Eval("StudentName") %>' /></div>
<div class="col-sm-2"><asp:Label ID="lblClassx" runat="server" Text='<%# Eval("StudentClass") %>' /></div>
<div class="col-sm-2"><a href="StudEdit.aspx?ID= <%# Eval("StudentID") %>">Edit</a></div>
<div class="col-sm-2">
<asp:LinkButton ID="linkbtn" runat="server" ClientIDMode="Static" CssClass="btn btn-primary" data-target="#exampleModalCenter"
data-toggle="modal" CommandArgument='<%# Eval("StudentID") %>' OnCommand="btn_clck" >
Delete
</asp:LinkButton>
</div>
</div>
<hr style="color:blue" />
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Conformation alert</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<legend class="col-form-label col-sm-2 pt-0">ID</legend>
<div class="col-sm-10">
<div class="form-check">
<asp:label id="lblStudentID" runat="server" class="form-check-label" />
</div>
</div>
</div>
<div class="form-group row">
<label for="inputName" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<asp:TextBox ID="inputName" runat="server" class="form-control" Text=""></asp:TextBox>
</div>
</div>
<div class="form-group row">
<label for="inputClass" class="col-sm-2 col-form-label">Class</label>
<div class="col-sm-10">
<asp:TextBox ID="inputClass" runat="server" class="form-control" Text=""></asp:TextBox>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<asp:Button runat="server" Text="yes" OnClick="Deletestudent" />
</div>
</div>
</div>
</div>
</form>
</body>
</html>
C#代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace intern4
{
public partial class Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT StudentID, StudentName, StudentClass FROM tbl_Student", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
}
}
}
protected void Deletestudent(object sender, EventArgs e)
{
int studentId = int.Parse(((sender as Button).NamingContainer.FindControl("lblId") as Label).Text);
string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM tbl_Student WHERE StudentID = @StudentId", con))
{
cmd.Parameters.AddWithValue("@StudentId", studentId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindRepeater();
}
protected void btn_clck(object sender, CommandEventArgs e)
{
int s = int.Parse((sender as LinkButton).CommandArgument);
lblStudentID.Text = s.ToString();
string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
try
{
SqlCommand cmd = new SqlCommand(
"select * from tbl_Student where StudentID=" + s + "", con);
SqlDataReader sd = cmd.ExecuteReader();
while (sd.Read())
{
inputName.Text = sd.GetValue(1).ToString();
inputClass.Text = sd.GetValue(2).ToString();
}
}
catch (SqlException ex)
{
}
con.Close();
}
}
}
答案 0 :(得分:0)
根据this answer,您必须在设置TextBox
控件后添加以下代码:
//... Some other code
while (sd.Read()) {
inputName.Text = sd.GetValue(1).ToString();
inputClass.Text = sd.GetValue(2).ToString();
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script type='text/javascript'>");
sb.Append("$('#exampleModalCenter').modal('show');");
sb.Append(@"</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "MyModel",
sb.ToString(), false);
此代码将从C#代码后面填充Bootstrap模型。