日期:2014-05-17  浏览次数:20418 次

动态生成textbox
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
  <style type="text/css">
  #abc
  {
  height: 216px;
  }
  </style>
</head>
<body>
  <form id="form1" runat="server">
  <div id="abc">
  <asp:Label ID="Label1" runat="server" Text="1"></asp:Label>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  </div>
  <div>
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  </div>
  <div>
   
  </div>
  </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  TextBox t = new TextBox();
  t.ID = "a";
  this.Label1.Controls.Add(t);
  }
}
为什么会将我Label1的值覆盖了,怎么能不覆盖Label1并且能在Label1下面 求大家帮忙

------解决方案--------------------
把你的刷新按钮放在UpdatePannel中,不去刷新TextBox所在的区域就可以了!
------解决方案--------------------
下面是动态添加行的代码(行中包括textbox),你稍微改改就就ok了
C# code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<script type="text/javascript">
function contactlist(){
        var table = document.getElementById("contactTable");
        var tr = table.insertRow(table.rows.length);//添加一行
        tr.align="center";
        tr.bgcolor="#F3F4F3";
        tr.height="23";

        //添加五列
        var td0 = tr.insertCell(0);
        td0.width="20%";
        var td1 = tr.insertCell(1);
        td1.width="20%";
        var td2 = tr.insertCell(2);
        td2.width="8%";
        var td3 = tr.insertCell(3);
        td3.width="30%";
        var td4 = tr.insertCell(4);
        td4.width="20%";
        
        //设置列内容
        td0.innerHTML = "";
        td1.innerHTML = "<input type='file' id='dataAudio' name='dataAudio'/><input type='hidden' name='dataAudios'/><input type='hidden' name='audioOldName'/>";
        td2.innerHTML = "<input type='text' name='playDateTime' id='playDateTime' onkeyup='validateNumber(this);' size='5' maxlength='3' style='color: red' />";
        td3.innerHTML = "<input type='text' name='content' id='content' size='35' maxlength='300'/>";
        td4.innerHTML = "<input type='button' value='删除' onclick='removeRow(this.parentNode.parentNode)'/>";
        
    }

function removeRow(r)
{
    var root = r.parentNode;
    var allRows = root.getElementsByTagName('tr');
    if(allRows.length > 3){