[英]Gridview column linkbutton change the value according to data
I am trying to complete a simple task; 我正在尝试完成一个简单的任务; a
gridview column
gets an integer data if integer is equals to zero print "active" and set the commandname
of the linkbutton to active else set linkbutton text and command value to inactive. 一个
gridview column
如果整数是等于零打印“活性”,并设置得到的整数数据commandname
LinkButton的与活性,否则设置的LinkButton文本和指令值为无效。
here is what I have so far 这是我到目前为止所拥有的
<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>
codebehind 代码隐藏
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);
}
}
}
However when I Click the active linkbutton on the cell I get an error at onrowcommand
function 但是,当我单击单元格上的活动链接按钮时,
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;
....
How can I manage to change LinkButton
text and CommandName
depending on that integer data? 如何根据该整数数据更改
LinkButton
文本和CommandName
?
PS I tried Eval
but I couldnt figure out whats going on.... PS我尝试了
Eval
但我不知道发生了什么...。
I would simply return the data from your data source, as intended. 我只是按预期从您的数据源返回数据。 Then I would use an
Update Panel
to avoid the Postback
that will occur from your Link Button
. 然后,我将使用“
Update Panel
来避免从“ Link Button
发生的Postback
发。 As for the text modifying I would use Javascript, so Javascript will read the page on the client. 至于修改文本,我将使用Javascript,因此Javascript将读取客户端上的页面。 So when your data source returns:
因此,当您的数据源返回时:
The Javascript can analyze those rows, then modify the internal cell within the Grid
quite painless. Javascript可以分析那些行,然后非常轻松地修改
Grid
的内部单元。
An example of Javascript: Javascript的示例:
$(".Activator").each(function () {
if ($(this).prev().find(':checkbox').prop('checked')) {
$(this).text("Deactivate");
}
});
This example will validate if a checkbox is checked, if it is modify the internal text
to the class .Activator
. 本示例将验证是否选中了一个复选框,是否将内部
text
修改为.Activator
类。
Then the internal item in the Grid
for code on front-end would look like: 然后,用于前端代码的
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>
As you can see, as the internal data is changed the Javascript will automatically adjust the value for me every time the Data Source
is binded again. 如您所见,随着内部数据的更改,每次再次绑定
Data Source
时,Javascript都会自动为我调整值。 Which the CustomerCheck_Click
would execute the code on the server, which will read the Command Argument
which contains the Row Id
. CustomerCheck_Click
将在服务器上执行该代码,该服务器将读取包含Row Id
的Command Argument
。 Which will Update
that particular record without a problem and rebind the Grid
. 它将毫无问题地
Update
该特定记录并重新绑定Grid
。 (Partially lied, I'm parsing the Tooltip
). (部分说谎,我正在解析
Tooltip
)。
You would instantiate on server side like: 您将在服务器端实例化:
((LinkButton)sender).ToolTip;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.