Repeater绑定DropDownList

本文介绍如何使用ASP.NET中的Repeater控件实现数据的显示与编辑功能,特别是针对每个单元格进行独立编辑的过程。通过示例代码展示了如何配置Repeater控件的数据源、事件处理以及自定义模板等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifCode
<%@ Page Theme="Default" Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="EditIndividualCell.aspx.cs" Inherits="Ajax学习.EditIndividualCell" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cph" runat="server">    
    
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
        onitemdatabound
="Repeater1_ItemDataBound" 
        ondatabinding
="Repeater1_DataBinding" onitemcreated="Repeater1_ItemCreated">
    
<ItemTemplate>
        
<asp:TextBox ID="TextBox1" runat="server" Text = '<% # Eval("TM") %>' TextMode="MultiLine">'></asp:TextBox><asp:TextBox ID="TextBox2" runat="server" Text = '<% # Eval("YHM") %>' TextMode="MultiLine">'></asp:TextBox><br />
        
<asp:DropDownList ID = "ddl" runat="server" DataTextField="YHM" DataValueField="YHM" ></asp:DropDownList><br />
    
</ItemTemplate>
    
</asp:Repeater>
    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString
="<%$ ConnectionStrings:QingShiConnectionString %>" 
        DeleteCommand
="DELETE FROM ML WHERE (SXH = @sxh)" 
        InsertCommand
="INSERT INTO ML(SXH, TM, XGRQ, IS_SELECTED, YHM) VALUES (@sxh, @tm, @xgrq, @IS_SELECTED)" 
        SelectCommand
="SELECT TOP (10) SXH, TM, XGRQ, IS_SELECTED, YHM FROM ML" 
        UpdateCommand
="UPDATE ML SET TM = @tm, XGRQ = @xgrq, IS_SELECTED = @IS_SELECTED, YHM = @YHM WHERE (SXH = @sxh)">
        
<DeleteParameters>
            
<asp:Parameter Name="sxh" />
        
</DeleteParameters>
        
<UpdateParameters>
            
<asp:Parameter Name="tm" />
            
<asp:Parameter Name="xgrq" />
            
<asp:Parameter Name="IS_SELECTED" />
            
<asp:Parameter Name="YHM" />
            
<asp:Parameter Name="sxh" />
        
</UpdateParameters>
        
<InsertParameters>
            
<asp:Parameter Name="sxh" />
            
<asp:Parameter Name="tm" />
            
<asp:Parameter Name="xgrq" />
            
<asp:Parameter Name="IS_SELECTED" />
        
</InsertParameters>
    
</asp:SqlDataSource>
    
<asp:SqlDataSource ID="sqlds" runat="server" ConnectionString="<%$ ConnectionStrings:QingShiConnectionString %>" SelectCommand="Select distinct yhm from ml" ></asp:SqlDataSource>
</asp:Content>
ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Ajax学习
{
    
public partial class EditIndividualCell : System.Web.UI.Page
    {
        SqlDataReader dr;
        DataSet ds;
        SqlDataAdapter da;
        
protected void Page_Load(object sender, EventArgs e)
        {
            getdata();
            
        }
        
private DataSet getdata(){

            SqlConnection conn;
            SqlCommand cmd;
            conn 
= new SqlConnection("Data Source=.;Initial Catalog=QingShi;Integrated Security=True");
            cmd 
= new SqlCommand();
            cmd.Connection 
= conn;
            cmd.CommandText 
= "select distinct YHM from ML";
            conn.Open();
            da 
= new SqlDataAdapter(cmd);
            ds 
= new DataSet();
            da.Fill(ds);
            dr 
= cmd.ExecuteReader();
            
return ds;

        }




        
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList ddl 
= (DropDownList)e.Item.FindControl("ddl");
            ddl.DataSource 
= ds;
            ddl.DataTextField 
= "YHM";
            ddl.DataValueField 
= "YHM";
            TextBox t 
= (TextBox)e.Item.FindControl("TextBox2");
            ddl.DataBind();
            DataRowView drv 
= (DataRowView)e.Item.DataItem;
            ddl.Items.FindByValue(drv[
"YHM"].ToString()).Selected = true;
            
        }

        
protected void Repeater1_DataBinding(object sender, EventArgs e)
        {
            
        }

        
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            
        }
    }
}

转载于:https://www.cnblogs.com/qhnokia/archive/2009/06/18/1505938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值