我有一个连接到数据库的gridview,并且每行都有一个预览按钮。我需要找到单击按钮的行的ID,从数据库中获取该行的所有数据,并以弹出模式显示它。因此,我需要弹出模式,在C#中调用一个方法,然后单击一下按钮即可找到行ID
我尝试将href和方法调用放在同一按钮上,但没有用。香港专业教育学院试图调用模式从方法中弹出,没有用。香港专业教育学院试图调用javascript,从方法内部弹出模式,没有用。
这是我的asp gridview:
<asp:GridView
ID="GridView1"
runat="server"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblID" Text='<%#Eval("PostID") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div class="dropdown">
<button class="btn dropdown-toggle dropdown-toggle-no-arrow" type="button" id="btnDropDown" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></button>
<div class="dropdown-menu dropdown-menu-sm dropdown-menu-ight" aria-labelledby="dropdown01">
<asp:Button
Text="Preview"
type="Button"
class="dropdown-item"
ID="btnPreview"
runat="server"
CommandName="Preview"
CommandArgument='<%# Container.DataItemIndex %>' />
</div>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是它调用的C#方法:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
Label id = selectedRow.FindControl("lblID") as Label;
localDB.Open();
if (e.CommandName == "Preview")
{
LoadPreview();
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
}
}
这是加载预览方法:
protected void LoadPreview()
{
try
{
localDB.Open();
System.Data.SqlClient.SqlCommand getTitle = new System.Data.SqlClient.SqlCommand();
getTitle.Connection = localDB;
getTitle.CommandText = "Select Title From Post where PostID = @id";
getTitle.Parameters.AddWithValue("id", id);
Label1.InnerText = getTitle.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getDescription = new System.Data.SqlClient.SqlCommand();
getDescription.Connection = localDB;
getDescription.CommandText = "Select PostDescription From Post where PostID = @id";
getDescription.Parameters.AddWithValue("id", id);
Label2.InnerText = getDescription.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getResponsibilities = new System.Data.SqlClient.SqlCommand();
getResponsibilities.Connection = localDB;
getResponsibilities.CommandText = "Select Responsibilities From Job where PostID = @id";
getResponsibilities.Parameters.AddWithValue("id", id);
Label3.InnerText = getResponsibilities.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getQualifications = new System.Data.SqlClient.SqlCommand();
getQualifications.Connection = localDB;
getQualifications.CommandText = "Select Requirements From Job where PostID = @id";
getQualifications.Parameters.AddWithValue("id", id);
Label4.InnerText = getQualifications.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getDepartment = new System.Data.SqlClient.SqlCommand();
getDepartment.Connection = localDB;
getDepartment.CommandText = "Select Department From Job where PostID = @id";
getDepartment.Parameters.AddWithValue("id", id);
Label5.InnerText = getDepartment.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getLocation = new System.Data.SqlClient.SqlCommand();
getLocation.Connection = localDB;
getLocation.CommandText = "Select JobAddress From Job where PostID = @id";
getLocation.Parameters.AddWithValue("id", id);
Label6.InnerText = getLocation.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getSalary = new System.Data.SqlClient.SqlCommand();
getSalary.Connection = localDB;
getSalary.CommandText = "Select Salary From Job where PostID = @id";
getSalary.Parameters.AddWithValue("id", id);
Label7.InnerText = getSalary.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getPayType = new System.Data.SqlClient.SqlCommand();
getPayType.Connection = localDB;
getPayType.CommandText = "Select PayType From Job where PostID = @id";
getPayType.Parameters.AddWithValue("id", id);
Label8.InnerText = getPayType.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getDueDate = new System.Data.SqlClient.SqlCommand();
getDueDate.Connection = localDB;
getDueDate.CommandText = "Select DueDate From Job where PostID = @id";
getDueDate.Parameters.AddWithValue("id", id);
Label9.InnerText = getDueDate.ExecuteScalar().ToString();
System.Data.SqlClient.SqlCommand getday = new System.Data.SqlClient.SqlCommand();
getday.Connection = localDB;
getday.CommandText = "Select ADayInTheLife From Job where PostID = @id";
getday.Parameters.AddWithValue("id", id);
Label10.InnerText = getday.ExecuteScalar().ToString();
localDB.Close();
}
catch
{
Label1.InnerText = "";
Label2.InnerText = "";
Label3.InnerText = "";
Label4.InnerText = "";
Label5.InnerText = "";
Label6.InnerText = "";
Label7.InnerText = "";
Label8.InnerText = "";
Label9.InnerText = "";
Label10.InnerText = "";
}
}
javascript:
<script type="text/javascript">
function openModal() {
$('#myModal3').modal('show');
}
</script>
预览模式
<!-- Preview Modal HTML -->
<div id="myModal3" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Preview Application</h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="bg-white space-sm">
<div class="container">
<div class="row">
<div class="col">
<h2><label ID="Label1" runat="server" text=""></label></h2>
</div>
</div>
</div>
</div>
<!--end of section-->
<div>
<div class="container">
<div class="row justify-content-between">
<div class="col-12 col-md-8 col-lg-7">
<article>
<h5>Role Description</h5>
<label ID="Label2" runat="server" text=""></label>
<h5>Responsibilities</h5>
<label ID="Label3" runat="server" text=""></label>
<h5>Minimum Qualifications</h5>
<label ID="Label4" runat="server" text=""></label>
<h5>A Day In The Life</h5>
<label ID="Label10" runat="server" text=""></label>
</article>
<hr>
</div>
</div>
</div>
</div>
</div>
<!--end of col-->
<div class="card full">
<ul class="list-group list-group-flush full">
<li class="list-group-item full">
<div class="d-flex justify-content-between full">
<div><i class="icon-tools mr-1"></i>Department</div>
<label id="Label5" runat="server" text=""></label>
</div>
</li>
<li class="list-group-item">
<div class="d-flex justify-content-between">
<div><i class="icon-home mr-1"></i>Location</div>
<label id="Label6" runat="server"></label>
</div>
</li>
<li class="list-group-item">
<div class="d-flex justify-content-between">
<div><i class="icon-calculator mr-1"></i>Salary</div>
<label id="Label7" runat="server"></label>
</div>
</li>
<li class="list-group-item">
<div class="d-flex justify-content-between">
<div><i class="icon-stopwatch mr-1"></i>Basis</div>
<label id="Label8" runat="server"></label>
</div>
</li>
<li class="list-group-item">
<div class="d-flex justify-content-between">
<div><i class="icon-calendar mr-1"></i>Due Date</div>
<label id="Label9" runat="server"></label>
</div>
</li>
</ul>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<!--End Preview Modal-->