网格视图列链接按钮根据数据更改值
本文关键字:数据 视图 链接 按钮 网格 | 更新日期: 2023-09-27 18:25:40
我正在尝试完成一项简单的任务;如果integer等于零,则CCD_ 1获取整数数据打印"活动"并将链接按钮的CCD_ 2设置为活动,否则将链接按钮文本和命令值设置为非活动。
这是我到目前为止的
<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
文本和CommandName
?
p.S我试过Eval
,但我不知道发生了什么…
我只需按预期返回数据源中的数据。然后我会使用Update Panel
来避免Postback
,它将从您的Link Button
中出现。至于文本修改,我会使用Javascript,所以Javascript会读取客户端上的页面。因此,当您的数据源返回时:
- 一
- 一
- 两个
- 一
Javascript可以分析这些行,然后非常轻松地修改gridview column
0中的内部单元格。
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>
正如您所看到的,随着内部数据的更改,每次再次绑定Data Source
时,Javascript都会自动为我调整值。哪个CustomerCheck_Click
将在服务器上执行代码,哪个将读取包含Row Id
的Command Argument
。这将毫无问题地Update
该特定记录并重新绑定Grid
。(部分撒谎,我正在解析commandname
0)。
您可以在服务器端实例化如下:
((LinkButton)sender).ToolTip;