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个回答

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

    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐