2013-05-11 26 views
0

我找不到任何解決方案,我的問題。 這是我正在開發的一個MVC項目。什麼是最好的方式(選擇行,刪除它點擊按鈕)

在GridView我怎麼可以這樣做:Click on row and then click on button to delete this selected or clicked row.

不需要自動選擇按鈕的任何解決方案。

所以

  1. 鼠標點擊該行

  2. 獲得其標識或任意數值

  3. 按鈕重定向到我的功能+編號。

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 =未定義所有的時間。

+0

請發表您的評論 – 2013-05-11 10:09:44

+0

已更新我的帖子 – 2013-05-11 10:16:17

回答

0

如果你使用MVC,我認爲你的GridView的用法都是錯誤的,因爲我可以從你的代碼背後看到。 這是一個與MVC不兼容的服務器控件,因爲它依賴於PostBack和ViewState。 您可以在「MVC導向時尚」中輕鬆完成:

比方說,您希望通過名爲「產品」的操作方法顯示產品列表。

  1. 右鍵單擊該控制器操作並添加新視圖。
  2. 彈出一個模態對話框。
  3. 將該視圖命名爲'產品'。
  4. 從列表中選擇它將強烈綁定的類型,因此 'Product'類。
  5. 選擇視圖類型爲'列表'。
  6. 選擇佈局頁面(如ASP.NET中的MasterPage)。
  7. 就是這樣。

現在,在控制器動作,你shold調用查看,並通過它的產品的集合,這樣的事情:

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

    return View(products); 
} 

簡單!

您現在要做的就是在生成的視圖中填充操作,如「編輯」,「刪除」等。

相關問題