Gridview列链接按钮根据数据更改值
问题描述:
我想完成一个简单的任务; a gridview column
如果整数等于零,则获取整数数据打印“激活”并将链接按钮的commandname
设置为激活,否则将链接按钮文本和命令值设置为不激活。Gridview列链接按钮根据数据更改值
这里是我迄今为止
<Columns>
<asp:BoundField HeaderText = "User Name" DataField="UserName"
HeaderStyle-Width="16%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText = "Role" DataField="RoleNAME"
HeaderStyle-Width="14%" ItemStyle-HorizontalAlign="Center" />
<asp:BoundField HeaderText = "Group" DataField="GroupName"
HeaderStyle-Width="12%" ItemStyle-HorizontalAlign="Center" />
<asp:ButtonField ButtonType="link" CommandName = "Active"
HeaderText="Status" Text="Active"
HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
<asp:ButtonField ButtonType="link" CommandName = "Edit"
HeaderText="" Text="Edit/View"
HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center" />
</Columns>
代码隐藏
protected void grdMyQueue_RowdataBound(object sender, GridViewRowEventArgs e)
{
// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
var obj = (User)e.Row.DataItem;
if (obj.isDisabled == 0)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = "Active";
linkButton.Enabled = true;
linkButton.CommandName = "Active";
//linkButton.CommandArgument = e.Row. //this might be causing the problem
e.Row.Cells[3].Controls.Clear();
e.Row.Cells[3].Controls.Add(linkButton);
}
else
{
LinkButton linkButton = new LinkButton();
linkButton.Text = "InActive";
linkButton.Enabled = true;
linkButton.CommandName = "InActive";
e.Row.Cells[3].Controls.Add(linkButton);
}
}
}
然而,当我点击细胞我在onrowcommand
功能
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument); // this is the error line
GridViewRow gvRow = userManager.Rows[index];
TableCell usrName = gvRow.Cells[0];
string userName = usrName.Text;
....
得到一个错误的积极的LinkButton我如何设法更改LinkButton
文本和CommandName
取决于整数数据?
P.S我试图Eval
,但我不能弄清楚怎么回事....
答
我会简单地从数据源返回数据,如预期。然后,我会使用Update Panel
来避免Postback
将从您的Link Button
发生。至于文本修改,我会使用Javascript,所以JavaScript将读取客户端上的页面。所以,当您的数据源返回:
- 一个
- 一个
- 两个
- 一个
,JavaScript会分析这些行,然后修改Grid
相当无痛人流中的内部单元。
的Javascript的一个例子:如果一个复选框被选中
$(".Activator").each(function() {
if ($(this).prev().find(':checkbox').prop('checked')) {
$(this).text("Deactivate");
}
});
这个例子将验证,如果是修改内部text
到类.Activator
。
然后在Grid
对前端代码的内部项目看起来像:
<asp:TemplateField HeaderText="Active">
<ItemTemplate>
<asp:CheckBox
ID="CustomerCheck" runat="server"
Checked='<%# Eval("Active") %>'
Enabled="false" />
<asp:LinkButton
ID="lbCustomerActive" runat="server"
Text="Activate"
CommandName="OnActivate"
ToolTip='<%# "Activate or Deactivate Course: " + Eval("CourseID") %>'
OnClick="CustomerCheck_Click"
CssClass="Activator">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
正如你可以看到,作为内部数据被更改的JavaScript会自动为每一次我调整值Data Source
再次绑定。其中CustomerCheck_Click
将执行服务器上的代码,该代码将读取包含Row Id
的Command Argument
。其中Update
该特定记录没有问题,并重新绑定Grid
。 (部分谎言,我正在解析Tooltip
)。
你会实例化在服务器端,如:
((LinkButton)sender).ToolTip;
'linkButton.CommandArgument = e.Row.RowIndex.ToString();'这解决了问题 – 2014-10-06 19:04:49