问一个关于asp:table的问题
private void Bindtable()
{
tbcontent.Rows.Clear();
tbcontent.Controls.Clear();
Hashtable ht = new Hashtable();
int i;
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ht.Add(ds.Tables[0].Rows[i]["ConName"], ds.Tables[0].Rows[i]["ConTent"]);
}
cnt = ht.Count;
tbcontent.HorizontalAlign = HorizontalAlign.Left;
for (i = 0; i < 4; i++)
{
TableRow rows = new TableRow();
for (int j = 0; j < 3; j++)
{
TableCell cell = new TableCell();
if (j == 0)
{
Label lbl = new Label();
lbl.ID = "Label" + i.ToString();
if (i == 0)
{
lbl.Text = "姓名";
}
else if(i==1)
{
lbl.Text = "性别";
}
else if(i==2)
{
lbl.Text = "部门";
}
else
{
lbl.Text = "权限";
}
cell.Controls.Add(lbl);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
else if(j==1)
{
TextBox txtbox = new TextBox();
txtbox.ID = "txtbox" + i.ToString();
if(i==0)
{
txtbox.Text = ht["姓名"].ToString();
ht.Remove("姓名");
}
else if(i==1)
{
txtbox.Text = ht["性别"].ToString();
ht.Remove("性别");
}
else if(i==2)
{
txtbox.Text = ds2.Tables[0].Rows[0]["DepartName"].ToString();
}
else
{
string permission = Session["Permission"].ToString();
if (permission == "0")
txtbox.Text = "管理员";
else if (permission == "-1")
txtbox.Text = "无权限";
else
txtbox.Text = permission;
}
txtbox.Enabled = false;
//txtbox.TextChanged+=new EventHandler(txtbox_TextChanged);
cell.Controls.Add(txtbox);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
else
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "编辑";
btn.CssClass = "btn";
if (i == 2 || i == 3)
{
btn.Enabled = false;
btn.Visible = false;
}
Button btnOK = new Button();
Button btnCancle = new Button();
btnOK.ID = "btnok" + i.ToString();
btnCancle.ID = "btncancle" + i.ToString();
btnOK.Text = "确定";
btnCancle.Text = "取消";
btnOK.Visible = false;
btnCancle.Visible = false;
btnOK.CssClass = "btn";
btnCancle.CssClass = "btn";
AddEvent(btn, new EventHandler(btn_Click));
AddEvent(btnOK, new EventHandler(btnOK_Click));
AddEvent(btnCancle, new EventHandler(btnCancle_Click));
cell.Controls.Add(btn);
cell.Controls.Add(btnOK);
cell.Controls.Add(btnCancle);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
}
}
IDictionaryEnumerator enumerator = ht.GetEnumerator();
while (enumerator.MoveNext())
{
TableRow rows = new TableRow();
for (int j = 0; j < 3; j++)
{
TableCell cell = new TableCell();
if (j == 0)
{
Label lbl = new Label();
lbl.ID = "Label" + i.ToString();
lbl.Text = enumerator.Key.ToString();
cell.Controls.Add(lbl);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
else if(j==1)
{
TextBox txtbox = new TextBox();
txtbox.ID = "txtbox" + i.ToString();
txtbox.AutoPostBack = true;
txtbox.Text = enumerator.Value.ToString();
txtbox.Enabled = false;
//txtbox.TextChanged+=new EventHandler(txtbox_TextChanged);
cell.Controls.Add(txtbox);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
else
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "编辑";
btn.CssClass = "btn";
Button btnOK = new Button();
Button btnCancle = new Button();
btnOK.ID = "btnok" + i.ToString();
btnCancle.ID = "btncancle" + i.ToString();
btnOK.Text = "确定";
btnCancle.Text = "取消";
btnOK.Visible = false;
btnCancle.Visible = false;
btnOK.CssClass = "btn";
btnCancle.CssClass = "btn";
AddEvent(btn, new EventHandler(btn_Click));
AddEvent(btnOK, new EventHandler(btnOK_Click));
AddEvent(btnCancle, new EventHandler(btnCancle_Click));
cell.Controls.Add(btn);
cell.Controls.Add(btnOK);
cell.Controls.Add(btnCancle);
rows.Controls.Add(cell);
tbcontent.Controls.Add(rows);
}
}
i++;
}
sqlcon.Close();
add_item();
}
private void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string id = btn.ID;
int row = Convert.ToInt32(id.Split('n')[1]);
Button btnok = (Button)tbcontent.Rows[row].FindControl("btnok" + row.ToString());
Button btncancle = (Button)tbcontent.Rows[row].FindControl("btncancle" + row.ToString());
TextBox tb = (TextBox)tbcontent.Rows[row].FindControl("txtbox" + row.ToString());
tb.Enabled = true;
btn.Visible = false;
btnok.Visible = true;
btncancle.Visible = true;
ClearEvent(btn);
}
private void btnOK_Click(object sender,EventArgs e)
{
Button btnok = (Button)sender;
string id = btnok.ID;
int row = Convert.ToInt32(id.Split('k')[1]);
Button btncancle = (Button)tbcontent.Rows[row].FindControl("btncancle" + row.ToString());
Button btn = (Button)tbcontent.Rows[row].FindControl("btn" + row.ToString());
TextBox tb = (TextBox)tbcontent.Rows[row].FindControl("txtbox" + row.ToString());
Label lbl = (Label)tbcontent.Rows[row].FindControl("Label" + row.ToString());
if(tb.Text.ToString()=="")
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('内容不能为空')</script>");
else
{
string sql = "update T_UserDetail set Content='" + tb.Text.ToString() + "',OperatorID='" + Session["UserID"].ToString() + "',CTime=getdate() where UserID='" + Session["UserID"].ToString() + "' and Type=(select CIndex from T_Type where ConName='" + lbl.Text.ToString() + "')";
sqlcon = new SqlConnection(strCon);
sqlcon.Open();
sqlcom = new SqlCommand(sql, sqlcon);
sqlcom.ExecuteNonQuery();
sqlcon.Close();
Bindtable();
}
tb.Enabled = false;
btn.Visible = true;
btnok.Visible = false;
btncancle.Visible = false;
ClearEvent(btnok);
ClearEvent(btncancle);
ClearEvent(btn);
}
private void btnCancle_Click(object sender, EventArgs e)
{
Button btncancle = (Button)sender;
string id = btncancle.ID;
int row = Convert.ToInt32(id.Split('e')[1]);
Button btnok = (Button)tbcontent.Rows[row].FindControl("btnok" + row.ToString());
Button btn = (Button)tbcontent.Rows[row].FindControl("btn" + row.ToString());
TextBox tb = (TextBox)tbcontent.Rows[row].FindControl("txtbox" + row.ToString());
tb.Enabled = false;
btn.Visible = true;
btnok.Visible = false;
btncancle.Visible = false;
ClearEvent(btncancle);
ClearEvent(btnok);
ClearEvent(btn);
}
void AddEvent(Button control, EventHandler eventhandler)
{
control.Click += eventhandler;
myEventHandlerList.AddHandler(control, eventhandler);
}
void ClearEvent(Button control)
{
Delegate d = myEventHandlerList[control];
foreach (Delegate dd in d.GetInvocationList())
control.Click -= (EventHandler)dd;
myEventHandlerList.RemoveHandler(control, d);
}
}
TableRow rows = new TableRow();
for (int j = 0; j < 3; j++)
{
TableCell cell = new TableCell();
if (j == 0)
{
Label lbl = new Label();
lbl.ID = "Label" + i.ToString();
lbl.Text = enumerator.Key.ToString();
cell.Controls.Add(lbl);
rows.Controls.Add(cell);
//tbcontent.Controls.Add(rows); 放到外面去
}
else if(j==1)
{
同上
}
else
{
}
}
tbcontent.Controls.Add(rows);