问题描述:GridView里的TemplateField有一个LinkButton。点击这个LinkButton后需要在一个DIV里显示出这个LinkButton所在行的相关数据。下面是我写得代码:
foreach(GridViewRow gvRow in gvFactory.Rows)
{
    if(gvRow.RowType == DataControlRowType.DataRow)
    {
        this.txtFactoryCode.Text = gvRow.Cells[1].Text;
        this.txtFactoryName.Text = gvRow.Cells[2].Text;
        this.txtFactoryCode.ReadOnly = true;
    }
}
但这样有个问题,最终显示得是GridView里最后一行的数据,因为是foreach遍历GridView的每一行数据。这与我想要得结果不符。请教:我怎么会只取出点击得这个LinkButton所在行的数据,其它的行数据不需要取出。急,在线等!

解决方案 »

  1.   

    不用遍历。。用GV中的SelectGridVeiwRow(选择行),有事件的。。直接在里面写。。
      

  2.   

    给LinkButton指定CommandName,如CommandName="MyCmd"), CommandArgument传行索引。处理gvFactory.RowCommand事件, 
    if(e.CommandName == "MyCmd")
    {
        //通过e.CommandArgument传进来的行索引就能找到指定的GridViewRow,再通过这个GridViewRow取到你想要的值。    
    }
      

  3.   

    帮你顶下
    可以判断gvRow.RowIndex == e.RowIndex
      

  4.   

    用GridView的OnRowCommand="gridview_RowCommand"事件试试,这个应该可以在TemplateField中加入CommandName和CommanArgument如以下方法:
    <asp:LinkButton CommandName="SelectReport" CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>在.cs文件中,代码类似如下:
    protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "SelectReport")
            {
                string id = e.CommandArgument.ToString(); //这个就是你要取的当前行的ID            ....//可能通过这个ID从GridView中查找你要的数据行。
            }
        }
      

  5.   

    谢谢chinahnzl,但你没明白我的意思。SelectedIndexChanging事件是行选中事件,也就是说我只要选中一行就触发该事件而不管我点击选中行的哪里。可我现在的需求是只能点击行里的LinkButton才能取出点击这个LinkButton所在行的数据,如果不点击LinkButton是不能取出数据的
      

  6.   

    <asp:Literal ID="RadioButtonMarkup" runat="server" meta:resourcekey="RadioButtonMarkupResource1"></asp:Literal>                                    
                           <asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" Width="0px"  />                                     
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Grab a reference to the Literal control
                Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup");            // Output the up except for the "checked" attribute
                output.Text = string.Format(
                    @"<input type=""radio"" name=""SuppliersGroup"" " +
                    @"id=""ControlProductRowSelector{0}"" value=""{0}""",  e.Row.RowIndex  );
               
                // See if we need to add the "checked" attribute
                if (SuppliersSelectedIndex == e.Row.RowIndex)
                {
                    output.Text += @" checked=""checked""";
                    WUCProductPrice.Text = GridView1.DataKeys[SuppliersSelectedIndex]["price"].ToString();            }
                 output.Text += String.Format(" onclick=\"javascript:document.getElementById('{0}').value='{1}'\" ",
                                 WUCProductPrice.ClientID, GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
                // Add the closing tag
                Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
                if (btnHiddenPostButton != null)
                {
                    //  = String.Format(" javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);                //output.Text += " onclick=\"javascript:setTimeout('__doPostBack(\\'WUCProduct1$GridView1$ctl03$RadioButton1\\',\\'\\')', 0)\" ";
                }            output.Text += " />";
            }    }
      

  7.   


    用这个可以获取。CommandArgument传行索引。一般可以绑定为设置有主键的那一列。
      

  8.   

    如果不是TemplateField里的LinkButton,只是一个Button,那么你不用在.aspx文件里指定CommandArgument,如:<asp:ButtonField Text="按钮" CommandName="MyCmd" />这就够了。protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
            if (e.CommandName == "MyCmd")
            { 
                
                int index = Convert.ToInt32(e.CommandArgument); //注意,这里e.CommandArgument会自动被赋值适当的索引值。
                GridViewRow gvr = GridView1.Rows[index];
                Response.Write(gvr.Cells[1].Text);
            }
     }
    如果是TemplateField里的LinkButton,那么你就得自己传了,按需要你可以传主关键字,或者Container.DisplayIndex等等吧。
      

  9.   

    设置linkbutton的CommandName="select"每次点击按钮时会触发SelectedIndexChanging事件
    然后在事件中写你的功能  e.RowIndex就是你选中行的索引,只有这一行数据
    比如:txtName.text=gridview.rows[e.RowIndex].cells[1].text;
      

  10.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productid" DataSourceID="SqlDataSource1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
        <Columns>
            <asp:BoundField DataField="productid" HeaderText="productid" InsertVisible="False"
                ReadOnly="True" SortExpression="productid" />
            <asp:BoundField DataField="productname" HeaderText="productname" />
            <asp:HyperLinkField DataNavigateUrlFields="productid" DataNavigateUrlFormatString="Details.aspx?id={0}"
                DataTextField="productname" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
    </asp:GridView>protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.txtFactoryCode.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        this.txtFactoryName.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
        this.txtFactoryCode.ReadOnly = true; 
    }