1

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

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

3 回答 3

1

使用GridView.RowCommand 事件在 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


    }
  } 
于 2012-11-04T07:16:25.480 回答
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() + ");";
        }

}

于 2012-11-05T07:29:30.077 回答
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));
        }
    }
于 2012-11-04T10:03:31.393 回答