GridView怎么得到这一行的索引( //我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据)

时间:2021-10-18 15:04:39
//这是我的前台代码
<asp:GridView ID="showNewsData" Width="100%" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" GridLines="Horizontal" OnRowCommand="showNewsData_RowCommand" OnRowDataBound="showNewsData_RowDataBound">
                            <Columns>
                                <asp:BoundField DataField="NewsInformatioId" HeaderText="Id" />
                                <asp:BoundField DataField="NewTypeName" HeaderText="新闻类型名称" />
                                <asp:BoundField DataField="NewTime" HeaderText="发表日期" />
                                <asp:BoundField DataField="NewAuthor" HeaderText="新闻作者" />
                                <asp:BoundField DataField="NewsInformationName" HeaderText="新闻标题" />
                                <asp:BoundField DataField="NewImgPath" HeaderText="新闻图片路径" />
                                <asp:BoundField DataField="NewsInformationTxt" HeaderText="新闻内容" />
                                <asp:TemplateField HeaderText="编辑">
                                    <ItemTemplate>
                                       <asp:ImageButton ID="imgbutAlterDate" CommandArgument='<%#Eval("NewsInformatioId") %>' CommandName="imgbutAlterDate" ImageUrl="~/NewsManage/edt.gif" runat="server" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="删除">
                                    <ItemTemplate>
                                        <asp:ImageButton ID="imgbutDeleteData" CommandArgument='<%#Eval("NewsInformatioId") %>' CommandName="imgbutDeleteData" runat="server" ImageUrl="~/NewsManage/del.gif" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
//后台代码
 protected void showNewsData_RowCommand(object sender, GridViewCommandEventArgs e)
{
            if (e.CommandName == "imgbutAlterDate")//编辑数据            
            {
               //我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据

            }
}

6 个解决方案

#1


	protected void showNewsData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "imgbutAlterDate")//编辑数据   
{
//我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据
Control control = e.CommandSource as Control;
GridViewRow row = control.NamingContainer as GridViewRow;
Response.Write(row.RowIndex);
}
}

#2


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibtn = e.Row.FindControl("ibtn") as ImageButton;
            if (ibtn != null)
            {
                ibtn.CommandArgument = e.Row.RowIndex.ToString();
            }
            
        }
    }
protected void ibtn_Click(object sender, ImageClickEventArgs e)
    {
        string str;
        ImageButton ibtn = sender as ImageButton;
        if (ibtn != null)
        {
            str=ibtn.CommandArgument;
        }             
    }

#3


GridView怎么得到这一行的索引( //我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据)
最笨的,把数据源循环一遍  就不知道是在哪一行了 

#4


引用 2 楼  的回复:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  ImageButton ibtn = e.Row.FindControl("ibtn") as ImageButto……

+1

#5


2楼我支持 呵呵 

#6


说错啦 是1楼 呵呵 我用那个方法好啊

#1


	protected void showNewsData_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "imgbutAlterDate")//编辑数据   
{
//我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据
Control control = e.CommandSource as Control;
GridViewRow row = control.NamingContainer as GridViewRow;
Response.Write(row.RowIndex);
}
}

#2


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibtn = e.Row.FindControl("ibtn") as ImageButton;
            if (ibtn != null)
            {
                ibtn.CommandArgument = e.Row.RowIndex.ToString();
            }
            
        }
    }
protected void ibtn_Click(object sender, ImageClickEventArgs e)
    {
        string str;
        ImageButton ibtn = sender as ImageButton;
        if (ibtn != null)
        {
            str=ibtn.CommandArgument;
        }             
    }

#3


GridView怎么得到这一行的索引( //我想在这里得到鼠标当前在哪一行,是指在GridView第几行数据)
最笨的,把数据源循环一遍  就不知道是在哪一行了 

#4


引用 2 楼  的回复:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  ImageButton ibtn = e.Row.FindControl("ibtn") as ImageButto……

+1

#5


2楼我支持 呵呵 

#6


说错啦 是1楼 呵呵 我用那个方法好啊