2013-01-09 63 views
1

我一直努力遵循這樣的回答:How to implement full row selecting in GridView without select button?製作中的GridView整行可點擊

但我還是略顯混亂。在遵循這個問題之後,我的行現在可以點擊了。但是我怎麼實現它來點擊後做點什麼?目前,我用一個按鈕做什麼,我需要每行數據庫:

下面是在.aspx代碼:

<Columns> 
<asp:ButtonField Text = "Click Me" CommandName = "Clicked" ButtonType = "Button" /> 
...other columns stuff 
</Columns> 

C#代碼背後:

protected void RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     //if button is clicked 
     if (e.CommandName == "Clicked") 
     { 
      //go find the index on the gridview 
      int selectedIndex = MsgInbox.SelectedIndex; 
      if (int.TryParse(e.CommandArgument.ToString(), out selectedIndex)) 
      { 
       //do something with database 
      } 

現在,精美的作品。但是,我不希望按鈕是可點擊的,我希望整行都是可點擊的。我知道這是目前錯誤的,但是這是我到目前爲止的代碼:

的.aspx代碼

<Columns> 
    <asp:TemplateField> 
       <ItemTemplate> 
        <asp:LinkButton ID="SelectRow" runat="server" ForeColor="red" CommandName="Clicked"></asp:LinkButton> 
       </ItemTemplate> 
    </asp:TemplateField> 

C#代碼:

protected void Gridview_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';"; 
      e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';"; 
      var selectButton = e.Row.FindControl("SelectRow") as Button; 
      e.Row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(selectButton, ""); 

我得到一個簡單的空指針異常當我這樣做時,但我並不真正熟悉e.Row.Attributes,所以我真的不知道這是哪裏失敗,以及我需要做什麼來添加數據庫邏輯。

謝謝

+1

哪一行特別是拋出一個異常?什麼樣的例外?注意你應該使用'作爲LinkBut​​ton'而不是'作爲Button',因爲'SelectRow'是一個'LinkBut​​ton'。 –

+0

哦,甜美!現在我沒有錯誤,現在我做了一個LinkBut​​ton。你知道我應該在哪裏輸入數據庫代碼的邏輯(例如,單擊這一行並對數據庫執行此操作)?現在,當我點擊它時,沒有任何反應,因爲我不確定在哪裏/如何添加代碼。謝謝! – Kevin

+0

處理GridView的RowCommand事件。 –

回答

2

如果您準備好使用jQuery,這將會簡單得多。例如,

<asp:GridView rowStyle-CssClass="row" ... 
... 
<asp:TemplateField> 
    <ItemTemplate> 
    <asp:LinkButton ID="SelectRow" runat="server" CommandName="Clicked" CssClass="selButton" /> 
    </ItemTemplate> 
</asp:TemplateField> 

注意,每個數據行將有css類row和選擇按鈕將有selButton

CSS將一些-東西一樣

tr.row { } /* normal row styling */ 
tr.row-highlight { background-color: blue; } /* highlighted row styling */ 
tr.row .selButton { display:none; visibility:hidden; } /* select button styling, I am using hidden button */ 

最後,Java的腳本

$(document).ready(function() { 
    $('tr.row').click(
     function() { 
     // simulate click of select button 
     $(this).find('.selButton').click(); 
     }).hover(
     // add/remove css class to highlight on mouse over/out 
     function() { $(this).addClass('row-highlight'); }, 
     function() { $(this).removeClass('row-highlight'); }); 
}); 
2

所以我想通了,我敢肯定有b etter的方式來實現它通過jQuery或JavaScript,但我不是太好。

對於我的.aspx文件,爲GridView,我只是說:

AutoGenerateSelectButton ="true" 

在我的C#,MsgInbox_SelectedIndexChanged下,我把我所有的RowCommand邏輯。

最後,在C#中,在我的Gridview_RowCreated,我加入這一行隱藏選擇鏈接:

e.Row.Cells[0].Style["display"] = "none";