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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿