简体   繁体   English

将C#字符串变量插入ASP.NET代码后面的Javascript警报中

[英]Insert C# string variable into Javascript alert in ASP.NET code behind

I'm using a Gridview with some text boxes and a drop down box in it. 我正在使用带有一些文本框和一个下拉框的Gridview。 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. 当用户单击一行(文本框或DDL)时,我想弹出一个Javascript警报,告诉他们行号是多少。 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. 当用户单击其中一个文本框时,我会触发一个事件,但是我无法告诉他们警报中的哪一行,因为我似乎无法弄清楚如何将C#变量放入Javascript中警报。

Here's what I've tried: 这是我尝试过的:

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
            }
        }
    }

I've found pages on the internet that use the "javascript:alert('" + message + "')"; 我在互联网上找到了使用"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: 我在使用双引号和单引号时非常小心,并且可以在调试器中看到有效消息(例如: javascript:alert('You've selected row: 0') ,我也认为“您可能是问题所在,因此我将其删除并用“您拥有”代替,但这也不起作用。我可以使用的唯一结构是:

txtBox1.Attributes.Add("onclick", "javascript:alert('Message')");

What am I missing? 我想念什么?

If you move your javascript to front end, your code will be a lot cleaner. 如果您将JavaScript移至前端,则代码将更加整洁。

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));
        }
    }
}

just try this 试试这个

string noofrows = dt.Rows.Count.ToString();
string message = "alert('"+ noofrows +" rows found')";
ScriptManager.RegisterClientScriptBlock((sender as Control), this.GetType(),"alert", message, true);
return;

Probably it doesn't work because the second message contains a single quote. 可能不起作用,因为第二条消息包含单引号。

"You've selected row: "
    ^

Try to write 尝试写

"You&apos;ve selected row: "

Your problem occurs beacuse of the apostrophe (') in "You've" . 您的问题是由于“ You've”中的撇号(')引起的。 You should escape the apostrophe and use : "You\\'ve selected row: " 您应该转义撇号并使用:“您已选择行:”

You may try populating all buttons with the ItemIndex property at render time, like this: 您可以尝试在渲染时使用ItemIndex属性填充所有按钮,如下所示:

txtBox1.Attributes.Add(
    "onclick", 
    "javascript:alert('" + e.Item.ItemIndex.ToString() + "')"
    );

At line 8 in your example. 在您的示例的第8行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM