日期:2014-05-18  浏览次数:20441 次

在gridview的RowCommand事件中改变button控件的text的值
<ItemTemplate>
  <asp:Button ID="Activate" runat="server" CommandArgument='<%# Eval("userid") %>' CommandName="activate" Text="激活" Width="50px"/>
  <asp:Button ID="Forbid" runat="server" CommandArgument='<%# Eval("userid") %>' CommandName="modify" Text="修改" Width="50px" />
  <asp:Button ID="Recommend" runat="server" CommandArgument='<%# Eval("userid") %>' CommandName="Recommend" Text="推荐" Width="50px"/>
  <asp:Button ID="BtnDelete" runat="server" CommandArgument='<%# Eval("userid") %>' CommandName="deletes" Text="删除" Width="50px" OnClientClick="return confirm('确定要删除吗?')"/>
  </ItemTemplate>
后台代码:
if (e.CommandName == "Recommend")
  {
  Components.Model.P_User model = new Components.Model.P_User();
  BLL.P_User bll = new BLL.P_User();
  model = bll.GetModel(Convert.ToInt32(e.CommandArgument));
  Button btn = ((Button)e.CommandSource) as Button;
  btn.Enabled = false;  
  if (model.Recommend == false)
  {
  btn.Text = "推荐";
  UpdateRecommend(Convert.ToInt32(e.CommandArgument), true);
  }
  else
  {
  btn.Text = "不推荐";
  UpdateRecommend(Convert.ToInt32(e.CommandArgument), false);
  }
想在点击后改变button的text的值。

------解决方案--------------------
Button btn = e.Row.findControl("Recommend") as Button;
Button btn = e.Item.Cells[0].FindControl("Recommend") as Button;

------解决方案--------------------
C# code
    string editFlag = ""; 
    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (e.Item.UniqueID!= editFlag)
            {
                ((Button)(e.Item.FindControl("DeleteButton"))).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
            }
        }
    }
    protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        ListViewDataItem lvitem = (ListViewDataItem)ListView1.Items[e.NewEditIndex];
        editFlag = lvitem.UniqueID;
    }