简体   繁体   中英

server side variable in javascript alert asp.net

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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