2015-06-21 71 views
0

取决于GridView列中复选框的状态,是否可以更改链接按钮的文本?取决于GridView中的复选框控件的Asp链接按钮文本

查看:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Id" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowCommand="FireRowCommand" > 
<AlternatingRowStyle BackColor="White" /> 
<Columns> 
    <asp:CommandField ShowEditButton="False" ShowDeleteButton="False" /> 
    <asp:TemplateField HeaderText="Approve"> 
     <ItemTemplate> 
      <asp:LinkButton ID="approveBtn" runat="server" CommandArgument='<%# Eval("Id") %>' 
       CommandName="ApproveCmd" Text="Mark Approved" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" /> 
    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> 
    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" /> 
    <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" /> 
    <asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" /> 
    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> 
    <asp:BoundField DataField="BusinessName" HeaderText="BusinessName" SortExpression="BusinessName" /> 
    <asp:CheckBoxField DataField="IsVerified" HeaderText="IsVerified" SortExpression="IsVerified" /> 
    <asp:CheckBoxField DataField="IsNewsletter" HeaderText="IsNewsletter" SortExpression="IsNewsletter" Visible="false"/> 
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" /> 
</Columns> 

我想文字更改为“马克未批准”如果IsVerified是真实的。

代码隐藏:

protected void FireRowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      var command = e.CommandName; 

      var clientId = Convert.ToInt32(e.CommandArgument); 

      switch (command) 
      { 

       case "ApproveCmd": 

        var service = new WholesaleClientService(); 
        service.ToggleClientVerfication(clientId); 
        break; 
      } 
     } 

回答

0

添加到您的.cs文件:(假设CheckBox1

protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
{ 
    CheckBox chk = CheckBox1 as CheckBox; 
    if (chk != null && chk.Checked) 
    { 
     approveBtn.Text ="Mark Unapproved" 
    } 
} 
+0

感谢。但是,我没有在我的复选框上获得.Checked属性。我错过了什么吗? –

+0

编辑我的回复,但你可能需要定制它 –

+0

这工作,除了我发现我不能添加一个ID属性到我的GridView的复选框。想想我必须改变我实施这个的方式。对不起,我不擅长asp.net。我用更多的代码更新了这个问题,以便更好地了解我拥有的东西。 –

0

一位同事想出了FOLL,你将不得不添加到您的复选框ID)由于溶液:

保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E) {

 if (e.Row.RowType == DataControlRowType.DataRow) 
     {     
      var check = ((CheckBox)(e.Row.Cells[9].Controls[0])).Checked; 

      if (check == true) 
      { 
       LinkButton btn = (LinkButton)e.Row.Cells[1].FindControl("approveBtn"); 
       btn.Text = "Mark Unapproved"; 
      } 
     } 
    } 
相关问题