datalist绑定用法

<ASP:DataList id="MyDataList" RepeatColumns="2" runat="server">

<ItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td width=1 bgcolor="BD8672"/>

<td valign="top">
<img align="top" src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' >
</td>

<td valign="top">

<b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>
<b>Category: </b><%# DataBinder.Eval(Container.DataItem, "type") %><br>
<b>Publisher ID: </b><%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>

<p>

<a href='<%# DataBinder.Eval(Container.DataItem, "title_id", "purchase.aspx?titleid={0}") %>' >
<img border="0" src="/quickstart/aspplus/images/purchase_book.gif" >
</a>

</td>
</tr>
</table>

</ItemTemplate>

</ASP:DataList>
 
 
加入时间控制
Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

SqlConnection myConnection;

void Page_Load(Object sender, EventArgs e) {

myConnection
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PubsString"]);

if (!Page.IsPostBack) {
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where type = 'business'", myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"Titles");

MyDataList.DataSource
=ds.Tables["Titles"].DefaultView;
MyDataList.DataBind();
}

}

void MyDataList_Select(Object sender, EventArgs e) {

String title
= MyDataList.DataKeys[MyDataList.SelectedItem.ItemIndex].ToString();
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where title_id = '" + title + "'" , myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"TitleDetails");

DataRowView rowview
= ds.Tables["TitleDetails"].DefaultView[0];

DetailsImage.Src
= "/quickstart/aspplus/images/title-" + rowview["title_id"] + ".gif";
DetailsPubId.InnerHtml
= "<b>Publisher ID: </b>" + rowview["pub_id"].ToString() + "<br>";
DetailsTitleId.InnerHtml
= "<b>Title ID: </b>" + rowview["title_id"].ToString() + "<br>";
DetailsType.InnerHtml
= "<b>Category: </b>" + rowview["type"].ToString() + "<br>";
DetailsPrice.InnerHtml
= "<b>Price: </b> $ " + rowview["price"].ToString() + "<p>";
PurchaseLink.InnerHtml
= "<img border='0' src='/quickstart/aspplus/images/purchase_book.gif' >";
PurchaseLink.HRef
="purchase.aspx?titleid=" + rowview["title_id"].ToString();
DetailsTitle.InnerHtml
= rowview["title"].ToString();

DetailsImage.Visible
= true;
}

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

<form runat="server">

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/header.inc" -->

<table width="100%">
<tr>
<td width="50%">

<ASP:DataList id="MyDataList" OnSelectedIndexChanged="MyDataList_Select" DataKeyField="title_id" runat="server">

<ItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td valign="top">
<img align="top" width="25" border=1 src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' runat="server"/>
</td>
<td valign="top">
<b>Title: </b>
<asp:linkbutton Text='<%# DataBinder.Eval(Container.DataItem, "title") %>' CommandName="Select" style="color:darkred" runat="server"/>
<br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %><br>
</td>
</tr>
</table>

</ItemTemplate>

</ASP:DataList>

</td>

<td valign="top" style="padding-top:15" width="50%">
<table cellpadding="5" width="100%" style="font: 10pt verdana">
<tr>
<td>
<img id="DetailsImage" visible="false" runat="server">
</td>
<td valign="top" width="400">
<div style="font: 12pt verdana;color:darkred">
<i><b><span id="DetailsTitle" runat="server"/></i></b><br>
</div>
<span id="DetailsTitleId" runat="server"/>
<span id="DetailsPubId" runat="server"/>
<span id="DetailsType" runat="server"/>
<span id="DetailsPrice" runat="server"/>
<a id="PurchaseLink" runat="server"/>
</td>
</tr>
</table>
</td>

</tr>
</table>

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/footer.inc" -->

</form>

</body>
</html>

如何自定义命令
自定义处理命令
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

SqlConnection myConnection;

void Page_Load(Object sender, EventArgs e) {

myConnection
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PubsString"]);

if (!Page.IsPostBack) {
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where type = 'business'", myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"Titles");

MyDataList.DataSource
=ds.Tables["Titles"].DefaultView;
MyDataList.DataBind();
}

}

void MyDataList_Select(Object sender, EventArgs e) {

String title
= MyDataList.DataKeys[MyDataList.SelectedItem.ItemIndex].ToString();
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where title_id = '" + title + "'" , myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"TitleDetails");

DataRowView rowview
= ds.Tables["TitleDetails"].DefaultView[0];

DetailsImage.Src
= "/quickstart/aspplus/images/title-" + rowview["title_id"] + ".gif";
DetailsPubId.Text
= "<b>Publisher ID: </b>" + rowview["pub_id"].ToString() + "<br>";
DetailsTitleId.Text
= "<b>Title ID: </b>" + rowview["title_id"].ToString() + "<br>";
DetailsType.Text
= "<b>Category: </b>" + rowview["type"].ToString() + "<br>";
DetailsPrice.Text
= "<b>Price: </b> $ " + rowview["price"].ToString() + "<p>";
PurchaseLink.Text
= "<img border='0' src='/quickstart/aspplus/images/purchase_book.gif' >";
PurchaseLink.NavigateUrl
="purchase.aspx?titleid=" + rowview["title_id"].ToString();
DetailsTitle.Text
= rowview["title"].ToString();

DetailsImage.Visible
= true;
}

void MyDataList_ItemCommand(Object sender, DataListCommandEventArgs E) {

String title
= MyDataList.DataKeys[E.Item.ItemIndex].ToString();
String command
= E.CommandName;

switch(command) {

case "Discuss" :
ShowDiscussions(title);
break;

case "Ratings" :
ShowRatings(title);
break;
}
}

void ShowRatings(String title) {

Message.InnerHtml
= "<h5>Ratings for \"" + title + "\"</h5>";
Message.InnerHtml
+= "Print Ratings here";
}

void ShowDiscussions(String title) {

Message.InnerHtml
= "<h5>Discussions for \"" + title + "\"</h5>";
Message.InnerHtml
+= "Print Discussions here";
}

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

<form runat="server">

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/header.inc" -->

<table width="100%">
<tr>
<td width="50%">

<ASP:DataList id="MyDataList" OnSelectedIndexChanged="MyDataList_Select" OnItemCommand="MyDataList_ItemCommand" DataKeyField="title_id" runat="server">

<ItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td valign="top">
<img align="top" width="25" border=1 src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' runat="server"/>
</td>
<td valign="top">
<b>Title: </b>
<asp:linkbutton Text='<%# DataBinder.Eval(Container.DataItem, "title") %>' CommandName="Select" style="color:darkred" runat="server"/>
<br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
<br>
<asp:linkbutton Text="Discussions" CommandName="Discuss" style="color:darkred;font:8pt tahoma" runat="server"/>
|
<asp:linkbutton Text="Ratings" CommandName="Ratings" style="color:darkred;font:8pt tahoma" runat="server"/>
</td>
</tr>
</table>

</ItemTemplate>

</ASP:DataList>

</td>

<td valign="top" style="padding-top:15" width="50%">
<table cellpadding="5" width="100%" style="font: 10pt verdana">
<tr>
<td>
<img id="DetailsImage" visible="false" runat="server">
</td>
<td valign="top" width="400">
<div style="font: 12pt verdana;color:darkred">
<i><b><asp:Label id="DetailsTitle" runat="server"/></i></b><br>
</div>
<asp:Label id="DetailsTitleId" runat="server"/>
<asp:Label id="DetailsPubId" runat="server"/>
<asp:Label id="DetailsType" runat="server"/>
<asp:Label id="DetailsPrice" runat="server"/>
<asp:HyperLink id="PurchaseLink" runat="server"/>
</td>
</tr>
</table>
</td>

</tr>
</table>

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/footer.inc" -->

<div id="Message" style="font: 10pt verdana;padding:0,15,15,15" runat="server"/>

</form>

</body>
</html>

修改

 

 

Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

void PopulateList() {

SqlConnection myConnection
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PubsString"]);
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where type='business'", myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"Titles");

MyDataList.DataSource
= ds.Tables["Titles"].DefaultView;
MyDataList.DataBind();
}

void Page_Load(Object sender, EventArgs e) {

if (!Page.IsPostBack) {
PopulateList();
}
}

void MyDataList_Edit(Object sender, DataListCommandEventArgs e) {

MyDataList.EditItemIndex
= (int)e.Item.ItemIndex;
PopulateList();
}

void MyDataList_Update(Object sender, DataListCommandEventArgs e) {

// database update left out for simplicity's sake

String price
= ((HtmlInputText)e.Item.FindControl("edit_price")).Value;
Message.InnerHtml
= "Price Updated: " + price;
MyDataList.EditItemIndex
= -1;
PopulateList();
}

void MyDataList_Cancel(Object sender, DataListCommandEventArgs e) {

MyDataList.EditItemIndex
= -1;
PopulateList();
}

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

<form runat="server">

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/header.inc" -->

<ASP:DataList id="MyDataList" RepeatColumns="2" OnEditCommand="MyDataList_Edit" OnUpdateCommand="MyDataList_Update" OnCancelCommand="MyDataList_Cancel" runat="server">

<ItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td width=1 bgcolor="BD8672"/>
<td valign="top">
<img align="top" src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' >
</td>
<td valign="top">
<b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>
<b>Category: </b><%# DataBinder.Eval(Container.DataItem, "type") %><br>
<b>Publisher ID: </b><%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
<p>
<asp:linkbutton CommandName="Edit" runat="server">
<img border="0" src="/quickstart/aspplus/images/edit_book.gif" >
</asp:linkbutton>
</td>
</tr>
</table>

</ItemTemplate>

<EditItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td width=1 bgcolor="BD8672"/>
<td valign="top">
<img align="top" src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' >
</td>
<td valign="top">
<b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>
<b>Category: </b><%# DataBinder.Eval(Container.DataItem, "type") %><br>
<b>Publisher ID: </b><%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b><input id="edit_price" type="text" value='<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>' runat="server"/>
<p>
<asp:linkbutton CommandName="Update" runat="server"><img border="0" src="/quickstart/aspplus/images/update_book.gif" ></asp:linkbutton>
<asp:linkbutton CommandName="Cancel" runat="server"><img border="0" src="/quickstart/aspplus/images/cancel_book.gif" ></asp:linkbutton>
</td>
</tr>
</table>

</EditItemTemplate>

</ASP:DataList>

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/footer.inc" -->

</form>

<div style="font: 10pt verdana;padding:0,15,15,15" id="Message" runat="server"/>

</body>
</html>

 

 找到item中的控件名称并处理它

 

Code
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<script language="C#" runat="server">

void Page_Load(Object Src, EventArgs E ) {

if (!Page.IsPostBack) {

SqlConnection myConnection
= new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["PubsString"]);
SqlDataAdapter myCommand
= new SqlDataAdapter("select * from Titles where type='business'", myConnection);

DataSet ds
= new DataSet();
myCommand.Fill(ds,
"Titles");

MyDataList.DataSource
= ds.Tables["Titles"].DefaultView;
MyDataList.DataBind();
}
}

void Submit_Click(Object Src, EventArgs E ) {

for (int i=0; i<MyDataList.Items.Count; i++) {

String isChecked
= ((CheckBox) MyDataList.Items[i].FindControl("Save")).Checked.ToString();
Message.InnerHtml
+= "Item(" + i + "): " + isChecked + "<br>";
}
}

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

<form runat="server">

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/header.inc" -->

<ASP:DataList id="MyDataList" RepeatColumns="2" runat="server">

<ItemTemplate>

<table cellpadding=10 style="font: 10pt verdana">
<tr>
<td width=1 bgcolor="BD8672"/>
<td valign="top">
<img align="top" src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>' >
</td>
<td valign="top">
<b>Title: </b><%# DataBinder.Eval(Container.DataItem, "title") %><br>
<b>Category: </b><%# DataBinder.Eval(Container.DataItem, "type") %><br>
<b>Publisher ID: </b><%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
<b>Price: </b><%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>
<p>
<asp:CheckBox id="Save" runat="server"/> <b>Save to Favorites</b>
</td>
</tr>
</table>

</ItemTemplate>

</ASP:DataList>

<p>

<div style="padding:0,15,0,15">
<input type="submit" Value="Update Favorites" OnServerClick="Submit_Click" runat="server"/>
</div>

<p>

<!-- #include virtual="/quickstart/aspplus/samples/webforms/customize/footer.inc" -->

</form>

<div style="font: 10pt verdana" EnableViewState="false" id="Message" runat="server"/>

</body>
</html>

转载于:https://www.cnblogs.com/qhnokia/archive/2008/10/29/1322487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值