我无法弄清楚如何处理这种情况。我有ASP.NET应用程序,网页显示GridView链接到数据库 - 显示可以租用的书籍。我想在每一行都有按钮,因此用户可以点击并租借这本书。点击后,用户应该收到带有&#34问题的messageBox;你真的想租这本书吗?和是/否选项,在c#代码中,我想用行来回答这个答案,单击此按钮并正确处理。
到目前为止,我能够创建此代码:
GridView:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" EmptyDataText="Žádné datové záznamy k zobrazení." OnRowCommand="GridView2_RowCommand">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="author" HeaderText="author" SortExpression="author" />
<asp:BoundField DataField="genre" HeaderText="genre" SortExpression="genre" />
<asp:BoundField DataField="availability" HeaderText="availability" SortExpression="availability" />
<asp:BoundField DataField="owner" HeaderText="owner" SortExpression="owner" />
<asp:BoundField DataField="isbn" HeaderText="isbn" SortExpression="isbn" />
<asp:BoundField DataField="barcode" HeaderText="barcode" SortExpression="barcode" />
<asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
<asp:ButtonField CommandName="text" Text="button" />
</Columns>
</asp:GridView>
处理问题的脚本:
<script type = "text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to rent this book?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
在我的C#代码中我有
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "text")
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
showMessage("You clicked YES!");
}
else
{
showMessage("You clicked NO!");
}
//GridView2.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Text); -> cell value of clicked row
}
}
private void showMessage(string text)
{
string script = "alert(\"" + text + "\");";
ScriptManager.RegisterStartupScript(this, GetType(), "ServerControlScript", script, true);
}
但我无法弄清楚,如何将脚本添加到gridView按钮,有什么帮助吗?
答案 0 :(得分:0)
您可以使用OnClientClick
hash -r
你的javascript函数看起来像
<asp:ButtonField CommandName="text" Text="button" OnClientClick="return Confirm();"/>
无需在隐藏字段中存储值,因为如果用户点击“否”,则页面将不会回发。
答案 1 :(得分:0)
只是弄清楚如何自己做,按钮需要转移到templateField然后可以添加onClientClick