protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 0;
foreach (TableCell tc in e.Row.Cells)
{
if (tc.Text == "1")
{
CheckBox cb = new CheckBox();
cb.LabelAttributes.Add("modelname", e.Row.Cells[1].Text);
cb.LabelAttributes.Add("columnid", i.ToString());
tc.Controls.Add(cb);
}
else if (tc.Text == "0" || tc.Text == null) tc.Text = "";
i++;
}
}
这是我动态添加的checkbox,现在想在databound方法中对其值进行设定,请问怎么做啊
{
int i = 0;
foreach (TableCell tc in e.Row.Cells)
{
if (tc.Text == "1")
{
CheckBox cb = new CheckBox();
cb.LabelAttributes.Add("modelname", e.Row.Cells[1].Text);
cb.LabelAttributes.Add("columnid", i.ToString());
tc.Controls.Add(cb);
}
else if (tc.Text == "0" || tc.Text == null) tc.Text = "";
i++;
}
}
这是我动态添加的checkbox,现在想在databound方法中对其值进行设定,请问怎么做啊
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Control c in e.Row.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
CheckBox cb = (CheckBox)c;
}
}
}
}要注意是HeaderRow 、 DataRow 还是FooterRow 。
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBoxID");
}
要在databound方法中实现,谢谢……另外,foreach找子控件的方法用过,写了3层都没找到checkbox……
//这样你在DataBound时只需要据条件给CheckBox赋值,隐藏等操作就好了//取值还好取
你是在RowDataBound中添加的CheckBox?
你要在哪里找动态创建的CheckBox
但是总感觉有漏洞
而且实际我这样写也出现了问题protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = 2;
foreach (TableCell tc in e.Row.Cells)
{
if (tc.Text == "1")
{
CheckBox cb = new CheckBox();
cb.ID = e.Row.RowIndex.ToString() + i.ToString();
cb.LabelAttributes.Add("modelname", e.Row.Cells[1].Text);
cb.LabelAttributes.Add("columnid", i.ToString());
tc.Controls.Add(cb);
}
else if (tc.Text == "0" || tc.Text == null) tc.Text = "";
i++;
}
} protected void GridView1_DataBound(object sender, EventArgs e)
{ for (int i = 0; i < ((DataTable)GridView1.DataSource).Rows.Count; i++)
{
for (int j = 0; j < ((DataTable)GridView1.DataSource).Columns.Count; j++)
{
CheckBox ctrl = new CheckBox();
ctrl = (CheckBox)GridView1.Rows[i].FindControl(i.ToString() + j.ToString());
if (ctrl is CheckBox)
{
string[] param = new string[3];
param[0] = ddlPersonName.SelectedValue.ToString();
param[1] = ((CheckBox)ctrl).Attributes["modelname"];
param[2] = GridView1.HeaderRow.Cells[Convert.ToInt32(((CheckBox)ctrl).Attributes["colunmid"])].Text;
DataTable funstate = controlDataBll.loadValue("loadfunstate", param); if (funstate.Rows[0][0].ToString() == "1")
{
((CheckBox)ctrl).Checked = true;
}
else if (funstate.Rows[0][0].ToString() == "0")
{
((CheckBox)ctrl).Checked = false;
}
}
}
}
}
现在这样写在param[1] = ((CheckBox)ctrl).Attributes["modelname"];
param[2] = GridView1.HeaderRow.Cells[Convert.ToInt32(((CheckBox)ctrl).Attributes["colunmid"])].Text;
处获得attributes时得到的都是空值
P.S.:gridview1有6列,第一列序号,第二列modelname,然后4列checkbox(可能有可能没有)
CheckBox cb = new CheckBox();
cb.Checked = ...
{
t_NOTICE_USER.USERID = dr_Col.ItemArray[0].ToString();
}
{
Control ctrl = row.FindControl("cblist");
if ((ctrl as CheckBox).Checked)
{
//写代码
}
}