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>
...
asp .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(对象发送方,gridviewcommanddeventargs e){if (e.CommandName == "Select"){string ProfitcenterCode = ((GridViewRow)(((LinkButton) . commandsource).NamingContainer)).Cells[2].Text;}}
我尝试使用LinkButton_Click()
事件而不是RowCommand
作为jason建议的:
protected void LinkButton_Click(Object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
GridViewRow row = (GridViewRow)button.NamingContainer;
if (row != null)
{
string theValue = ((LinkButton)sender).CommandArgument.ToString();
...
...
//code for the extra thing I needed to do after selecting a cell value.
}
}
然而,我仍然有问题,我弄清楚了。问题是LinkButton
没有绑定到行,因此,它不能在选择时传递任何值。缺少的是以下代码:
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = e.Row.Cells[0].Text; //value of the first column from the grid
linkButton.Enabled = true;
linkButton.Click += new EventHandler(LinkButton_Click); //triggering the LinkButton event here.
e.Row.Cells[0].Controls.Add(linkButton);
}
您可以使用sender对象(调用事件的链接按钮)并从gridview中获取父行。例子:
Protected Sub linkButton_click(ByVal sender As Object, ByVal e As EventArgs)
'First cast the sender to a link button
Dim btn As LinkButton = CType(sender, LinkButton)
'now, if there is a command arguement, you can get that like this
Dim id As String = btn.CommandArgument
'to get the gridviewrow that the button is on:
Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)
End Sub
很难准确地了解你在寻找什么,所以如果我错过了什么,请告诉我,我会补充的。
protected void linkButton_click(object sender, EventArgs e)
{
//First cast the sender to a link button
LinkButton btn = (LinkButton)sender;
//now, if there is a command arguement, you can get that like this
string id = btn.CommandArgument;
//to get the gridviewrow that the button is on:
GridViewRow row = (GridViewRow)btn.NamingContainer;
}
并将链接按钮更改为:
<asp:LinkButton OnClick="linkButton_click" ID="_UIPCCodeLinkButton"
runat="server" Text='<%# Eval("ProfitcenterCode") %>'
CommandName="Select"
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'>