1

私はこの答えに従おうとしてきました:選択ボタンなしで 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 に詳しくないため、これがどこで失敗しているのか、データベース ロジックを追加するために何をする必要があるのか​​ 本当にわかりません。

ありがとう

4

2 に答える 2

2

だから私はそれを理解し、jqueryまたはjavascriptを介してそれを実装するためのより良い方法があると確信していますが、私はまだどちらもあまり得意ではありません。

.aspxファイルの場合、gridviewの場合は、次のように追加しました。

AutoGenerateSelectButton ="true"

私のC#では、MsgInbox_SelectedIndexChangedの下に、すべてのRowCommandロジックを配置しました。

最後に、C#のGridview_RowCreatedの下に、[選択]リンクを非表示にするために次の行を追加しました。

e.Row.Cells[0].Style["display"] = "none";
于 2013-01-10T21:21:11.333 に答える
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あり、選択ボタンには css クラスがあることに注意してください。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'); });
});
于 2013-01-09T05:05:25.313 に答える