LiangShuainiu 2018-11-20 08:23 采纳率: 66.7%
浏览 1415
已采纳

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

选择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 2018-11-20 08:27
    关注

    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)
        {
    
        }
    }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘