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

翻译|其它|编辑:郝浩|2007-09-25 11:23:18.000|阅读 2051 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

<%...@ 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>");
        
    }
}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP