LiangShuainiu
LiangShuainiu
采纳率100%
2018-11-20 08:23

ASP.NET 后台动态添加的控件操作

20
已采纳

选择checkbox时,文本才可以被编辑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
sql.oracl();//数据库连接
DataSet ds = sql.user();//获取数据
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TableRow row = new TableRow();
TableCell cellhead = new TableCell();
TableCell cellcontent = new TableCell();
CheckBox ck = new CheckBox();//复选框
TextBox tb = new TextBox();//文本框
ck.ID ="ck"+ ds.Tables[0].Rows[i]["f_userid"].ToString();//给ID
ck.Text = ds.Tables[0].Rows[i]["f_username"].ToString();//设置文本
tb.ID = "tb" + ds.Tables[0].Rows[i]["f_userid"].ToString();//设置ID
tb.Enabled = false;//复选框没有选的时候文本框不可用
ck.Width=200;//设置宽度
ck.Checked = false;//默认设置
ck.AutoPostBack = true;
ck.CheckedChanged += ck_CheckedChanged;//添加事件
cellhead.Controls.Add(ck);
cellcontent.Controls.Add(tb);
cellhead.HorizontalAlign = HorizontalAlign.Right;
cellcontent.HorizontalAlign = HorizontalAlign.Left;
row.Cells.Add(cellhead);
row.Cells.Add(cellcontent);
this.Table1.Rows.Add(row);
}
}

    void ck_CheckedChanged(object sender, EventArgs e)
    {
        该怎么写?
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

}
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • LiangShuainiu LiangShuainiu 3年前

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    namespace WebApplication2
    {
    public partial class WebForm1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    sql.oracl();
    DataSet ds = sql.user();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    TableRow row = new TableRow();
    TableCell cellhead = new TableCell();
    TableCell cellcontent = new TableCell();
    CheckBox ck = new CheckBox();
    TextBox tb = new TextBox();
    ck.ID ="ck"+ ds.Tables[0].Rows[i]["f_userid"].ToString();
    ck.Text = ds.Tables[0].Rows[i]["f_username"].ToString();
    tb.ID = "tb" + ds.Tables[0].Rows[i]["f_userid"].ToString();
    tb.Enabled = false;
    Response.Write("a=&quot; + ds.Tables[0].Rows[i][&quot;f_userid&quot;].ToString() + &quot;;");
    ck.Width=200;
    ck.Checked = false;
    ck.AutoPostBack = true;
    ck.CheckedChanged += ck_CheckedChanged;
    cellhead.Controls.Add(ck);
    cellcontent.Controls.Add(tb);
    cellhead.HorizontalAlign = HorizontalAlign.Right;
    cellcontent.HorizontalAlign = HorizontalAlign.Left;
    row.Cells.Add(cellhead);
    row.Cells.Add(cellcontent);
    this.Table1.Rows.Add(row);
    }
    }

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

    }

    点赞 评论 复制链接分享

为你推荐