Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在gridview中如何对按钮使用rowcommand事件_C#_Asp.net_Button_Gridview_Rowcommand - Fatal编程技术网

C# 在gridview中如何对按钮使用rowcommand事件

C# 在gridview中如何对按钮使用rowcommand事件,c#,asp.net,button,gridview,rowcommand,C#,Asp.net,Button,Gridview,Rowcommand,我在aspx中使用gridview,我有两个页面,分别是注册和详细信息。aspx注册完成后,应转到详细信息页面。aspx我在GV中保留了一个gridview,我应该为按钮使用行命令事件。它应显示学生的所有结果,并将编辑按钮作为所有结果的最后一列学生们,我使用了项目模板。但是在row命令事件中,我不知道要写什么函数,如果用户单击edit,它应该使用userid转到编辑页面,id应该是可编辑模式,其他字段可以编辑 details.aspx <asp:GridView ID="GridVi

我在aspx中使用gridview,我有两个页面,分别是注册和详细信息。aspx注册完成后,应转到详细信息页面。aspx我在GV中保留了一个gridview,我应该为按钮使用行命令事件。它应显示学生的所有结果,并将编辑按钮作为所有结果的最后一列学生们,我使用了项目模板。但是在row命令事件中,我不知道要写什么函数,如果用户单击edit,它应该使用userid转到编辑页面,id应该是可编辑模式,其他字段可以编辑

details.aspx

   <asp:GridView ID="GridView3" runat="server" CellPadding="3" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"  OnRowCommand="GridView3_RowCommand" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
         <Columns>
             <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/>
             <asp:BoundField DataField="Password" HeaderText="Password" />
             <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
             <asp:BoundField DataField="LastName" HeaderText="LastName" />
             <asp:BoundField DataField="EMail" HeaderText="Emaid-ID" />
             <asp:BoundField DataField="PhoneNo" HeaderText="Phone Number" />
             <asp:BoundField DataField="Location" HeaderText="Location" />

              <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                   <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="UserName"/>
                </ItemTemplate>          
             </asp:TemplateField> 
         </Columns>
     <EditRowStyle BackColor="#999999" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
     <SortedAscendingCellStyle BackColor="#E9E7E2" />
     <SortedAscendingHeaderStyle BackColor="#506C8C" />
     <SortedDescendingCellStyle BackColor="#FFFDF8" />
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
 </asp:GridView>
    </div>
</form>

}

首先,您的按钮控件
CommandArgument
属性必须在每行中具有唯一的值:

 <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" 
             CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
有两种方法可以做到这一点

方法1

请在标记中更改这些内容

  • 更改
    CommandName=“EditUserName”
  • 省略
    命令参数
    。我们不需要这个
  • 代码隐藏

    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditUserName")
        {
            //first find the button that got clicked
            var clickedButton = e.CommandSource as Button;
            //find the row of the button
            var clickedRow = clickedButton.NamingContainer as GridViewRow;
            //now as the UserName is in the BoundField, access it using the cell index.
            var clickedUserName = clickedRow.Cells[0].Text;
        }
    }
    
    方法2

    给出一个
    命令参数
    。你可以给出很多不同的论点

  • CommandArgument=”“
  • CommandArgument=”“
  • CommandArgument=”“
    ()
  • 现在在代码中,这样做

    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditUserName")
        {            
            var clickedUserName = CustomersTable
                .Rows[Convert.ToInt32(e.CommandArgument)]//find the row with the clicked index
                .Cells[0]//find the index of the UserName cell
                .Text;//grab the text
        }
    }
    
    附言:

    1.更改命令名的原因是,如果
    CommandName=“Edit”
    ,它将触发
    RowEditing
    事件,该事件将导致此错误

    GridView“GridView3”触发了未处理的事件行编辑


    2.如果(!IsPostBack)或以上方法无效,则将页面加载代码放入
    中。

    uh-oh。。别忘了把数据绑定代码放在加载页面内
    如果(!IspostBack)
    你好Naveen和Ali Shahrokhi谢谢你的回答我尝试了这两种方法并实现了非常感谢。。。现在工作正常,我发现使用“Edit”作为命令名将始终执行默认编辑。需要使用“myedit”或其他一些命令字符串。
    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditUserName")
        {
            //first find the button that got clicked
            var clickedButton = e.CommandSource as Button;
            //find the row of the button
            var clickedRow = clickedButton.NamingContainer as GridViewRow;
            //now as the UserName is in the BoundField, access it using the cell index.
            var clickedUserName = clickedRow.Cells[0].Text;
        }
    }
    
    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditUserName")
        {            
            var clickedUserName = CustomersTable
                .Rows[Convert.ToInt32(e.CommandArgument)]//find the row with the clicked index
                .Cells[0]//find the index of the UserName cell
                .Text;//grab the text
        }
    }