创建运行时TexBox [英] Create Runtime TexBox

查看:65
本文介绍了创建运行时TexBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主席先生...
我正在尝试使用Web方法在运行时创建多个文本框.
我想将所有数据保存到mssql数据库中,并在单击按钮后创建文本框.所以请帮助我.

Dir Sir...
I am trying to create multiple textbox at run time using web method.
And I want to save all data into mssql database.And textbox create after click button. So plz help me out.

推荐答案

学生表的SQL脚本

这是用于存储学生信息的示例表
SQL Script for Student Table

This is a sample table for storing the student information
USE [DB Name]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Stud_Name] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Age] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Addr] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Gender] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Stud_Qualification] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF





.aspx代码这里只是一个DropDownList,用于触发SelectedIndexChanged事件,正如您所说的那样,您想在按钮上单击时创建文本框,正在使用此事件...





.Aspx code Here just a DropDownList for firing a SelectedIndexChanged event as you said you want to create textboxes on button click am using this event...

<div>
       Select Id: <asp:dropdownlist id="DropDownList1" runat="server" autopostback="True" appenddatabounditems="true" xmlns:asp="#unknown">
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:dropdownlist>
</div>




web.confog代码,这是用于连接sql的信息,您的信息...




web.confog Code this is for connecting sql fill your information...

<connectionstrings>
   <add name="ConString" connectionstring="Data Source=[SQL serverName];Initial Catalog=[DB name];Persist Security Info=True;User ID=sa;Password=[*****]" providername="System.Data.SqlClient" />
 </connectionstrings>




--aspx.cs代码
在此处,所选更改后的事件名称显示在动态创建的单个char文本框中,因此请查看




--aspx.cs code
here on selected changed event name is displaying in a single char text box which is dynamically creating so please check it out

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class Default2 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);

        if (!IsPostBack)
        {
            ddl();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("Select Stud_Name from student where Id='" + DropDownList1.SelectedItem + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string s = ds.Tables[0].Rows[0][0].ToString();
            for (int i = 0; i < s.Length; i++)
            {
                string id = "txt" + i.ToString();
                TextBox txt = new TextBox();
                txt.ID = id;
                txt.Text = s[i].ToString();
                txt.Width = 20;
                txt.MaxLength = 1;
                txt.ReadOnly = true;
                form1.Controls.Add(txt);
            }
        }
        catch
        {
        }
    }
    private void ddl()
    {
        try
        {
            cmd = new SqlCommand("Select Id from student ", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ds.Clear();
            DropDownList1.Items.Add("Select...");
            da.Fill(ds);

            DropDownList1.DataSource = ds;
            DropDownList1.DataTextField = "Id";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
        }
        catch
        {
        }
    }
}





希望对您有所帮助:)
谢谢您的宝贵时间...
-Mahesh





I hope it will help you :)
Thanks for your time...
-Mahesh


函数GetDynamicTextBox(value){
返回''< input name ="DynamicTextBox" type ="text" value =''+ value +''"/>''+
''< input type ="button" value =删除" onclick ="RemoveTextBox(this)"/>''
}
函数AddTextBox(){
var div = document.createElement(``DIV'');
div.innerHTML = GetDynamicTextBox(");
document.getElementById("TextBoxContainer").appendChild(div);
}

函数RemoveTextBox(div){
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}

函数RecreateDynamicTextboxes(){
var values = eval(''& lt;%= Values%& gt;'');
if(values!= null){
var html =";
for(var i = 0; i& lt; values.length; i ++){
html + =" + GetDynamicTextBox(values [i])+";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;
function GetDynamicTextBox(value){
return ''<input name="DynamicTextBox" type="text" value="'' + value + ''" />'' +
''<input type="button" value="Remove" onclick="RemoveTextBox(this)" />''
}
function AddTextBox() {
var div = document.createElement(''DIV'');
div.innerHTML = GetDynamicTextBox("");
document.getElementById("TextBoxContainer").appendChild(div);
}

function RemoveTextBox(div) {
document.getElementById("TextBoxContainer").removeChild(div.parentNode);
}

function RecreateDynamicTextboxes() {
var values = eval(''&lt;%=Values%&gt;'');
if (values != null) {
var html = "";
for (var i = 0; i &lt; values.length; i++) {
html += "" + GetDynamicTextBox(values[i]) + "";
}
document.getElementById("TextBoxContainer").innerHTML = html;
}
}
window.onload = RecreateDynamicTextboxes;


这篇关于创建运行时TexBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆