2

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,

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
asifa
  • 771
  • 1
  • 28
  • 63
  • possible duplicate of [Passing variable from ASP.net to JavaScript](http://stackoverflow.com/questions/10540217/passing-variable-from-asp-net-to-javascript) – Habib May 11 '12 at 06:01

2 Answers2

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();"/>
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • but my button is in gridview and the struser_name is the binded value of the first column of the gridview – asifa May 11 '12 at 06:13
  • my onclientclick event is not firing the record is getting directly deleted without the alert. I change the onclick to onclientclick but still no luck – asifa May 11 '12 at 06:53
  • @asifa - check now i just forgot to include bracket in script ...now take the edit code again this will work..the line which i comment and resolue error by adding bracket.. – Pranay Rana May 11 '12 at 07:17
0
 <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

Nikolai Borisik
  • 1,501
  • 13
  • 22