在GridView 中放置一个模板列,其中放一个LinkButton,请问,如何取得点击LinkButton时,所在的当前行GridViewRow 呢?2005没有e.Item了阿,请帮忙了... ....

解决方案 »

  1.   

    void AuthorsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            // If multiple ButtonField column fields are used, use the
            // CommandName property to determine which button was clicked.
            if(e.CommandName=="Add")
            {
                // 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 = AuthorsGridView.Rows[index];
                
                // Create a new ListItem object for the author in the row.     
                ListItem item = new ListItem();
                item.Text = row.Cells[2].Text + " " + row.Cells[1].Text;
                
                // If the author is not already in the ListBox, add the ListItem 
                // object to the Items collection of the ListBox control. 
                if(!AuthorsListBox.Items.Contains(item))
                {
                    AuthorsListBox.Items.Add(item);
                }           
            }
      

  2.   

    waiber(开心工作 && 开心生活) ,你的代码我早写过了,我的这个问题是,取DridView的模板列所在行,请看清问题,不过还是谢谢你的回复,谢谢!
       真的很难么?唉  急死我了... ...
      

  3.   

    我做出来了。
            <asp:GridView ID="GridView1" Runat="server" BorderWidth="1px" BackColor="White" GridLines="Vertical" CellPadding="4" BorderStyle="None" BorderColor="#DEDFDE" ForeColor="Black" AutoGenerateColumns="False" AllowPaging="True" PageSize="5">
                <FooterStyle BackColor="#CCCC99"></FooterStyle>
                <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#F7F7DE"></PagerStyle>
                <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#6B696B"></HeaderStyle>
                <AlternatingRowStyle BackColor="White"></AlternatingRowStyle>
                <Columns>
                    <asp:BoundField HeaderText="FirstName" DataField="FirstName"></asp:BoundField>
                    <asp:BoundField HeaderText="LastName" DataField="LastName"></asp:BoundField>
                    <asp:TemplateField HeaderText="Test Template">
                    <ItemTemplate>
                    <asp:Label ID="Id" text="Test Template" Runat=server ></asp:Label>
                    </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#CE5D5A"></SelectedRowStyle>
                <RowStyle BackColor="#F7F7DE"></RowStyle>
            
            </asp:GridView>-----------------------    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                MyOninit()
            End If
        End Sub    Private Sub MyOninit()
            Dim strConn As String = "server=(local);database=Northwind;uid=sa;pwd="
            Dim Conn As SqlConnection = New SqlConnection(strConn)
            Conn.Open()
            Dim sql As String = "select * from Employees"
            Dim cmd As SqlCommand = New SqlCommand(sql, Conn)
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim ds As DataSet = New DataSet
            da.Fill(ds, "myxTest")
            GridView1.DataSource = ds.Tables(0).DefaultView
            GridView1.DataBind()
            Conn.Close()    End Sub    Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim row As GridViewRow = e.Row
                Dim LbId As Label = CType(row.Cells(2).FindControl("Id"), Label)            If Not LbId Is Nothing Then
                    Response.Write(LbId.Text & "<br>")
                End If
            End If    End Sub但我不知道在下面这怎么分页,用原来的CurrentPageIndex 出错。555
        Private Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
        '    'GridView1.CurrentPageIndex = e.NewPageIndex    End Sub
      

  4.   

    我解决了。在GridView 的RowDataBound事件中,指定模板列中LinkButton 的CommandArgument 属性的参数为,当前绑定行的索引,即绑定每一行时,就挨个指定这个
    模板列中LinkButton 的CommandArgument 属性的参数为行号,使用时,在RowCommand事件或其他这个GrideView 的事件中,这个按钮的CommandArgument 属性就是当前行的索引号了。
    我也是结合MSDN才知道必须,提前给模板列中命令按钮的CommandArgument 赋值之后才可以使用的。
    Code样例:
    RowDataBound事件中:
    {
       LinkButton lb = ((LinkButton)e.Row.FindControl("LinkButtonSR"));
            if (lb == null)
            {
                return;
            }
            lb.Enabled = true;
            switch (Convert.ToInt32((e.Row.Cells[10].Text.Trim())))
            {
                case 1:
                    lb.CommandArgument = e.Row.DataItemIndex.ToString();
                     break;
                  ... ...
             }
    }
      

  5.   

    RowCommand事件中,判断CommandArgument 是否是null , 是null 则,不执行
    RowCommand事件中的代码,则分页不会出错了。
      

  6.   

    这么麻烦?我怎么在窗体上用个this.close都出错啊?