RepeaterDemo.ascx
但重新绑定的话dropdownlist会恢复到默认绑定状态,之前用户选择的项就没保存下来
Code
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.ascx.cs" Inherits="ApplicationDemos.RepeaterDemo" %>
<div>
<asp:DataList ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand"
onitemcreated="Repeater1_ItemCreated" onprerender="Repeater1_PreRender">
<ItemTemplate>
<table>
<tr>
<td>
<table width="100%">
<tr>
<td width="210">Condition </td>
<td><span style="margin-right:1px">:</span>
<asp:DropDownList ID="DdlCondofLicen" runat="server" Height="17px"
Width="160px" AutoPostBack="True" OnSelectedIndexChanged="DdlCondofLicen_SelectedIndexChanged">
</asp:DropDownList>
<asp:LinkButton ID="LbtnRemove" runat="server" CommandName="Remove">Remove</asp:LinkButton>
</td>
</tr>
<tr>
<td>Description </td>
<td>:
<asp:Label ID="LbDescription" runat="server"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" />
RepeaterDemo.ascx.cs<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.ascx.cs" Inherits="ApplicationDemos.RepeaterDemo" %>
<div>
<asp:DataList ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand"
onitemcreated="Repeater1_ItemCreated" onprerender="Repeater1_PreRender">
<ItemTemplate>
<table>
<tr>
<td>
<table width="100%">
<tr>
<td width="210">Condition </td>
<td><span style="margin-right:1px">:</span>
<asp:DropDownList ID="DdlCondofLicen" runat="server" Height="17px"
Width="160px" AutoPostBack="True" OnSelectedIndexChanged="DdlCondofLicen_SelectedIndexChanged">
</asp:DropDownList>
<asp:LinkButton ID="LbtnRemove" runat="server" CommandName="Remove">Remove</asp:LinkButton>
</td>
</tr>
<tr>
<td>Description </td>
<td>:
<asp:Label ID="LbDescription" runat="server"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
<asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" />
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace ApplicationDemos
{
public partial class RepeaterDemo : System.Web.UI.UserControl
{
ArrayList items = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindRp();
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Repeater1.Items.Count + 1; i++)
{
string li = i.ToString();
items.Add(li);
}
Items = items;
BindRp();
}
private void BindRp()
{
Repeater1.DataSource = (ArrayList)Items;
Repeater1.DataBind();
}
protected void Repeater1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
Items.RemoveAt(e.Item.ItemIndex);
}
BindRp();
}
public ArrayList Items
{
get
{
return (ArrayList)ViewState["items"];
}
set
{
ViewState["items"] = items;
}
}
private ArrayList CreateAL()
{
string temp1 = "temp1";
string temp2 = "temp2";
string temp3 = "temp3";
ArrayList al = new ArrayList();
al.Add(temp1);
al.Add(temp2);
al.Add(temp3);
return al;
}
protected void Repeater1_ItemCreated(object sender, DataListItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DdlCondofLicen");
ddl.DataSource = CreateAL();
ddl.DataBind();
}
protected void DdlCondofLicen_SelectedIndexChanged(object sender,EventArgs e)
{
((Label)((DropDownList)sender).NamingContainer.FindControl("LbDescription")).Text = ((DropDownList)sender).SelectedItem.Text.Trim();
}
protected void Repeater1_PreRender(object sender, EventArgs e)
{
}
}
}
这个是一个DEMO,用于动态的添加和删除模板项,目前的问题是:动态添加模板项实例的处理方式是添加数据源中的一条记录后重新绑定到DataList上using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace ApplicationDemos
{
public partial class RepeaterDemo : System.Web.UI.UserControl
{
ArrayList items = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindRp();
}
protected void BtnAdd_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Repeater1.Items.Count + 1; i++)
{
string li = i.ToString();
items.Add(li);
}
Items = items;
BindRp();
}
private void BindRp()
{
Repeater1.DataSource = (ArrayList)Items;
Repeater1.DataBind();
}
protected void Repeater1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Remove")
{
Items.RemoveAt(e.Item.ItemIndex);
}
BindRp();
}
public ArrayList Items
{
get
{
return (ArrayList)ViewState["items"];
}
set
{
ViewState["items"] = items;
}
}
private ArrayList CreateAL()
{
string temp1 = "temp1";
string temp2 = "temp2";
string temp3 = "temp3";
ArrayList al = new ArrayList();
al.Add(temp1);
al.Add(temp2);
al.Add(temp3);
return al;
}
protected void Repeater1_ItemCreated(object sender, DataListItemEventArgs e)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("DdlCondofLicen");
ddl.DataSource = CreateAL();
ddl.DataBind();
}
protected void DdlCondofLicen_SelectedIndexChanged(object sender,EventArgs e)
{
((Label)((DropDownList)sender).NamingContainer.FindControl("LbDescription")).Text = ((DropDownList)sender).SelectedItem.Text.Trim();
}
protected void Repeater1_PreRender(object sender, EventArgs e)
{
}
}
}
但重新绑定的话dropdownlist会恢复到默认绑定状态,之前用户选择的项就没保存下来