通过文本框和按钮动态创建控件 [英] Dynamically creating control through textbox and button

查看:72
本文介绍了通过文本框和按钮动态创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次输入数字成功,但是如果再次删除该数字并输入数字,则表示正在添加.
像在文本框中的第一个输入4并输入.删除该数字文本框输入6,然后输入.
它添加4 + 6" 4个右加新6个将显示总共10个.

It is successful the first time you enter a number, but if you remove that number again and enter it is adding.
Like first input 4 in textbox and enter. Remove that number textbox input 6 and enter.
It "adding 4+6" 4 al right plus new 6 more will display means totally 10.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class assigned: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack && DDLCount > 0)
        {
            CreateDropDownLists();
        }

    }
    public int DDLCount
    {
        get
        {
            // Try to get an instance of the DDLCount ViewState object
            object temp = ViewState["DDLCount"];
            // If temp is not null then cast it to int and return it,
            // otherwise return 0
            return temp == null ? 0 : (int)temp;
        }
        set { ViewState["DDLCount"] = value; }
    }
    private void CreateDropDownLists()
    {
        int a = Convert.ToInt32(TextBox4.Text);
        // This is just to set the count to a default value
        // You may possibly need to generate the count of dropdownlists
        // in some other manner, depending on your project
        //if (DDLCount == 0)
        DDLCount = a;   //i   have  to  ask  HOw  DDLCount  is  0
        // I  can  not   understand  it 


        for (int i = 0; i < DDLCount; i++)
        {
            // Create the dropdownlists
            DropDownList ddl = new DropDownList();
            DropDownList dd2 = new DropDownList();
            ddl.ID = "Text" + i;
            dd2.ID = "Texts" + i;
            LiteralControl l1 = new LiteralControl("<br></br>");

            //ddl.Items.Add(new ListItem("TestText_" + i, "TestValue_" + i));
            //ddl.Items.Add("anant");
            //ddl.Items.Add("pratibha");
            //ddl.Items.Add("rahul");
            SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con1.Open();
            string st = "select  doe_name from   Doe_detail ";
            SqlCommand cmd = new SqlCommand(st, con1);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
               ddl.Items.Add(dr.GetString(0).ToString());


            }
            con1.Close();

            SqlConnection con2 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
            con2.Open();
            string std = "select  phone from   Doe_detail ";
            SqlCommand cmd1 = new SqlCommand(std, con2);
            SqlDataReader dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                dd2.Items.Add(dr1.GetString(0).ToString());


            }
            con2.Close();
            
            // Add it to the panel
            Panel3.Controls.Add(ddl);
            Panel3.Controls.Add(dd2);

        }
    }



   
    protected void TextBox4_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateDropDownLists();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string value = "";
        string ids = "";
        // Cycle through each dropdownlist
        for (int i = 0; i < DDLCount; i++)
        {
            // Find the dropdownlist inside the Panel
            DropDownList ddl = Panel3.FindControl("Text" + i) as DropDownList;

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            value = ddl.SelectedValue.ToString();
            DropDownList dd2 = Panel3.FindControl("Texts" + i) as DropDownList;

            // Set the label to the SelectedValue of the dropdownlist
            //Label1.Text += ddl.SelectedValue + " : ";
            ids = dd2.SelectedValue.ToString();
            SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");

            con.Open();

            SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "','"+ids+"')", con);
            cmd1.ExecuteNonQuery();
            con.Close();
        }
    }
}

推荐答案

朋友您好,您的问题是什么....

如何创建动态下拉列表..

但是,在您现有的代码中出现错误或您想要的内容时,请指定要提供完整代码的那件事...
Hello friend what is your question....

How to create a dynamic drop down list..

But in your existing code where you got error or what you want please specify that thing instaed of giving your whole code...


这篇关于通过文本框和按钮动态创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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