[英]Asp linkbutton text depending on checkbox control in gridview
Is it possible to change the text of a linkbutton depending on the state of a checkbox in a gridview column? 是否可以根据gridview列中复选框的状态来更改linkbutton的文本?
View: 视图:
<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>
I'd like the text to change to "Mark Unapproved" if IsVerified is true. 如果IsVerified为true,我希望文本更改为“标记为未批准”。
Codebehind: 代码背后:
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;
}
}
复选框上有一个可用于进行任何更改的事件,其CheckedChanged事件为https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.checkedchanged(v = vs.110).aspx
Add this to your .cs file: (assume CheckBox1 is the id that you will have to add to your checkbox) 将此添加到您的.cs文件:(假设CheckBox1是您必须添加到复选框的ID)
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = CheckBox1 as CheckBox;
if (chk != null && chk.Checked)
{
approveBtn.Text ="Mark Unapproved"
}
}
A colleague came up with the following solution: 一位同事提出了以下解决方案:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { 受保护的void GridView1_RowDataBound(object sender,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";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.