asp.net动态生成控件及访问控件

using System;
using System.Data;
using System.Configuration;
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 System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 在此处放置用户代码以初始化页面
       // Session["p_userid"] = "admin";
       // Session["p_loginName"] = "admin";
       // Session["p_created"] = "admin";
        if (Session["p_userid"] == null || Session["p_loginName"] == null || Session["p_created"] == null)
        {
            Response.Write("<script>alert('请先登陆');location.href='www.baidu.com';</script>");
            Response.End();
        }
        else
        {
            Panel1.Controls.Add(new LiteralControl("<table>"));
            Panel1.Controls.Add(new LiteralControl("\n"));
            SqlConnection connPartner = new SqlConnection("server=.;uid=sa;pwd=123;database=cdsq");
            SqlCommand cmd = new SqlCommand("select * from link", connPartner);
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = cmd;
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Panel1.Controls.Add(new LiteralControl("<tr>"));
                TableCell cell = new TableCell();//创建单元格,也就是第一列
                cell.Text = ds.Tables[0].Rows[i]["link_name"].ToString();//设置单元格内的文本
                Panel1.Controls.Add(cell);
                Panel1.Controls.Add(new LiteralControl("<td>"));
                TextBox textBox = new TextBox();
                textBox.ID = "t" + ds.Tables[0].Rows[i]["link_id"].ToString();
                Panel1.Controls.Add(textBox);
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("<td>"));
                Label lb = new Label();
                lb.ID = "l" + ds.Tables[0].Rows[i]["link_id"].ToString();
                lb.Text = "&nbsp;" + ds.Tables[0].Rows[i]["link_url"].ToString();
                //lb.CssClass = "registerInfo";
                Panel1.Controls.Add(lb);
                Panel1.Controls.Add(new LiteralControl("</td>"));
                Panel1.Controls.Add(new LiteralControl("</tr>"));
                Panel1.Controls.Add(new LiteralControl("\n"));
            }

            Button rebt = new Button();
            rebt.ID = "redirect";
            rebt.Text = " 不填了 ";
            rebt.Click += new EventHandler(rebt_Click);
            Panel1.Controls.Add(rebt);
        }
    }
    private void rebt_Click(object sender, EventArgs e)
    {
        Response.Redirect();
    }

}

//访问动态生成控件,可以通过FindControl方法,代码如下:

for(int i=0;i<ds.Tables[0].Rows.Count;i++)
       {
                Label lb;
                 lb=(Label)Panel1.FindControl("l"+ds.Tables[0].Rows[i]["id"].ToString());
             if(lb!=null)
            {
                    lb.Text="&nbsp;"+ds.Tables[0].Rows[i]["tip"].ToString();
                     lb.CssClass="registerInfo";
               }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值