当用户在GridView中单击行时如何调用特定方法

时间:2012-11-04 07:07:14

标签: asp.net

我在每行添加以下行,当我点击GridView中的行时我可以选择它(意味着事件GridView_SelectedIndexChanged雇用)当我点击行特定方法调用时如何更改它

e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);

3 个答案:

答案 0 :(得分:1)

使用 GridView.RowCommand Event :在GridView控件中单击按钮时会引发RowCommand事件。这使您能够提供事件处理方法,以便在发生此事件时执行自定义例程。

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="YourCommandName")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = GridView1.Rows[index];

      //Your Code


    }
  } 

答案 1 :(得分:1)

in view:

 <script type="text/javascript" language="javascript">
     function show(id) {
         alert(id);
         // Do something as your need
     }
    </script>

<asp:GridView runat="server" ID="GridView1" DataKeyNames="DepartmentID" AutoGenerateColumns="False"
        Font-Names="Tahoma" Font-Size="Small" 
        OnRowDataBound="GridView1_RowDataBound" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="DepartmentID" HeaderText="DepartmentID" 
                InsertVisible="False" ReadOnly="True" SortExpression="DepartmentID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Budget" HeaderText="Budget" 
                SortExpression="Budget" />
            <asp:BoundField DataField="StartDate" HeaderText="StartDate" 
                SortExpression="StartDate" />
            <asp:BoundField DataField="Administrator" HeaderText="Administrator" 
                SortExpression="Administrator" />
        </Columns>
    </asp:GridView>

inCodeBehind:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

             if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.cursor='pointer'");

            e.Row.Attributes["onclick"] = "show(" + e.Row.RowIndex.ToString() + ");";
        }

}

答案 2 :(得分:0)

<asp:GridView runat="server" ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="False"
        Font-Names="Tahoma" Font-Size="Small" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
        OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Row">
                <ItemTemplate>
                    <%# Container.DataItemIndex+1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
            <asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
        </Columns>
    </asp:GridView>


 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //add css to GridViewrow based on rowState
            e.Row.CssClass = e.Row.RowState.ToString();
            //Add onclick attribute to select row.
            e.Row.Attributes.Add("onclick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex));
        }
    }