如何在动态表中添加按钮 [英] How to add button in dynamic table

查看:76
本文介绍了如何在动态表中添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友我在节目中遇到问题



我没有得到如何在动态表格中添加BUTTON。

Friends I am facing problem in on program

I am not getting how to add BUTTON in dynamic table.

推荐答案

您可以按照此代码解决您的问题。

You can resolve your problem by follow this code.
List<Button> buttons = new List<Button>();
    for (int i = 0; i < 10; i++)
    {
        Button newButton = new Button();
        buttons.Add(newButton);
        this.Controls.Add(newButton);   
    }


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

namespace DynamicTable
{
    public partial class Table : System.Web.UI.Page
    {
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        StringBuilder htmlTable = new StringBuilder();
        StringBuilder htmlTable1 = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                BindData1();

            }


        }

        public void BindData()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=IMICRON006;Integrated Security=true;Initial Catalog=imicron";
            SqlCommand cmd = new SqlCommand("SELECT * FROM Employee", con);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            htmlTable.Append("<table border='1'>");
            htmlTable.Append("<tr style='background-color:green; color: White;'><th>Customer ID.</th><th>Name</th><th>City</th><th>Pin-no</th><th>Edit</th></tr>");

            if (!object.Equals(ds.Tables[0], null))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                    {

                        htmlTable.Append("<tr style='color: White;'>");
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Id"] + "</td>");
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["name"] + "</td>");
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["city"] + "</td>");
                        htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["pin"] + "</td>");
                        htmlTable.Append("<td>" + "<input type='button' onclick ='__doPostBack(\'btn1\',\'\') value='Click Here' />" + "</td>");
                        //htmlTable.Append("<td>" + <a href="Edit.aspx?id=+Id+"></a> + "</td>");
                        htmlTable.Append("</tr>");


                    }
                    htmlTable.Append("</table>");
                    DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
                }



            }



            else
            {
                htmlTable.Append("<tr>");
                htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                htmlTable.Append("</tr>");
            }
        }

           public void BindData1()
            {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=IMICRON006;Integrated Security=true;Initial Catalog=imicron";
            SqlCommand cmd = new SqlCommand("SELECT * FROM Department", con);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds1);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            htmlTable1.Append("<table border='1'>");
            htmlTable1.Append("<tr style='background-color:green; color: White;'><th>ID.</th><th>name</th></tr>");

            if (!object.Equals(ds.Tables[0], null))
            {
                if (ds1.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)

                    {

                        htmlTable1.Append("<tr style='color: White;'>");
                        htmlTable1.Append("<td>" + ds1.Tables[0].Rows[i]["Id"] + "</td>");
                        htmlTable1.Append("<td>" + ds1.Tables[0].Rows[i]["name"] + "</td>");
                        htmlTable1.Append("</tr>");
                    }
                    htmlTable.Append("</table>");
                    DBDataPlaceHolder1.Controls.Add(new Literal { Text = htmlTable1.ToString() });
                }
                else
                {
                    htmlTable1.Append("<tr>");
                    htmlTable1.Append("<td align='center' colspan='4'>There is no Record.</td>");
                    htmlTable1.Append("</tr>");
                }
            }
        }
    }
}


这篇关于如何在动态表中添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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