【问题标题】:What's the best way for (select row, delete it on button click)最好的方法是什么(选择行,单击按钮将其删除)
【发布时间】:2013-05-05 22:37:57
【问题描述】:

我找不到任何解决问题的方法。 这是我正在做的一个 MVC 项目。

在 GridView 中我该怎么做:Click on row and then click on button to delete this selected or clicked row.

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

所以

  1. 鼠标点击行

  2. 获取其 Id 或任何值

  3. 重定向到我的函数 + Id 的按钮。

gridview 不可能吗?如果我使用 Table 会更好吗?

这是我尝试过的:

//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 = 一直未定义。

【问题讨论】:

  • 请发表您的尝试

标签: javascript c# gridview model-view-controller html-table


【解决方案1】:

如果您使用的是 MVC,我认为您对 GridView 的使用完全错误,正如我从您后面的代码中看到的那样。 这是一个与 MVC 不兼容的服务器控件,因为它依赖于 PostBack 和 ViewState。 你可以很容易地以“面向 MVC 的方式”做到这一点:

假设您要显示来自名为“Products”的操作方法的产品列表。

  1. 右键单击该控制器操作并添加新视图。
  2. 会弹出一个模式对话框。
  3. 将该视图命名为“产品”。
  4. 从列表中选择要强绑定的类型,因此 “产品”类。
  5. 选择视图类型为“列表”。
  6. 选择布局页面(如 ASP.NET 中的 MasterPage)。
  7. 就是这样。

现在,在控制器操作中,您应该调用该 View 并将其传递给 Products 集合,如下所示:

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

  return View(products);
}

简单!

您现在要做的就是在生成的视图中填充操作,例如“编辑”、“删除”等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2017-04-10
    • 2015-01-06
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多