首页 新闻 会员 周边

DataList模板控件问题

0
悬赏园豆:80 [已解决问题] 解决于 2009-07-27 17:52

RepeaterDemo.ascx

<%@ 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


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上
但重新绑定的话dropdownlist会恢复到默认绑定状态,之前用户选择的项就没保存下来

0
0
(请您对文章做出评价)
问题补充: 不好意思 我可能没有表述清楚 当前实现的是用户选择了Dropdownlist中的某项后Label显示对应选择项的描述信息,所以回发是必要的,不知道这是不是你说的"确认"操作
Bob&xiaobo.liu的主页 Bob&xiaobo.liu | 初学一级 | 园豆:3
提问于:2009-07-27 15:34
< >
分享
最佳答案
0

如果用户只是选择了下拉列表,没有“确认”操作,不向服务器发出任何形式的请求,那么自然不会保存下拉列表所选择的内容。我觉得这不应该属于一个功能性的问题,而应该是一个用户操作的问题。

如果非要保存的话,在添加数据源的新记录之前,可以将DataList中的所有数据导出到一个List中,在List中记录用户的修改。

麒麟.NET | 老鸟四级 |园豆:3614 | 2009-07-27 16:23
其他回答(1)
0

为何不尝试ajax

davidsdad | 园豆:235 (菜鸟二级) | 2009-07-27 16:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册