asp.net 动态添加控件
2012-05-02 18:42
351 查看
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { List<Student> preStus = new List<Student>(); protected void Page_Load(object sender, EventArgs e) { Student stu = new Student(); stu.Age = "23"; stu.Name = "zhangsan"; Student stu1 = new Student(); stu1.Age = "23"; stu1.Name = "zhangsan"; preStus.Add(stu); preStus.Add(stu1); string jsMethodLoad = ""; if (!IsPostBack) { for (int i = 0; i < preStus.Count; i++) { jsMethodLoad += " addnew('"+preStus[i].Name+"','"+preStus[i].Age+"');"; } } this.RegisterStartupScript("ss", "<script> " + jsMethodLoad + "</script>"); // Response.Write("<script> " + jsMethodLoad + "</script>"); } protected void Button1_Click(object sender, EventArgs e) { System.Collections.Specialized.NameValueCollection nv = this.Request.Form; List<Student> stus = new List<Student>(); foreach (string item in nv) { if (item.StartsWith("Student.name") && item!="Student.name") { string[] eles = item.Split('.'); Student stu = new Student(); foreach (string subitem in nv) { if (subitem.EndsWith("name." + eles[2])) { stu.Name = Request.Form[subitem]; } else if (subitem.EndsWith("age." + eles[2])) { stu.Age = Request.Form[subitem]; } } stus.Add(stu); } } } public class Student { string name; public string Name { get { return name; } set { name = value; } } string age; public string Age { get { return age; } set { age = value; } } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="js/jquery-1.6.1.min.js" type="text/javascript"></script> <script type="text/javascript"> var i = 0; function addnew(name,name,age){ var temp= $("div[name='_template']").clone(); temp.attr('name','ready'); var inputs= temp.find('input'); inputs.each(function() { //根据input-name 来修给初始值 var iName = $(this).attr('name') if (name != null && iName == "Student.name") { $(this).attr('value', name); } else if (age != null && iName == "Student.age") { $(this).attr('value', age); } //编号索引 $(this).attr('name', $(this).attr('name') + "." + i); }); var container=$("#_container"); container.append(temp); i++; } // // // // function SetJsonResult() {// var container = $("#_container");// var result = "[";// var isFirstt = true;// container.find("div[name='ready']").each(// function() {// if (!isFirstt) {// result += ",";// } else {// isFirstt = false;// }// result += "{";// var temp = $(this);// var isFirst = true;// temp.find("input[type='text']").each(function() {// if (!isFirst) {// result += ",";// } else {// isFirst = false;// }// result += $(this).attr('name') + ":" + $(this).attr('value');// });// result += "}";// }// );// result += "]";// // $("#Text1").attr('value',result);// } </script> <style type="text/css"> #Text1 { width: 374px; height: 152px; } </style></head><body> <form id="form1" runat="server"> <div> <input type='button' onclick='addnew()' value='add'> <div name="_template"> name<input type ='text' name ='Student.name'> age <input type='text' name ='Student.age'> </div> <div id='_container'> </div> </div> <input id="Text1" type="text" /> <input id="Button2" type="button" onclick="SetJsonResult()" value="button" /> <asp:Button ID="Button3" runat="server" Text="Button" /> <asp:Button ID="addtoplace" runat="server" onclick="addtoplace_Click" Text="addtoplace" /> <asp:PlaceHolder ID="PlaceHolder1" runat="server"> </asp:PlaceHolder> </form></body></html>
相关文章推荐
- ASP.NET 后台动态添加控件checkbox
- asp.net为控件动态添加事件
- asp.net为控件动态添加事件
- 在ASP.NET页面中动态添加控件
- asp.net页面动态添加控件造成控件事件不响应的解决办法
- ASP.NET给Table动态添加删除行,并且得到控件的值
- 在ASP.NET页面中动态添加控件
- 在Asp.net中动态添加css文件,js文件,控件
- asp.net动态添加控件并获取值
- asp.net动态添加控件和获取控件的值
- 在ASP_NET页面中动态添加控件 - 陈希章 - 博客园
- asp.net 中服务器端控件动态添加和删除
- ASP.NET页面中动态增加的控件、添加事件
- ASP.NET动态添加用户控件的方法
- asp.net从后台动态添加用户控件
- [ASP.NET AJAX]谨记给UpdatePanel中动态添加的控件赋ID
- asp.net 中服务器端控件动态添加和删除
- ASP.NET动态添加用户控件的方法
- asp.net 服务器端动态生添加控件
- asp.net中动态创建控件添加事件