开发者

use a imageButton in GridView to select the row

I am trying to use a image button in a grid view to select a row so that i can then use the SelectedIndexChanged function to do oth开发者_Go百科er things. i have tried this:

    <asp:ImageButton ID="Image1" CommandName="SelectRowGrid2" runat="server" ImageUrl="~/images/select.png" />

  Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
        Select Case e.CommandName
            Case "SelectRowGrid2"
                'some code for selecting the index?
                Label1.Text = GridView2.SelectedIndex
        End Select
    End Sub

but it didn't even step in to the RowCommand sub when i went to debug it.


  • You could set GridView's AutoGenerateSelectButton to true MSDN
  • You could set LinkButton's CommandName to "Select" MSDN

Or you could try following:

 Protected Sub ImgSelect_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)  
     Dim row As GridViewRow = DirectCast(DirectCast(sender, ImageButton).NamingContainer, GridViewRow)
     DirectCast(row.NamingContainer, GridView).SelectedIndex = row.RowIndex
 End Sub

aspx:

<asp:TemplateField HeaderText="Select">
     <ItemTemplate >
         <asp:ImageButton ID="ImgSelect" OnClick="ImgSelect_Click" runat="server" />
      </ItemTemplate>
</asp:TemplateField>


I have done this plenty times in c#. Maybe I can help you out.

Some things I do different than you:

I call CellContentClick opposed to RowCommand. 2. I think check to see, which cell in the row was clicked 3. Check that the row is not null 4. If it was my image button cell, get my row data

1.

private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)

2.

if (e.ColumnIndex == dgv.Columns["btnClmn"].Index)

3.

if (dgvAllApplication.CurrentRow != null)

4.

txtName.Text = dgv.CurrentRow.Cells["App_Name"].Value.ToString();

Good Luck!


you can create on click event on the imagebutton and do whatever you need to do on that row from there

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton img = (ImageButton)sender;
        GridViewRow row = (GridViewRow)img.Parent.Parent;

        //do stuff
        //find label in the same row
        Label lbl = (Label)row.FindControl("Label1");
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜