在页面加载时,我想为GridView的按钮背景颜色着色 [英] On page load I want to color GridView's button backcolor

查看:46
本文介绍了在页面加载时,我想为GridView的按钮背景颜色着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在数据库中为gridview按钮设置了活动和非活动状态。当我在页面加载时加载gridview我想根据数据库值着色按钮。

I have set active and deactive status in database for gridview button. when I load gridview on page load i want to color button according to database value.

推荐答案

foreach (GridViewRow row in GridView1.Rows)
        {
            Button BTN = (Button)row.FindControl("XXXXX");
            BTN.BackColor = System.Drawing.Color.Bisque;
        }


您好,



尝试如下。



在gridview中添加一个隐藏标签lblStatus。

Hi,

try as below.

Add, one hidden label "lblStatus" in gridview.
<templatecolumn visible="false">
<itemtemplate>
<asp:label id="lblStatus" runat="server" text="<%#Eval("Status") %>" xmlns:asp="#unknown"></asp:label>
</itemtemplate>
</templatecolumn>




//In Row data bound event, add below lines.

if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btn");
Label lbl = (Label)e.Row.FindControl("lblStatus");
if(Convert.ToBoolean(lbl.Text))
{
  btn.BackColor = System.Drawing.Color.Gray;
}
else
{
  btn.BackColor = System.Drawing.Color.Green;
}
}





标签文本将由数据库中的数据源绑定。根据该状态值,可以更改按钮颜色。



请注意,状态的文本将为True或False,否则您可以与您选择的数据。



希望它有所帮助。



The label text will be binded by datasource from database. based on that status value, the button color can be changed.

Note that, the text for status would be "True" or "False" otherwise you can compare with data of your choice.

hope it helps.


这篇关于在页面加载时,我想为GridView的按钮背景颜色着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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