【发布时间】:2013-02-21 17:11:23
【问题描述】:
UI 功能:我有一个 GridView,但列很少。最重要的列是 PcCode,它显示每一行的 string 值。
预期:当我单击该 PcCode 列的一行中的一个单元格时,应显示另一个 GridView。但是,当我尝试将asp:LinkButton 用于单元格时,事情就不起作用了。当我单击GridView 单元格中的asp:LinkButton 时,RowCommand 不会被触发。我在哪里做错了?哪种方式可以实现预期的功能?在此先感谢您帮助新手。
在下面的代码中,我试图获取RowIndex 并将其传递给CommandArgument 并使用CommandName。
.aspx 代码
<asp:GridView ID="_UIProfitcenterTotalGridView" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="22" ShowFooter="True" AutoGenerateColumns="False"
DataKeyNames="ProfitcenterCode" EnableViewState="False"
OnRowDataBound="_UIProfitcenterTotalGridView_RowDataBound"
OnPageIndexChanging="_UIProfitcenterTotalGridView_PageIndexChanging"
OnRowCommand="_UIProfitcenterTotalGridView_OnRowCommand">
<Columns>
<asp:TemplateField HeaderText="PcCode" InsertVisible="False"
ShowHeader="False" SortExpression="ProfitcenterCode" FooterText="Total" FooterStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="_UIPCCodeLinkButton" runat="server" Text='<%# Eval("ProfitcenterCode") %>'
CommandName="Select"
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
...
aspx.cs 的代码
protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = _UIProfitcenterTotalGridView.Rows[index];
string ProfitcenterCode = _UIProfitcenterTotalGridView.DataKeys[_UIProfitcenterTotalGridView.SelectedIndex].Values["ProfitcenterCode"].ToString();
}
}
选择行后,我需要将所选行的值作为字符串并与列表项进行比较以显示新的 GridView。
试过
-
使用 Link_Button_Click(Object sender, EventArgs e) 和以下但失败。
protected void _UIProfitcenterTotalGridView_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "选择") {string ProfitcenterCode = ((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer)).Cells[2].Text; } }
【问题讨论】:
标签: asp.net gridview asplinkbutton rowcommand