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
2 Answers
Reset to default 4EDIT
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