C#.net怎样判断gridview中某一列的是否为空
我在gridview中的一列加入Label,此列中还共同存在一个linkbutton按钮,怎样判断当Label为空时,Linkbutton显示,Label中有字段时,Li...
我在gridview中的一列加入Label,此列中还共同存在一个linkbutton按钮,怎样判断当Label为空时,Linkbutton显示,Label中有字段时,LinkButton的显示为False,Label中的字段显示为true
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ><%# Eval("questionplan") %></asp:Label>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandArgument='<%# Eval("ID") %>'
CommandName="EditProducts" Text="处理"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
我要显示的每行都不一样,如第一行的Label里有值,Button隐藏,Label显示,第二行Label里有值就显示Label,值为空就显示Button。就是每行的Label(只有一个)里有值就显示,没值就隐藏,显示Button。应用到整行,不是整列 展开
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" ><%# Eval("questionplan") %></asp:Label>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandArgument='<%# Eval("ID") %>'
CommandName="EditProducts" Text="处理"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="150px" />
</asp:TemplateField>
我要显示的每行都不一样,如第一行的Label里有值,Button隐藏,Label显示,第二行Label里有值就显示Label,值为空就显示Button。就是每行的Label(只有一个)里有值就显示,没值就隐藏,显示Button。应用到整行,不是整列 展开
3个回答
展开全部
我做了一个例子,测试通过了,绝对没有问题:
前台:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Ref_ID" />
<asp:BoundField DataField="User_ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#Eval("Sys") %>'></asp:Label>
<asp:LinkButton runat="server" ID="LinkButton1" Text="留言"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
private void Page_Load(object sender, EventArgs e)
{
//TextBox2.Text = "aaaaaaaa\r\nbbbbb";
if (!Page.IsPostBack)
{
//DateTime dt = DateTime.Now.ToString();
//GridView2.DataSource = ds;
//GridView2.DataBind();
DataSet ds = getSys();
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
Label lb1;
LinkButton lbl1;
for (int i = 0; i < GridView2.Rows.Count; i++)
{
lb1 = (Label)GridView2.Rows[i].FindControl("Label1");
lbl1 = (LinkButton)GridView2.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lbl1.Visible = false;
}
else //if (lb1.Text.Trim() == "True")
{
lbl1.Visible = true;
}
}
}
//int[] num = new int[5];
//string aa = "";
//for (i = 0; i < 5; i++)
//{
// num[i] = randoms();
// aa = aa + num[i].tostring();
// message.show(aa);
//}
}
public DataSet getSys()
{
try
{
//string FoxconnCAUserDBConnString = System.Configuration.ConfigurationManager.AppSettings["FCARegConnString"].ToString();
string DBConnString = "Data Source=10.153.26.252;Initial Catalog=FCAReg;Persist Security Info=True;User ID=causer;Password=foxconnca";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(DBConnString);
string pSQL = "select * from ref_usersreg_sys";
SqlCommand cmd = new SqlCommand(pSQL, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
return ds;
}
catch
{
return null;
}
}
前台:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Ref_ID" />
<asp:BoundField DataField="User_ID" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="Label1" Text='<%#Eval("Sys") %>'></asp:Label>
<asp:LinkButton runat="server" ID="LinkButton1" Text="留言"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台:
private void Page_Load(object sender, EventArgs e)
{
//TextBox2.Text = "aaaaaaaa\r\nbbbbb";
if (!Page.IsPostBack)
{
//DateTime dt = DateTime.Now.ToString();
//GridView2.DataSource = ds;
//GridView2.DataBind();
DataSet ds = getSys();
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
Label lb1;
LinkButton lbl1;
for (int i = 0; i < GridView2.Rows.Count; i++)
{
lb1 = (Label)GridView2.Rows[i].FindControl("Label1");
lbl1 = (LinkButton)GridView2.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lbl1.Visible = false;
}
else //if (lb1.Text.Trim() == "True")
{
lbl1.Visible = true;
}
}
}
//int[] num = new int[5];
//string aa = "";
//for (i = 0; i < 5; i++)
//{
// num[i] = randoms();
// aa = aa + num[i].tostring();
// message.show(aa);
//}
}
public DataSet getSys()
{
try
{
//string FoxconnCAUserDBConnString = System.Configuration.ConfigurationManager.AppSettings["FCARegConnString"].ToString();
string DBConnString = "Data Source=10.153.26.252;Initial Catalog=FCAReg;Persist Security Info=True;User ID=causer;Password=foxconnca";
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(DBConnString);
string pSQL = "select * from ref_usersreg_sys";
SqlCommand cmd = new SqlCommand(pSQL, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
return ds;
}
catch
{
return null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
protected void GridView1_OnDataBound(object sender, GridViewRowEventArgs e)
{
if(String.IsNullOrEmpty(((Label)e.Row.FindControl("Label1")).Text))
e.Row.FindControl("Label1").Visible = false;
else
e.Row.FindControl("LinkButton").Visible = false;
}
{
if(String.IsNullOrEmpty(((Label)e.Row.FindControl("Label1")).Text))
e.Row.FindControl("Label1").Visible = false;
else
e.Row.FindControl("LinkButton").Visible = false;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
for (int i = 0; i < GridView1.Rows.Count; i++)
{
Label lb1 = (Label)GridView1.Rows[i].FindControl("Label1");
LinkButton lk1 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lk1.Visible =false;
}
else
{
lb1.Visible = false;
}
}
{
Label lb1 = (Label)GridView1.Rows[i].FindControl("Label1");
LinkButton lk1 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton1");
if (lb1.Text.Trim() == "")
{
lk1.Visible =false;
}
else
{
lb1.Visible = false;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询