如何动态添加子控件(动态添加TextBox控件)

转载 2007年09月24日 12:11:00
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InQuiryAdd2.aspx.cs" Inherits="InQuiryManager_InQuiryAdd2" %>

<!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>
    
<link href="../CSS/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table style="width: 100%">
            
<tr>
                
<td colspan="2"  bgcolor="#5D7B9D" ">
             
<div class="v12WhiteB" style="padding-top: 3px;">
                                                        
<id="A2" href="#"><font color="white">娣诲姞闂</font></a></div>
                
</td>
                
<asp:Label ID="Label1" runat="server" ForeColor="Red" Width="300px"></asp:Label><asp:Label ID="Lbl_WrongMessage" runat="server" ForeColor="Red" Width="300px"></asp:Label></tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Lbl_InQuiryQuestion" runat="server" Height="11px" Text="鏍囥€€棰? Width="67px"></asp:Label></td>
                
<td>
                    
<asp:TextBox ID="Txt_InQuiryQuestion" runat="server" CssClass="ipt120px" Width="218px"></asp:TextBox></td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Lbl_IsShow" runat="server" Text="鏄惁鏄剧ず"></asp:Label></td>
                
<td>
                    
<asp:RadioButton ID="Rad_Yes" runat="server" GroupName="isshow" Text="  鏄? " />
                    
<asp:RadioButton ID="Rad_No" runat="server" GroupName="isshow" Text="  鍚?" /></td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Lbl_CheckType" runat="server" Text="閫夐」绫诲瀷"></asp:Label></td>
                
<td>
                    
<asp:RadioButton ID="Rad_CheckOne" runat="server" GroupName="CheckType" Text="鍗曢€? />
                    <asp:RadioButton ID="
Rad_CheckMore" runat="server" GroupName="CheckType" Text="澶氶€? /></td>
            </tr>
            <tr>
                <td colspan="
2" bgcolor="#5D7B9D">
                       
<div class="v12WhiteB" style="padding-top: 3px;">
                                                        
<id="A1" href="#"><font color="white">娣诲姞閫夐」</font></a></div>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated">
                       
                    
</asp:DataList></td>
                
<td>
                    
<asp:Button ID="btn_AddQuestion" runat="server" Text="娣诲姞閫夐」" OnClick="btn_AddQuestion_Click" CssClass="btn80px" /></td>
            
</tr>
            
<tr>
                
<td align="center">
                    
<asp:Button ID="btn_Save" runat="server" Text="鎻愪氦" OnClick="btn_Save_Click" Width="100px" CssClass="btn80px" /></td>
                
<td align="center">
                    
<asp:Button ID="btn_Reset" runat="server" OnClick="btn_Reset_Click" Text="閲嶇疆" Width="100px" CssClass="btn80px" /></td>
            
</tr>
            
<tr>
                
<td align="center" colspan="2">
                    
</td>
            
</tr>
        
</table>
    
    
</div>
    
</form>
</body>
</html>

 

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BigOA.Logic;

public partial class InQuiryManager_InQuiryAdd2 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!this.IsPostBack)
        
{
            DataTable dt 
= new DataTable();
            dt.Columns.Add(
"newcolumns");
            
for (int i = 0; i < 3; i++)
            
{
                DataRow row 
= dt.NewRow();
                dt.Rows.Add(row);
            }

            
this.DataList1.DataSource = dt;
            
this.DataList1.DataBind();
            ViewState[
"dt"= dt;
        }

    }

    
protected void btn_AddQuestion_Click(object sender, EventArgs e)
    
{
        DataTable dt 
= (DataTable)ViewState["dt"];
        
for (int i = 0; i < this.DataList1.Items.Count; i++)
        
{
            TextBox tb 
= (TextBox)this.DataList1.Items[i].Controls[0];
            dt.Rows[i][
0= tb.Text;
        }


        DataRow row 
= dt.NewRow();
        row[
0= "";
        dt.Rows.Add(row);
        ViewState[
"dt"= dt;
        
this.DataList1.DataSource = dt;
        
this.DataList1.DataBind();
    }

    
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
    
{
        DataRowView drv 
= (DataRowView)(e.Item.DataItem);

        
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        
{
            TextBox tmptxt 
= new TextBox();

            
if (drv != null)
            
{
                tmptxt.Text 
= drv[0].ToString();
            }

            tmptxt.MaxLength 
= 80;
            e.Item.Controls.Add(tmptxt);
        }

    }

    
protected void btn_Save_Click(object sender, EventArgs e)
    
{


        
try
        
{
            Lbl_WrongMessage.Text 
= "";
            InQuiryQuestion QuiryQuestion 
= new InQuiryQuestion();
            
if (Txt_InQuiryQuestion.Text.Trim() != "")
            
{
                QuiryQuestion.Title 
= Txt_InQuiryQuestion.Text.ToString();
            }

            
else
            
{
                
throw new Exception("璋冩煡鏍囬涓嶈兘涓虹┖锛?);
            }

            
if (Rad_Yes.Checked || Rad_No.Checked)
            
{
                
if (Rad_Yes.Checked)
                
{
                    QuiryQuestion.IsShow 
= "鏄?;
                }

                
else
                
{
                    QuiryQuestion.IsShow 
= "鍚?;
                }

            }

            
else
            
{
                
throw new Exception("鏄惁鏄剧ず鑷冲皯瑕侀€夋嫨涓€椤癸紒");
            }

            
if (Rad_CheckMore.Checked || Rad_CheckOne.Checked)
            
{
                
if (Rad_CheckMore.Checked)
                
{
                    QuiryQuestion.CheckType 
= "澶氶€?;
                }

                
else
                
{
                    QuiryQuestion.CheckType 
= "鍗曢€?;
                }

            }

            
else
            
{
                
throw new Exception("绫诲瀷鑷冲皯瑕侀€夋嫨涓€椤癸紒");
            }



            
for (int i = 0; i < this.DataList1.Items.Count; i++)
            
{
                TextBox txt_tmp 
= (TextBox)this.DataList1.Items[i].Controls[0];
                
if (txt_tmp.Text.Trim() == "")
                
{
                    txt_tmp.Focus();
                    
throw new Exception("鎮ㄦ病鏈夊~鍐欒皟鏌ラ€夐」!");

                }

            }



            
int id = QuiryQuestion.Insert();

            
for (int i = 0; i < this.DataList1.Items.Count; i++)
            
{
                TextBox txt_answer 
= (TextBox)this.DataList1.Items[i].Controls[0];
                SaveAnswer(id, txt_answer.Text);
            }


            Page.ClientScript.RegisterStartupScript(
typeof(string), """<script language='javascript'>alert('娣诲姞鎴愬姛');window.location.href=window.location.href;</script>");
            Txt_InQuiryQuestion.Text 
= "";
            Rad_No.Checked 
= false;
            Rad_Yes.Checked 
= false;
            Rad_CheckMore.Checked 
= false;
            Rad_CheckOne.Checked 
= false;
        }

        
catch (Exception ex)
        
{
            Lbl_WrongMessage.Text 
= ex.Message.ToString();
        }

    }


    
private void SaveAnswer(int questionID, string answer)
    
{
        
try
        
{
            Lbl_WrongMessage.Text 
= "";
            InQuiryAnswer QuiryAnswer 
= new InQuiryAnswer();
            QuiryAnswer.Answer 
= answer;
            QuiryAnswer.InQuiryQuestionID 
= questionID;
            QuiryAnswer.Insert();
        }

        
catch (Exception ex)
        
{
            Lbl_WrongMessage.Text 
= ex.Message.ToString();
        }

    }

    
protected void btn_Reset_Click(object sender, EventArgs e)
    
{

        Lbl_WrongMessage.Text 
= "";
        Txt_InQuiryQuestion.Text 
= "";
        Rad_Yes.Checked 
= false;
        Rad_No.Checked 
= false;
        Rad_CheckMore.Checked 
= false;
        Rad_CheckOne.Checked 
= false;
        Page.ClientScript.RegisterStartupScript(
typeof(string), """<script language='javascript'>window.location.href=window.location.href;</script>");
        
    }

}

 

 

 

 

相关文章推荐

ASP.NET动态添加TextBox控件

ASP.NET动态添加TextBox控件

动态生成textbox控件

  • 2015年02月15日 12:27
  • 315KB
  • 下载

在 Visual C# 中,怎样为 TextBox 控件添加水印文字

http://www.zhimengzhe.com/windows/31513.html 在使用 C# 的 TextBox 控件时,有时候会有以下需求:在用户没有输入文字时,TextBo...

动态添加控件

  • 2017年11月01日 16:13
  • 21KB
  • 下载

asp.net 动态添加控件

  • 2017年11月06日 21:33
  • 6.63MB
  • 下载

Android GridLayout 动态添加子控件 + 平均分配空间

Android GridLayout 动态添加子控件 + 平均分配空间 有时候会遇到这样的需求: 1. 要求子控件网格布局,平均分布 2. 内容根据接口动态加载 3. 父控件充满界面剩余空间,不...

动态添加控件

  • 2015年12月27日 09:09
  • 40.67MB
  • 下载

iOS通过代码动态添加和删除控件

  • 2015年01月19日 16:55
  • 89KB
  • 下载

GridView中动态添加模板列和其子控件集合

一、实施说明: 1 实施原因: 因为在做一个MOSS WebPart控件时候,遇到一个问题,如何选择需要导出的数据列; GridView的数据源是动态的,所以没有办法静态使用模板列,来选择导出的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何动态添加子控件(动态添加TextBox控件)
举报原因:
原因补充:

(最多只允许输入30个字)