ajax中使用Listview控件邦定DropdownList控件

Listview控件是VS2008中新增控件,相关的使用方法也有不少了,但是在AJAX中配合DropdownList控件的介绍却不多见。我在使用过程中出现下面的情况:
“Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。”的错误

如何解决这个问题,结合http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx的资料,希望能给刚开始使用的朋友一些帮助。


       < asp:UpdatePanel ID = " UpdatePanel1 "  runat = " server "  UpdateMode = " Conditional " >
            
< ContentTemplate >
                
< asp:ListView ID = " ListView1 "  runat = " server "  DataKeyNames = " UserID "  DataSourceID = " SqlDataSource1 "
                    InsertItemPosition
= " LastItem "  OnItemUpdating = " ListView1_ItemUpdating "  
                    oniteminserting
= " ListView1_ItemInserting " >
                    
< AlternatingItemTemplate >
                        
< tr style = " background-color: #FAFAD2; color: #284775; " >
                            
< td >
                                
< asp:Button ID = " DeleteButton "  runat = " server "  CommandName = " Delete "  Text = " 删除 "   />
                                
< asp:Button ID = " EditButton "  runat = " server "  CommandName = " Edit "  Text = " 编辑 "   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " UserIDLabel "  runat = " server "  Text = ' <%# Eval("UserID") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_NAMELabel "  runat = " server "  Text = ' <%# Eval("USER_NAME") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_PASSWORDLabel "  runat = " server "  Text = ' <%# Eval("USER_PASSWORD") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " RoleIdLabel "  runat = " server "  Text = ' <%# Eval("RoleId") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_DWBMLabel "  runat = " server "  Text = ' <%# Eval("USER_DWBM") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " DEPCODELabel "  runat = " server "  Text = ' <%# Eval("DEPCODE") %> '   />
                            
</ td >
                        
</ tr >
                    
</ AlternatingItemTemplate >
                    
< LayoutTemplate >
                        
< table runat = " server " >
                            
< tr runat = " server " >
                                
< td runat = " server " >
                                    
< table id = " itemPlaceholderContainer "  runat = " server "  border = " 1 "  style = " background-color: #FFFFFF;
                                        border - collapse: collapse; border - color: # 999999 ; border - style: none; border - width: 1px;
                                        font
- family: Verdana, Arial, Helvetica, sans - serif; " >
                                         < tr runat = " server "  style = " background-color: #FFFBD6; color: #333333; " >
                                            
< th runat = " server " >
                                            
</ th >
                                            
< th runat = " server " >
                                                用户ID
                                            
</ th >
                                            
< th runat = " server " >
                                                用户名
                                            
</ th >
                                            
< th runat = " server " >
                                                用户密码
                                            
</ th >
                                            
< th runat = " server " >
                                                所属角色
                                            
</ th >
                                            
< th runat = " server " >
                                                用户矿籍
                                            
</ th >
                                            
< th runat = " server " >
                                                用户单位
                                            
</ th >
                                        
</ tr >
                                        
< tr id = " itemPlaceholder "  runat = " server " >
                                        
</ tr >
                                    
</ table >
                                
</ td >
                            
</ tr >
                            
< tr runat = " server " >
                                
< td runat = " server "  style = " text-align: center; background-color: #FFCC66; font-family: Verdana, Arial, Helvetica, sans-serif;
                                    color: # 333333 ; " >
                                     < asp:DataPager ID = " DataPager1 "  runat = " server " >
                                        
< Fields >
                                            
< asp:NextPreviousPagerField ButtonType = " Button "  ShowFirstPageButton = " True "  ShowLastPageButton = " True "   />
                                        
</ Fields >
                                    
</ asp:DataPager >
                                
</ td >
                            
</ tr >
                        
</ table >
                    
</ LayoutTemplate >
                    
< InsertItemTemplate >
                        
< tr style = "" >
                            
< td >
                                
< asp:Button ID = " InsertButton "  runat = " server "  CommandName = " Insert "  Text = " 插入 "   />
                                
< asp:Button ID = " CancelButton "  runat = " server "  CommandName = " Cancel "  Text = " 清除 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " UserIDTextBox "  runat = " server "  Text = ' <%# Bind("UserID") %> '  Width = " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_NAMETextBox "  runat = " server "  Text = ' <%# Bind("USER_NAME") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_PASSWORDTextBox "  runat = " server "  Text = ' <%# Bind("USER_PASSWORD") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:DropDownList ID = " roleddl "  runat = " server "  DataSourceID = " sqlroleds "  DataValueField = " RoleId "
                                    DataTextField
= " RoleName "  OnDataBound = " roleddl_DataBound " >
                                
</ asp:DropDownList >
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_DWBMTextBox "  runat = " server "  Text = ' <%# Bind("USER_DWBM") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " DEPCODETextBox "  runat = " server "  Text = ' <%# Bind("DEPCODE") %> '  Width = " 70 "   />
                            
</ td >
                        
</ tr >
                    
</ InsertItemTemplate >
                    
< SelectedItemTemplate >
                        
< tr style = " background-color: #FFCC66; font-weight: bold; color: #000080; " >
                            
< td >
                                
< asp:Button ID = " DeleteButton "  runat = " server "  CommandName = " Delete "  Text = " 删除 "   />
                                
< asp:Button ID = " EditButton "  runat = " server "  CommandName = " Edit "  Text = " 编辑 "   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " UserIDLabel "  runat = " server "  Text = ' <%# Eval("UserID") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_NAMELabel "  runat = " server "  Text = ' <%# Eval("USER_NAME") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_PASSWORDLabel "  runat = " server "  Text = ' <%# Eval("USER_PASSWORD") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " RoleIdLabel "  runat = " server "  Text = ' <%# Eval("RoleId") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_DWBMLabel "  runat = " server "  Text = ' <%# Eval("USER_DWBM") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " DEPCODELabel "  runat = " server "  Text = ' <%# Eval("DEPCODE") %> '   />
                            
</ td >
                        
</ tr >
                    
</ SelectedItemTemplate >
                    
< EmptyDataTemplate >
                        
< table runat = " server "  style = " background-color: #FFFFFF; border-collapse: collapse;
                            border - color: # 999999 ; border - style: none; border - width: 1px; " >
                             < tr >
                                
< td >
                                    No data was returned.
                                
</ td >
                            
</ tr >
                        
</ table >
                    
</ EmptyDataTemplate >
                    
< EditItemTemplate >
                        
< tr style = " background-color: #FFCC66; color: #000080; " >
                            
< td >
                                
< asp:Button ID = " UpdateButton "  runat = " server "  CommandName = " Update "  Text = " 更新 "   />
                                
< asp:Button ID = " CancelButton "  runat = " server "  CommandName = " Cancel "  Text = " 取消 "   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " UserIDLabel1 "  runat = " server "  Text = ' <%# Eval("UserID") %> '   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_NAMETextBox "  runat = " server "  Text = ' <%# Bind("USER_NAME") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_PASSWORDTextBox "  runat = " server "  Text = ' <%# Bind("USER_PASSWORD") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
<%--在listview EdititemTemplate中定义一个ID为roleddl的DropDownList控件
OnDataBound事件邦定roleddl_DataBound事件
--%>                                < asp:DropDownList ID = " roleddl "  runat = " server "  DataSourceID = " sqlroleds "  DataValueField = " RoleId "
                                    DataTextField
= " RoleName "  OnDataBound = " roleddl_DataBound " >
                                
</ asp:DropDownList >
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " USER_DWBMTextBox "  runat = " server "  Text = ' <%# Bind("USER_DWBM") %> '
                                    Width
= " 70 "   />
                            
</ td >
                            
< td >
                                
< asp:TextBox ID = " DEPCODETextBox "  runat = " server "  Text = ' <%# Bind("DEPCODE") %> '  Width = " 70 "   />
                            
</ td >
                        
</ tr >
                    
</ EditItemTemplate >
                    
< ItemTemplate >
                        
< tr style = " background-color: #FFFBD6; color: #333333; " >
                            
< td >
                                
< asp:Button ID = " DeleteButton "  runat = " server "  CommandName = " Delete "  Text = " 删除 "   />
                                
< asp:Button ID = " EditButton "  runat = " server "  CommandName = " Edit "  Text = " 编辑 "   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " UserIDLabel "  runat = " server "  Text = ' <%# Eval("UserID") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_NAMELabel "  runat = " server "  Text = ' <%# Eval("USER_NAME") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_PASSWORDLabel "  runat = " server "  Text = ' <%# Eval("USER_PASSWORD") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " RoleIdLabel "  runat = " server "  Text = ' <%# Eval("RoleId") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " USER_DWBMLabel "  runat = " server "  Text = ' <%# Eval("USER_DWBM") %> '   />
                            
</ td >
                            
< td >
                                
< asp:Label ID = " DEPCODELabel "  runat = " server "  Text = ' <%# Eval("DEPCODE") %> '   />
                            
</ td >
                        
</ tr >
                    
</ ItemTemplate >
                
</ asp:ListView >
            
</ ContentTemplate >
        
</ asp:UpdatePanel >



后台代码
     protected   void  roleddl_DataBound( object  sender, EventArgs e)
    
{
        DropDownList ddl 
= (DropDownList)sender;
        
//add an empty item on top of the list
        AddEmptyItem(ddl);
        ListViewDataItem listI 
= null;

         //注意ListViewItemType.InsertItem,ddl.NamingContainer是ListViewItem对象,而不是ListVIew对象
        
if (((ListViewItem)ddl.NamingContainer).ItemType == ListViewItemType.DataItem)
        
{
            listI 
= (ListViewDataItem)ddl.NamingContainer;
        }

        
//  frmV.
        if (listI != null)
        
{
            
//Let's pull the province value from the databound item.  The data
            
//in my application is supplied by BLL as a dataview.  Therefore each
            
//item bound to the FormView is of type DataRowView.  So let's cast 
            
//that DataItem to the appropriate type to be able to use it
             //记得要把DataItem改成DataRowView类型
            string roleid = ((DataRowView)listI.DataItem).Row["roleid"].ToString();
            
            ddl.ClearSelection();
            
//be careful of the possibility that the value saved on the
            
//database does not exist in the valid selections that are displayed
            
//on the list
            ListItem li = ddl.Items.FindByValue(roleid);
            
if (li != null) li.Selected = true;
        }

        
//since the city selection is dependent on the province, we
        
//have to databind the city list after we changed the selection for the province
        
//ddl = (DropDownList)frmV.FindControl("ddlCity");
        
//if (ddl != null) ddl.DataBind();

    }

    
void  AddEmptyItem(DropDownList ddl)
    
{
        ListItem li 
= new ListItem("请选择""");
        ddl.Items.Insert(
0, li);

    }



    
protected   void  ListView1_ItemUpdating( object  sender, ListViewUpdateEventArgs e)
    
{
        
if (Page.IsValid)
        
{
             //ListView直接找不到DropDownList的
            
string roleid = ((DropDownList)((ListView)sender).Items[e.ItemIndex].FindControl("roleddl")).SelectedValue;

            e.NewValues[
"roleid"= roleid;
            e.Cancel 
= false;

        }

        
else
            e.Cancel 
= true;
    }


    
protected   void  ListView1_ItemInserting( object  sender, ListViewInsertEventArgs e)
    
{
        
string roleid = ((DropDownList)e.Item.FindControl("roleddl")).SelectedValue;

        
//如果没有选择正确角色,则取消更新
        if (roleid == "请选择")
            e.Cancel 
= true;
        
else
        
{
            e.Values[
"roleid"= roleid;
            e.Cancel 
= false;
        }

    }

本人文笔不好,请朋友们多多包函。祝好运!

转载于:https://www.cnblogs.com/redw/archive/2008/01/10/1033985.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值