0

I currently have an ASP DataGrid with a ButtonColumn in it, like this:-

<asp:DataGrid id="gradesGrid"
              AutoGenerateColumns="true"
              runat="server"
              OnSelectedIndexChanged="GradesDataGridSelectedCallback">
    <Columns>
        <asp:ButtonColumn HeaderText=""
                          ButtonType="LinkButton"
                          Text="Graph"
                          CommandName="Select">
        </asp:ButtonColumn>
    </Columns>
</asp:DataGrid>

and this works perfectly; when the button column is clicked, the GradesDataGridSelectedCallback function is invoked and all is marvellous. I now need to add a second button column to this data grid, to perform a different function related to the grid item. I add the extra code:-

<asp:ButtonColumn HeaderText=""
                  ButtonType="LinkButton"
                  Text="Display"
                  CommandName="NewFunction">
</asp:ButtonColumn>

This displays as expected but clicking the second button (although it causes a post-back), doesn't invoke the GradesDataGridSelectedCallback function. The question is, how to I wire up this second ButtonColumn to a particular function in the C# codebehind?

Alternatively, if I specify the button column so:-

<asp:ButtonColumn HeaderText=""
                  ButtonType="LinkButton"
                  Text="Display"
                  CommandName="Select">
</asp:ButtonColumn>

then the GradesDataGridSelectedCallback does get invoked, but I can't see any way of determining which ButtonColumn was clicked. Is there a way, and if so what is it?

4

2 回答 2

1

使用ItemCommand事件而不是 OnSelectedIndexChanged它会为每个按钮触发。

<asp:DataGrid ID="dtGrg" runat="server" AutoGenerateColumns="true" 
                            onitemcommand="dtGrg_ItemCommand">
                            <Columns>
                                <asp:ButtonColumn HeaderText="" ButtonType="LinkButton"  Text="Graph"  CommandName="Select">
                                </asp:ButtonColumn>
                                <asp:ButtonColumn HeaderText="" ButtonType="LinkButton" Text="Display" CommandName="NewFunction" >
                                </asp:ButtonColumn>
                            </Columns>
                        </asp:DataGrid>

protected void dtGrg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "NewFunction")
            { 
             //Your Code Here :
            }
             if (e.CommandName == "Select")
            {
                //Your Code Here :
            }
        }

OnSelectedIndexChanged仅适用于选择按钮。

于 2013-07-30T14:44:28.247 回答
1

代替

OnSelectedIndexChanged="GradesDataGridSelectedCallback"

利用

OnItemCommand ="GradesDataGridSelectedCallback"

并将您的定义GradesDataGridSelectedCallback

Protected void GradesDataGridSelectedCallback(Object source , DataGridCommandEventArgs e)

End Sub

检查e.CommandName将使您指示单击了哪个按钮。

于 2013-07-30T14:06:02.480 回答