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

来源:互联网 发布:matlab三角波编程 编辑:程序博客网 时间:2024/05/22 05:51
<%@ 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>");
        
    }

}

 

 

 

 

原创粉丝点击