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,

1

2 Answers 2

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();"/>
Sign up to request clarification or add additional context in comments.

3 Comments

but my button is in gridview and the struser_name is the binded value of the first column of the gridview
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 - 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..
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.