C# 为mvc3中动态创建的html按钮分配操作
我是mvc新手。我动态创建了html按钮,无法为这些按钮分配操作 代码是:C# 为mvc3中动态创建的html按钮分配操作,c#,asp.net-mvc,C#,Asp.net Mvc,我是mvc新手。我动态创建了html按钮,无法为这些按钮分配操作 代码是: sb.Append("<li style=\"margin:17px;\">"); sb.Append("<img src=\"" + path + "Content/HeaderSlides/FullImages/" + imagename + "\" width=\"180\" height=\"100\"/>"); sb
sb.Append("<li style=\"margin:17px;\">");
sb.Append("<img src=\"" + path + "Content/HeaderSlides/FullImages/"
+ imagename + "\" width=\"180\" height=\"100\"/>");
sb.Append("<input type=\"button\" name=\"deleteimage" + id
+ " \" id=\"btndelete" + id
+ "\" value=\"Delete\" class=\"t-button t-grid-delete\" "
+ " style=\"margin-left:10px;\" />");
sb.Append(“”;
某人加上(“”);
某人加上(“”);
我想让html按钮像这样:
<input type="submit"
title="Delete"
value="Delete"
name="btndelete"
onclick="location.href='@Url.Action("DeleteHeaderSlide",
"HeaderSlides",
new { filename = "Sunset.jpg" })'" />
如果您能够用代码创建html按钮,定义/分配操作的最简单方法是
string action = "<script>function yourFunction() {alert('hi');}</script>";
string btnCraeteCode = "<input type='button' onClick='yourFunction()' />";
sub.Append(action+btnCraeteCode);
string action=“function yourFunction(){alert('hi');}”;
字符串btnCraeteCode=“”;
子项追加(动作+btnCraeteCode);
您也可以已经在页面的head标记中的脚本中定义了yourFunction()
在这种情况下,您不需要在createButton之前添加操作字符串,如果您能够用代码创建html按钮,定义/分配操作的最简单方法是
string action = "<script>function yourFunction() {alert('hi');}</script>";
string btnCraeteCode = "<input type='button' onClick='yourFunction()' />";
sub.Append(action+btnCraeteCode);
string action=“function yourFunction(){alert('hi');}”;
字符串btnCraeteCode=“”;
子项追加(动作+btnCraeteCode);
您也可以已经在页面的head标记中的脚本中定义了yourFunction()
在这种情况下,您不需要在createButton前面添加操作字符串,最好使用jquery
$(document).ready(function(){
$('input[name=btndelete]').bind('click', function() {
window.location.href='@Url.Action("DeleteHeaderSlide",
"HeaderSlides",
new { filename = "Sunset.jpg" })';
});
});
您最好使用jquery
$(document).ready(function(){
$('input[name=btndelete]').bind('click', function() {
window.location.href='@Url.Action("DeleteHeaderSlide",
"HeaderSlides",
new { filename = "Sunset.jpg" })';
});
});
仅给出创建一个按钮的完整代码。所有其他的都是一样的。为什么要用字符串创建html?使用它要好得多。另外,如果你的按钮只是改变了当前页面,为什么不使用链接呢?只给出一个按钮的完整创建代码。所有其他的都是一样的。为什么要用字符串创建html?使用它要好得多。此外,如果您的按钮只是更改当前页面,为什么不使用链接?