【问题标题】:Gridview column linkbutton change the value according to dataGridview列linkbutton根据数据改变值
【发布时间】:2014-10-07 02:18:27
【问题描述】:

我正在尝试完成一个简单的任务; gridview column 获取整数数据,如果整数等于零打印“活动”并将链接按钮的commandname 设置为活动,否则将链接按钮文本和命令值设置为非活动。

这是我目前所拥有的

<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,但我不知道发生了什么......

【问题讨论】:

  • linkButton.CommandArgument = e.Row.RowIndex.ToString(); 这解决了问题

标签: c# asp.net gridview


【解决方案1】:

我只是按照预期从您的数据源返回数据。然后我会使用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>

如您所见,随着内部数据的更改,每次再次绑定 Data Source 时,Javascript 都会自动为我调整值。其中CustomerCheck_Click 将在服务器上执行代码,这将读取包含@​​987654333@ 的Command Argument。这将使Update 没有问题的特定记录并重新绑定Grid。 (部分撒谎,我正在解析Tooltip)。

你会像这样在服务器端实例化:

((LinkButton)sender).ToolTip;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多