如果在 C# 中有条件,则在 gridview 中设置文本框控件 ASP.NET
Set textbox control in gridview if condition in C# ASP.NET
在我的 ASP.NET gridview 中,我默认插入标签 lbannotation
:
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
如果数据库table列的字符串tannotation
:
Eval("tannotation").ToString()
在我们的字符串中包含单词 ready
我需要更改 asp:Label
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
到asp:TextBox
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:TextBox ID="txannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
这个需要设置什么?
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
????
}
}
}
帮我做一下
您可以使用 Visible
属性 内嵌执行此操作。使用 Contains
检查“就绪”。它 returns 一个布尔值,因此您可以将其用于可见性。
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbannotation" Visible='<%# Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:Label>
<asp:TextBox ID="txannotation" Visible='<%# !Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
或者您可以使用方法进行更一般的使用
public bool IsReady(string keyword, string value)
{
return value.Contains(keyword);
}
aspx
Visible='<%# IsReady("ready", Eval("tannotation").ToString()) %>'
尝试添加两个控件并仅在字段“tannotation”时显示标签!=“就绪”
代码应该类似于:
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
var lbannotation = (Lable)e.Row.FindControl("lbannotation");
var txannotation = (TextBox)e.Row.FindControl("txannotation");
if(e.Row.DataItem["tannotation"].ToString() == "ready")
{
lbannotation.Visible = false;
txannotation.Visible = true;
}else{
txannotation.Visible = false;
lbannotation.Visible = true;
}
}
}
}
在我的 ASP.NET gridview 中,我默认插入标签 lbannotation
:
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
如果数据库table列的字符串tannotation
:
Eval("tannotation").ToString()
在我们的字符串中包含单词 ready
我需要更改 asp:Label
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
到asp:TextBox
<asp:TemplateField HeaderText="annotation"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:TextBox ID="txannotation" runat="server"
Text='<%# Eval("tannotation").ToString() %>'
CssClass="ddl_Class_new"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
这个需要设置什么?
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
????
}
}
}
帮我做一下
您可以使用 Visible
属性 内嵌执行此操作。使用 Contains
检查“就绪”。它 returns 一个布尔值,因此您可以将其用于可见性。
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbannotation" Visible='<%# Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:Label>
<asp:TextBox ID="txannotation" Visible='<%# !Eval("tannotation").ToString().Contains("ready") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
或者您可以使用方法进行更一般的使用
public bool IsReady(string keyword, string value)
{
return value.Contains(keyword);
}
aspx
Visible='<%# IsReady("ready", Eval("tannotation").ToString()) %>'
尝试添加两个控件并仅在字段“tannotation”时显示标签!=“就绪”
代码应该类似于:
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
var lbannotation = (Lable)e.Row.FindControl("lbannotation");
var txannotation = (TextBox)e.Row.FindControl("txannotation");
if(e.Row.DataItem["tannotation"].ToString() == "ready")
{
lbannotation.Visible = false;
txannotation.Visible = true;
}else{
txannotation.Visible = false;
lbannotation.Visible = true;
}
}
}
}