如何判断一个字段中是否存在某字?。 如何获取GridView中的控件?
我想判断在数据库中某个字段是否存在某字!例 在a字段中 是否存在b
还有一个问题 我以前做过 datalist 的获取控件 改更他的属性 现在用GridView不会!
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="Label1" runat="server" BackColor="Red" Text=""></asp:Label> </ItemTemplate>
</asp:TemplateField>
例如 以上 我怎么获取 Label21 改更他的属性?
在线等!
--------------------编程问答--------------------
codeselect * from table where field='char'
遍历GridView里的所有Row,然后遍历Row里面所有的控件。
使用FindControl方法获取Label1 --------------------编程问答-------------------- 我想判断在数据库中某个字段是否存在某字!
例 在a字段中 是否存在b
select * from 表名 where a like '%b%' --------------------编程问答-------------------- 在Gridview 的RowDataBound事件中添加判断,如下代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//获取绑定到该行的记录视图:DataRowView
DataRowView drv = (DataRowView)e.Row.DataItem;
//在该行中查找控件ID为"Label1"的标签并将字段a的值赋值给标签的Text属性
Label lbl1 = (Label)e.Row.FindControl("Label1");
lbl1.Text = drv["a"].ToString();
if (drv["a"].ToString().Contains("b"))
{
//a字段里包含b字窜
lbl1.ForeColor = System.Drawing.Color.Red;
}
else lbl1.ForeColor = System.Drawing.Color.Black;
}
}
--------------------编程问答-------------------- 来晚了,楼上说的都可行,UP!
select * from 表名 where a like '%b%' --------------------编程问答-------------------- 1
select * from 表名 where a like '%b%'
2
--------------------编程问答--------------------
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl1 = e.Row.FindControl("Label1") as Label ;
....//可以做你想要的操作了
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设备状态
Label lb =(Label) e.Row.Cells[9].FindControl("lbsbzt");
}
取Label的
--------------------编程问答-------------------- 用筛选器 --------------------编程问答-------------------- a.Contains(b); --------------------编程问答-------------------- 1
select * from 表名 where a like '%b%'
2
C# code
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl1 = e.Row.FindControl("Label1") as Label ;
....//可以做你想要的操作了
}
}
--------------------编程问答-------------------- 1.
select * from 表名 where a like '%b%'
2.
--------------------编程问答-------------------- 在事件当中查找可用楼上的方法,也可直接查找.
在Gridview 的RowDataBound事件中添加判断,如下代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//获取绑定到该行的记录视图:DataRowView
DataRowView drv = (DataRowView)e.Row.DataItem;
//在该行中查找控件ID为"Label1"的标签并将字段a的值赋值给标签的Text属性
Label lbl1 = (Label)e.Row.FindControl("Label1");
}
}
this.GridControl.Rows[index].Cells[index].FindControl("controlID")
也是可以的
补充:.NET技术 , ASP.NET