日期:2014-05-17 浏览次数:20403 次
TextBox tbUserName=(TextBox)e.Item.FindControl("tbUserName") ddl.Attributes["onchange"]="myonchange(this,'"+tbUserName.ClientID+"')"
<%@ Page Language="C#" EnableViewState="true" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { System.Data.DataSet ds = new System.Data.DataSet(); System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser"); System.Data.DataRow dr; dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32))); dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String))); dataTable1.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["UserId"] }; for (int i = 0; i < 8; i++) { dr = dataTable1.NewRow(); dr[0] = i; dr[1] = "【孟子E章】" + i.ToString(); dataTable1.Rows.Add(dr); } Rp1.DataSource = dataTable1; Rp1.DataBind(); } } protected void Rp1_ItemCreated(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { TextBox tbUserName = (TextBox)e.Item.FindControl("tbUserName"); DropDownList ddl = e.Item.FindControl("DropDownList1") as DropDownList; ddl.Attributes["onchange"] = "myonchange(this,'" + tbUserName.ClientID + "')"; } } protected void Rp1_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) { TextBox tbUserName = (TextBox)e.Item.FindControl("tbUserName"); DropDownList ddl = e.Item.FindControl("ddl") as DropDownList; ddl.Attributes["onchange"] = "myonchange(this,'" + tbUserName.ClientID + "')"; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script type="text/javascript"> function myonchange(s, t) { alert(document.getElementById(t).value); } </script> </head> <body> <form id="form1" runat="server"> <asp:ListView ID="Rp1" runat="server" OnItemCreated="Rp1_ItemCreated" OnItemDataBound="Rp1_ItemDataBound"> <ItemTemplate> <div> <asp:TextBox ID="tbUserName" runat="server" Text='<%#Eval("UserName") %>'></asp:TextBox> <asp:DropDownList ID="ddl" runat="server"> <asp:ListItem>AA</asp:ListItem> <asp:ListItem>BB</asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="DropDownList1" runat=&quo