什么是最好的方式(选择行,删除按钮点击)(What's the best way for (

2019-10-29 09:59发布

我找不到我的问题的任何解决方案。 这是一个MVC项目我的工作。

在GridView我怎么可以这样做: Click on row and then click on button to delete this selected or clicked row.

不需要自动选择按钮的任何解决方案。

所以

  1. 鼠标点击该行

  2. 得到它的ID号或任何价值

  3. 按钮重定向到我的功能+编号。

它是不可能的GridView的? 它会更好,如果我使用表?

这是我曾尝试:

//To get the id 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.RowIndex.ToString();
        string id = DataBinder.Eval(e.Row.DataItem, "Id").ToString();
       e.Row.Attributes.Add("rowid", id);
    }

}

我的JavaScript的按钮

 <a href='<%=ResolveUrl("~/Producter/Delete?id=" ) %>' ID="HyperLink1">Delete</a>

<script  type ="text/javascript" language="javascript">
    //every time a row is clicked this script will perform the following actions:
    $("tr").click(function () {
        var clicked = $(this);
        //get the row id from the currently cliked row
        var rowid = clicked.attr("rowid");
        //get the value of href attribute from the link with id 'HyperLink1'
        var link = $("#HyperLink1").attr("href");
        //remove any previously appended values
        var linkTokens = link.split("=");
        linkTokens[1] = "";
        link = linkTokens.join("=");
        //append the current row id to the link
        link = link + rowid;
        //set the href attribute of your link to the new value
        $("#HyperLink1").attr("href", link);
    });
</script>

有ID =未定义所有的时间。

Answer 1:

如果你使用MVC,我觉得你有GridView的所有错误的用法,我可以从你的代码后面看到。 这是一个服务器控件,因为它取决于回发和ViewState中这是不符合MVC兼容。 您可以在“MVC时装化”很容易做到这一点:

比方说,你想从所谓的“产品”的操作方法显示产品的列表。

  1. 用鼠标右键单击控制器动作,并添加新的视图。
  2. 一个模式对话框弹出。
  3. 名景“产品”。
  4. 从列表中选择的类型就会牢固地结合,因此“产品”类。
  5. 选择的视图“列表”类型。
  6. 选择布局页面(就像母版在ASP.NET)。
  7. 而已。

现在,在控制器动作,你shold调用查看,并通过它的产品,类似的东西的集合:

public ActionResult Products()
{
  List<Products> products = SomeMethodToGetProducts();

  return View(products);
}

简单!

所有你现在要做的是填充在像“编辑”,“删除”等生成的视图的操作。



文章来源: What's the best way for (select row, delete it on button click)