最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - server side variable in javascript alert asp.net - Stack Overflow

programmeradmin0浏览0评论

I want to access the server side variable in javascript alert.

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirm('Are you sure you want to delete this record?'+<%# Eval("struser_name") %>);"/>

This is a delete button in a gridview. On click of it I want to alert the message and the user name. I am getting server tag is not formattted error.

Thanks,

I want to access the server side variable in javascript alert.

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirm('Are you sure you want to delete this record?'+<%# Eval("struser_name") %>);"/>

This is a delete button in a gridview. On click of it I want to alert the message and the user name. I am getting server tag is not formattted error.

Thanks,

Share Improve this question edited May 11, 2012 at 7:35 Pranay Rana 177k37 gold badges243 silver badges266 bronze badges asked May 11, 2012 at 5:58 asifaasifa 7711 gold badge28 silver badges65 bronze badges 1
  • possible duplicate of Passing variable from ASP to JavaScript – Habib Commented May 11, 2012 at 6:01
Add a ment  | 

2 Answers 2

Reset to default 4

EDIT

Attach javascript click function in rowDataBound event of the gridview safest and easy way to do it...code is as below

protected void GridView1_RowDataBond(object source, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            Button btnAlertStatus = (Button)e.Row.FindControl("btnAlertStatus"); 

            DataRowView drv = (DataRowView)e.Row.DataItem; 

            string username = drv["User_name"].ToString(); 

            btnAlertStatus.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?"+ username + "')"); //error because of javascript left one bracket
        } 
    }

PRE

Try

JavaScript

function confirmMsg()
{

  var Variable = '<%= ServerVaraible %>';
  return confirm('Are you sure you want to delete this record?'+ Variable );
}

HTML

 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick="return confirmMsg();"/>
 <asp:Button id="btnDelete" runat="server" class="deleteicon" Text='<%# Eval("iuser_id") %>' OnClick="deleteclick" onclientclick='return confirm("Are you sure you want to delete this record? <%# Eval("struser_name") %>");'/>

Update. Try change quotes

发布评论

评论列表(0)

  1. 暂无评论