海外主机测评

您现在的位置是:首页 > 数据库 > 正文

数据库

itemtemplateitemtemplate用法

cds8202023-04-26数据库64
本文目录一览:1、ASP.NET的ItemTemplate用法2、asp获取ItemTemplate的值3、datalist中的itemtemplate重复排列,如何编写它的边框

本文目录一览:

  • 1、ASP.NET的ItemTemplate用法
  • 2、asp获取ItemTemplate的值
  • 3、datalist 中的itemtemplate重复排列,如何编写它的边框
  • 4、gridview编辑模板中没有itemtemplate
  • 5、wpf中 listView 更加字段值不同 用不同的ItemTemplate

ASP.NET的ItemTemplate用法

在用DataList控件的时候肯定的要用到itemtemplate,现在我就跟大家怎么用它

第一步:先把DataList控件绑定数据库中的某个表,代码如下:

SqlConnectiON con = new SqlConnection("server=.; uid=sa; pwd=密码; database=数据库");

SqlDataAdapter sd = new SqlDataAdapter("select ", con);

DataSet ds = new DataSet();

sd.Fill(ds, "LanMuName");

DataList1.DataSource = ds;

DataList1.DataBind();

第二步;再用itemtemplate绑定表里的某列,代码如下:

asp:DataList ID="DataList1" runat="server" CellPadding="0"

ItemTemplate

asp:Label ID= "CaptionLabel " runat= "server "

Text= '%# DataBinder.eval_r(Container.DataItem,"shipname")%'

/ItemTemplate

/asp:DataList

通过上面两步就可以实现绑定了,现在我为大家解释一下

DataBinder.eval_r(Container.DataItem,"shipname")里各项是什么意思:

DataBinder:数据绑定管理器

Eval:求值

Container:被绑定到的容器,比如GridView,DataList等

DataItem:容器的数据项,包括项、交替模板行

shipname:绑定到容器的字段(来自数据库表字段即表中的列)

asp获取ItemTemplate的值

假定Gridview的ID为“Gridview1”,那么获取第3行的label的数据方法如下:

Label Label1 = Gridview1.Rows[2].FindControl("Label1") as Label; string a = Label1.Text;

所有的控件ID都是一样的,FindContral("Label1")中的“Label1”是控件的ID,而Label Label1只是我声明的一个变量,可以换成其它的。

datalist 中的itemtemplate重复排列,如何编写它的边框

楼主的Table

和 td

都设置了边框样式,所以才会重叠。

只设置 Table的样式就OK拉

table style="border:1px solid #ccc"

gridview编辑模板中没有itemtemplate

先在”编辑列“选项中选择要使用的列(字段),然后在右侧选择“将此字段转换为TemplateField"。然后再打开“编辑模板”时就会看到“Itemtemplate”模板了。

wpf中 listView 更加字段值不同 用不同的ItemTemplate

listview后台绑定数据集合,根据集合中某字段不同使用不同ItemTemplate

根据楼主的问题我根据不同情况给出不同方案:(方便起见,我拿listbox做例子)

1. 集合中某字段不同是指集合中每个实体对象的这一字段统一都发生改变:

我的思路是首先创建一个集合类,listbox的itemtemplate绑定集合值通过转换器拿到想要的DataTemplate

页面的资源:

Window.Resources

converter:TempConverter x:Key="myTempConverter"/

DataTemplate x:Key="temp1"

StackPanel Orientation="Horizontal"

TextBlock Text="temp1" Margin="0 0 10 0"/

TextBlock Text="{Binding Name}"/

/StackPanel

/DataTemplate

DataTemplate x:Key="temp2"

StackPanel Orientation="Horizontal"

TextBlock Text="temp2" Margin="0 0 10 0"/

TextBlock Text="{Binding Name}"/

/StackPanel

/DataTemplate

/Window.Resources

前台:

ListBox ItemsSource="{Binding pList}" ItemTemplate="{Binding pList,Converter={StaticResource myTempConverter}}" Height="200" x:Name="listBox1" Width="120" /

后台:

public class People

{

public bool IsTemp1 { get; set; }

public string Name { get; set; }

}

public class PeopleList

{

public ObservableCollectionPeople pList { get; set; }

}

转换器类:

public class TempConverter:IValueConverter

{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

ListPeople datalist = (ListPeople)value;

Window1 window =new Window1();

DataTemplate template = new DataTemplate();

if (datalist[0].IsTemp1 == true)

{

template = window.Resources["temp1"] as DataTemplate;

}

else

{

template = window.Resources["temp2"] as DataTemplate;

}

return template; }

。。。

第二种情况:集合中某字段不同是指集合中每个实体对象的这一字段不同时发生改变:

那我建议只建立一个datatemplate。里面的内容可以放两套:

DataTemplate x:Key="temp"

Grid

StackPanel Orientation="Horizontal"

TextBlock Text="temp1" Margin="0 0 10 0"/

TextBlock Text="{Binding Name}"/

/StackPanel

StackPanel Orientation="Horizontal"

TextBlock Text="temp2" Margin="0 0 10 0"/

TextBlock Text="{Binding Name}"/

/StackPanel

/Grid

/DataTemplate

然后每套的显示绑定该属性,通过转换器转换成可见性即可。

StackPanel Orientation="Horizontal" Visibility="{Binding 属性,converter ={...}"

关于itemtemplate和itemtemplate用法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~