1

I am having trouble when trying to fire a button in a GridView with the parameter CommandName = "x", I try to reach my "If" in the GridView1_RowCommand event but I just cant for some reason, if you could help me I'd be gratefull .

This is my .aspx part

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
 <ContentTemplate>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4" 
            EnableModelValidation="True" ForeColor="#333333" GridLines="None" 
            Height="193px" Width="968px" AllowPaging="True" AllowSorting="True" 
            AutoGenerateColumns="False" DataKeyNames="ID" 
            DataSourceID="SOURCE1" onrowcommand="GridView1_RowCommand">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:Button Text = "Seleccionar" runat="server" CommandName="X" CommandArgument='<%# Container.DataItemIndex %>' />
                </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="IDEmpresa" HeaderText="IDEmpresa" 
                    SortExpression="IDEmpresa" />
            </Columns>
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        </asp:GridView> 
   </ContentTemplate>
</asp:UpdatePanel>

And this is my C# code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "X")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridView1.Rows[index];
        Label1.Text = "WOW It reached out";
    }
}

I followed the instructions of the ASP.net page, and im fairly new to .net (I dont know if the UpdatePanel has something to do with it)

4

1 回答 1

1

进行快速测试,您提供的代码有效,但我确实必须连接我自己的数据源。

您在这里缺少的是 Label1 在您的 UpdatePanel 之外,并且不会根据您在 UpdatePanel 中的本地化回发刷新。

GridViews 和 UpdatePanels/Buttons 的注意事项。确保您没有在 Page_Load 期间手动绑定/重新绑定,如果您有执行此操作的代码,请在if(!IsPostBack) { }语句中执行此操作。

于 2012-07-03T20:00:20.720 回答