关于显示复选框 [英] About displaying checkbox

查看:112
本文介绍了关于显示复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在wat的gridview中选中复选框,其他数据来自datbase
我想如果有多个行,则复选框不可见
怎么做

I am taking checkbox in gridview this wat and other data is coming from datbase
i want if there is more than one row then checkbox should not be visible
how to do that

<asp:TemplateField>
<itemtemplate>
<asp:CheckBox ID="LinkAssignButton" runat="server"

CommandName="Assign" 

Text="Take" Visible="true">

--%>

</itemtemplate>



[edit]已添加代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

您好,可以使用CSS或服务器端代码以多种方式完成此操作.在这里,我给出了3种不同的可能性.


-aspx代码-
Hi, this can be done in many ways, either by using css or server side code. here i have given 3 different possiblities.


-- aspx code --
<asp:GridView ID="UsersGv" runat="server" AutoGenerateColumns="false" OnRowDataBound="UsersGvRowDataBound" ShowFooter="true" ShowHeader="true" AllowPaging="true"

                   RowStyle-Font-Names="arial" HeaderStyle-BackColor="Gray" HeaderStyle-Font-Names="arial"

                   >
                       <Columns>
                           <asp:TemplateField>
                               <ItemTemplate>
                                   <asp:CheckBox ID="LinkAssignButton" runat="server" CommandName="Assign" Text="Take"

                                       Visible="true" />
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="First Name">
                               <ItemTemplate>
                                   <asp:Label ID="FirstNameGvLbl" runat="server" Text='<% #Eval("FirstName") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                            <asp:TemplateField HeaderText="Middle Name">
                               <ItemTemplate>
                                   <asp:Label ID="MiddleNameGvLbl" runat="server"  Text='<% #Eval("MiddleName") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                            <asp:TemplateField HeaderText="Last Name">
                               <ItemTemplate>
                                   <asp:Label ID="LastNameGvLbl" runat="server"  Text='<% #Eval("LastName") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                       </Columns>
                   </asp:GridView>




-.cs代码---

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Data;
公共局部类_Default:System.Web.UI.Page
{
///< summary>
///该属性保存数据源中存在的用于绑定网格的行数.
///</summary>
私有int NoOfRowsInGridDataSource
{
得到;
设置;
}
受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
{
BindUsers();
}
}
///< summary>
///UsersGvRowDataBound-GridView Row数据绑定时会触发此事件.
///如果NoOfRowsInGridDataSource值大于1,则添加逻辑以隐藏复选框 ///</summary>
///< param name ="sender"></param>
///< param name ="e"></param>
受保护的void UsersGvRowDataBound(对象发送者,GridViewRowEventArgs e)
{
如果(NoOfRowsInGridDataSource> 1)
{
//这将使用css
隐藏整个复选框列 //取消注释下面的行,并注释其余代码-解决方案1 ​​
//e.Row.Cells[0].Attributes.Add("style," display:none);
//这将隐藏整个复选框列,这是Serer附加代码
//取消注释下面的行,并注释其余代码-解决方案2
//e.Row.Cells[0].Visible = false;
//这将是一个复选框,但不会隐藏该列,因此该复选框处于隐藏状态,但您仍然可以看到带有页眉和页脚等的列,这是服务程序的侧面代码
//这是-解决方案3
如果(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox LinkAssignButton =(CheckBox)e.Row.Cells [0] .FindControl("LinkAssignButton");
如果(LinkAssignButton!= null)
{
//隐藏该复选框的服务器端代码.
LinkAssignButton.Visible = false;
//css隐藏该复选框.
LinkAssignButton.Attributes.Add("style","display:none");
}
}
}
}
私有void BindUsers()
{
DataTable用户=新的DataTable();
users.Columns.Add("FirstName");
users.Columns.Add("MiddleName");
users.Columns.Add("LastName");
users.Rows.Add(users.NewRow());
users.Rows [users.Rows.Count-1] ["FirstName"] =名字1";
users.Rows [users.Rows.Count-1] ["MiddleName"] =中间名1";
users.Rows [users.Rows.Count-1] ["LastName"] =姓氏1";
users.Rows.Add(users.NewRow());
users.Rows [users.Rows.Count-1] ["FirstName"] =名字2";
users.Rows [users.Rows.Count-1] ["MiddleName"] =中间名2";
users.Rows [users.Rows.Count-1] ["LastName"] =姓氏2";
//在绑定网格之前,使用网格视图数据源中的行数或记录数设置NoOfRowsInGridDataSource属性的值.
NoOfRowsInGridDataSource = users.Rows.Count;
UsersGv.DataSource =用户;
UsersGv.DataBind();
}
}


希望以上代码对您有所帮助,如果您还有其他需要或有任何疑问,请告诉我.




--- .cs code ---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
/// <summary>
/// This propery is holds the number of rows exists in the datasource, which is used to bind the grid.
/// </summary>
private int NoOfRowsInGridDataSource
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUsers();
}
}
/// <summary>
/// UsersGvRowDataBound - This event is fired when the GridView Row data bounds.
/// Add a logic to hide the checkbox if the NoOfRowsInGridDataSource value is greater than 1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void UsersGvRowDataBound(object sender, GridViewRowEventArgs e)
{
if (NoOfRowsInGridDataSource > 1)
{
// This will hide the entire check box column, using css
// UnComment the below line and comment rest of the code -- Solution 1
//e.Row.Cells[0].Attributes.Add("style", "display:none");
// This will hide the entire check box column, this is serer side code
// UnComment the below line and comment rest of the code -- Solution 2
//e.Row.Cells[0].Visible = false;
// This will check box, but does not hide the column, so the check box is hidden but still you can see the column with header and footer etc., this is serer side code
// This is -- Solution 3
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox LinkAssignButton = (CheckBox)e.Row.Cells[0].FindControl("LinkAssignButton");
if (LinkAssignButton != null)
{
//Serverside code to hide the checkbox.
LinkAssignButton.Visible = false;
//css to hide the checkbox.
LinkAssignButton.Attributes.Add("style", "display:none");
}
}
}
}
private void BindUsers()
{
DataTable users = new DataTable();
users.Columns.Add("FirstName");
users.Columns.Add("MiddleName");
users.Columns.Add("LastName");
users.Rows.Add(users.NewRow());
users.Rows[users.Rows.Count - 1]["FirstName"] = "First Name 1";
users.Rows[users.Rows.Count - 1]["MiddleName"] = "Middle Name 1";
users.Rows[users.Rows.Count - 1]["LastName"] = "Last Name 1";
users.Rows.Add(users.NewRow());
users.Rows[users.Rows.Count - 1]["FirstName"] = "First Name 2";
users.Rows[users.Rows.Count - 1]["MiddleName"] = "Middle Name 2";
users.Rows[users.Rows.Count - 1]["LastName"] = "Last Name 2";
//Before binding the grid, set the value of NoOfRowsInGridDataSource property with the number of rows or count of the records which are in the grid view datasource.
NoOfRowsInGridDataSource = users.Rows.Count;
UsersGv.DataSource = users;
UsersGv.DataBind();
}
}


Hope the above code helps you, let me know if you need anything else or if you have any questions.


这篇关于关于显示复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆