如何使用Asp.net在gridview中查找rowcommand事件的特定行 [英] how to find the perticular row of rowcommand event in gridview using Asp.net

查看:92
本文介绍了如何使用Asp.net在gridview中查找rowcommand事件的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们好,我在google中搜索了很多网站.但是我没有解决方案.

这是我的GridView:

Hi Guys, I was searched many sites in google.But i doesn''t got the solution.

This is my GridView:

<asp:GridView 

        ID="gvwEditRequisition" 

        runat="server" 

        AutoGenerateColumns="False"

        CellPadding="3" Style="table-layout: auto; width: 1000px;" Height="240px"    

        OnRowCommand="gvwEditRequisition_RowCommand"

        AllowPaging="True" AllowSorting="True" 

        OnPageIndexChanging="gvwEditRequisition_PageIndexChanging"

        OnSorting="gvwEditRequisition_Sorting" BackColor="White" 

        BorderColor="#CCCCCC"

        BorderStyle="None" BorderWidth="1px">
        <Columns>
            <asp:TemplateField HeaderText="Approve" HeaderStyle-ForeColor="Blue">
                <ItemTemplate>
                    <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:LinkButton ID="lbnREQNO" runat="server" Text="REQNO" CommandArgument="REQNO"

                        CommandName="Sort"></asp:LinkButton>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblREQNO" runat="server" Text='<%# Eval("REQNO") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


protected void gvwEditRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Approve")
    {
        int index =  Int32.Parse(e.CommandArgument.ToString());  // here I got the error is : Input string was not in a correct format.
        Label lblReqNo = gvwEditRequisition.Rows[index].FindControl("lblREQNO") as Label;
        string s =Convert.ToString( lblReqNo);
        string ss = s;
        Response.Redirect("UpdateRequisition.aspx");
    }
}


我想将 ReqNO 转换为String.

请任何人帮我.....


I want ReqNO into String.

please any one help me.....

推荐答案

添加
CommandArgument 


连同


along with

<asp:TemplateField HeaderText="Approve" HeaderStyle-ForeColor="Blue">
            <ItemTemplate>
                <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>


如果您只需要查找索引,则可以通过以下方法进行:

If you only need to find the index then this is how you can do it:

LinkButton l = e.CommandSource as LinkButton; 
if(l != null)
{
 GridViewRow selectedRow = l.NamingContainer as GridViewRow ;
 if(selectedRow != null)
 {
  int intRowIndex = Convert.ToInt32(selectedRow.RowIndex);
 }
}



这样做有点粗略,但是可以.



Its a little crude way of doing it but it works.


您收到此错误,因为您没有在批准LinkButton中指定任何CommandArgument,但尝试访问它.

因此,如果要获取单击LinkButtonGridView行的索引,则需要将该行索引添加到CommandArgument 属性中,如下所示.
You are getting this error because you don''t have not specified any CommandArgument in the Approve LinkButton, but trying to access it.

So, if you want to get the index of the GridView row from which LinkButton is clicked then you need to add that row index in the CommandArgument property like below.
<ItemTemplate>
     <asp:LinkButton ID="lbnView" runat="server" Text="Approve" CommandName="Approve" CommandArgument="<%#((GridViewRow)Container).RowIndex%>">
     </asp:LinkButton>
</ItemTemplate>


正如我们现在将rowindex赋予CommandArgument一样,因此在执行代码时,您将获得单击按钮的行的索引.

请参考使用DataKey在GridView RowCommand事件中获取RowIndex [


As we have given the rowindex to the CommandArgument now, so when you execute the code, you will get the index of the row from which the button is clicked.

Refer Get RowIndex In GridView RowCommand Event Using DataKey[^] for more details.

Thanks...


这篇关于如何使用Asp.net在gridview中查找rowcommand事件的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆