问题描述
我使用的GridView的一些文本框和一个下拉它框。当用户点击某行(文本框或DDL),我想弹出一个JavaScript警告告诉他们行号是什么。我可以得到一个事件,当用户点击文本框之一开火,但我不能告诉他们这是警报内部哪一行,因为我似乎无法弄清楚如何把一个C#变量为一个Javascript警报。
I'm using a Gridview with some text boxes and a drop down box in it. When the user clicks on a row (text box or DDL), I want to pop up a Javascript alert that tells them what the row number is. I can get an event to fire when a user clicks on one of the text boxes, but I can't tell them which row it is inside the alert because I can't seem to figure out how to put a C# variable into a Javascript alert.
下面是我已经试过:
public void gv_instruments_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtBox1 = (TextBox)e.Row.FindControl("txt_partNumbers");
if (txtBox1 != null)
{
txtBox1.Attributes.Add("onclick", "javascript:alert('Message')"); //works
Int32 selectedRow = e.Row.RowIndex;//get row index number
string message = "You've selected row: " + selectedRow.ToString();
message = "javascript:alert('" + message + "')";
//txtBox1.Attributes.Add("onclick", message); //doesn't work
string title = "title";
//ScriptManager.RegisterStartupScript(Page, Page.GetType(),
title, "alert('" + message + "');", true); //doesn't work
//ScriptManager.RegisterClientScriptBlock(Page,Page.GetType(),
title, "alert('" + message + "');",true); //doesn't work
}
}
}
我发现,使用的javascript:警报('在互联网上的网页+消息+');
结构,但它不工作(或至少我无法得到它的工作)。我一直小心使用双引号和放大器;单引号和我可以看到在调试器看起来像一个有效的消息(如: JavaScript的:警报('您已经选择行:0')
,我还以为。撇号你可能会出现的问题,所以我删除和放大器;以你所取代它,但这并不工作的唯一的构造,我可以得到的工作是这样的:
I've found pages on the internet that use the "javascript:alert('" + message + "')";
construct, but it doesn't work (or at least I can't get it to work). I've been careful with the double quotes & single quotes and I can see what looks like a valid message in the debugger (EG: javascript:alert('You've selected row: 0')
, I also thought the apostrophe in "you've" might have been the problem, so I removed that & replaced it with "you have", but that doesn't work either. The only construct that I can get to work is this:
txtBox1.Attributes.Add(onclick事件,JavaScript的:警报('消息'));
我是什么失踪?
推荐答案
如果你移动你的JavaScript来的前端,您的code会大量清洁。
If you move your javascript to front end, your code will be a lot cleaner.
下面是如果你点击一个文本框用于显示一个警告框的例子。
Here is an example which displays an alert box if you click on a textbox.
<script type="text/javascript">
function showMessage(id) {
alert('You have selected row: ' + id);
}
</script>
<asp:GridView runat="server" ID="gv_instruments"
OnRowDataBound="gv_instruments_RowDataBound"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" />
<asp:BoundField HeaderText="FirstName" DataField="FirstName" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:TemplateField HeaderText="Click Me">
<ItemTemplate>
<asp:TextBox runat="server" ID="txt_partNumbers">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var collection = new List<User>()
{
new User {Id = 1, FirstName = "John", LastName = "Doe"},
new User {Id = 2, FirstName = "Marry", LastName = "Doe"},
new User {Id = 3, FirstName = "David", LastName = "Newton"},
};
gv_instruments.DataSource = collection;
gv_instruments.DataBind();
}
}
public void gv_instruments_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var txtBox1 = (TextBox) e.Row.FindControl("txt_partNumbers");
if (txtBox1 != null)
{
txtBox1.Attributes.Add("onclick",
string.Format("showMessage('{0}')", e.Row.RowIndex));
}
}
}
这篇关于将C#字符串变量转换成JavaScript警报在ASP.NET code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!