2015-06-21 77 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"; 
      } 
     } 
    } 
相關問題