用户界面功能:我的GridView
列数很少。最重要的列是 PcCode ,每行显示string
值。
预期:当我点击该PcCode列的一行中的一个单元格时,应显示另一个GridView
。但是,当我尝试将asp:LinkButton
用于单元格时,事情就不起作用了。当我点击RowCommand
单元格中的asp:LinkButton
时,GridView
不会被触发。我哪里做错了?哪种方式可以实现预期的功能?在此先感谢帮助新手。
在以下代码中,我尝试获取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();
}
}
选择行后,我需要将所选行的值作为字符串,并与listitem进行比较以显示新的GridView。
尝试
使用Link_Button_Click(对象发件人,EventArgs e)和以下但失败。
protected void _UIProfitcenterTotalGridView_RowCommand(object sender,GridViewCommandEventArgs e) { if(e.CommandName ==“Select”) {string ProfitcenterCode =((GridViewRow)(((LinkButton)e.CommandSource).NamingContainer))。Cells [2] .Text; } }
答案 0 :(得分:3)
我尝试使用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);
}
答案 1 :(得分:1)
您可以使用发件人对象(调用事件的链接按钮)并从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 %>'>