Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为mvc3中动态创建的html按钮分配操作_C#_Asp.net Mvc - Fatal编程技术网

C# 为mvc3中动态创建的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

我是mvc新手。我动态创建了html按钮,无法为这些按钮分配操作

代码是:

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?使用它要好得多。此外,如果您的按钮只是更改当前页面,为什么不使用链接?