English 中文(简体)
在协会中,将C# string变量列入Javascript警觉。 NET代码背后
原标题:Insert C# string variable into Javascript alert in ASP.NET code behind

I m使用带有某些文字箱的“格里德”和其中的“滴滴”盒。 当用户在一行(文字箱或DDL)上点击时,我想唤起 Java语的警示,告诉他们浏览次数。 当用户点击其中一个文本箱时,我会发生火灾,但我可以告诉他们,哪里把它放在警示中,因为我似乎无法说明如何将C#变量纳入 Java的警示。

我在此试图:

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:alert(+电+“......”);的构造,但它确实是t work(或者至少我可以做这项工作)。 我先仔细阅读双引书和插图;单引书,我可以看到,在小gger(EG:javascript:alert(a))中,我会看到这样的有效信息。 我也认为,在“现今”中的“apos”可能是问题,因此我删除了“你有”一词,而是用“你有”代替了它。 我能够开展工作的唯一办法就是:

txtBox1.Attributes.Add(“onclick”,“javascript:alert(电文)”);

我失踪了什么?

最佳回答

如果你把你的 j脚本提前结束,你的法典将更加清洁。

这里的一个例子是,如果你在文字箱上点击,就会显示一个警示箱。

“entergraph

<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));
        }
    }
}
问题回答

公正的审判

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

或许,由于第二个信息包含一个单一引文,它就没有工作。

"You ve selected row: "
    ^

写字

"You&apos;ve selected row: "

你的问题出现在“You ve”中。 你们应当摆脱热带,使用:“你选择的行文:”

您可尝试在时间上把所有纽芬兰人与Index人的财产相聚,如:

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

页: 1

@:alert(@name)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!