开发者

input string is not in correct format (confirm delete button code behind)

开发者 https://www.devze.com 2023-02-07 23:37 出处:网络
I am trying to get 开发者_如何学Pythona new line when display the message. Whats wrong with this code?

I am trying to get 开发者_如何学Pythona new line when display the message. Whats wrong with this code?

Button l = (Button)e.Row.FindControl("btnDelete");            
string textForMessage = @"<script language='javascript'> confirm('Are you sure you want to delete this record \ntest\ntest\ntest');</script>";            
l.Attributes.Add("onclick", textForMessage + DataBinder.Eval(e.Row.DataItem, "Name") + ")");


nono, there is something wrong there...

I think you want to do this:

string textForMessage = @"confirm('Are you sure you want to delete this record with the name: {0}');";            
l.Attributes.Add("onclick", String.Format(textForMessage, DataBinder.Eval(e.Row.DataItem, "Name"));


I think you want to display Name in the confirmation message, here is the proper way.

Button l = (Button)e.Row.FindControl("btnDelete");
string textForMessage = @"return confirm('Are you sure you want to delete this record with the name: {0}');";            
l.Attributes.Add("onclick", String.Format(textForMessage, DataBinder.Eval(e.Row.DataItem, "Name"));

There is no need of script blocks as string inside onclick quotes is known as javascript to browsers and you need to add return keywork as return will stops deleting event to be called.

Edit 1 there is nothing looks wrong with \n you can add \n easily for instance

string textForMessage = @"return confirm('Are you sure you want to delete this record, \nwith the name: {0}\nsome more text lines');";

make sure your string started with @ symbol, as string textForMessage = @"message" else you need to use double slash

0

精彩评论

暂无评论...
验证码 换一张
取 消