一个很奇怪的有关问题
一个很奇怪的问题?
运行后的结果 this.TextBox1.Text= 222222222222
也就是说 cbox.Checked 始终=false
为什么我明明提交的时候在所对应的checkbox里面打了勾
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataKeyNames = new string[] { "id " };
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cbox = (CheckBox)row.FindControl( "CheckBox1 ");
if (cbox.Checked == true)
{
this.TextBox1.Text = GridView1.DataKeys[row.RowIndex].Value.ToString();
}
else
this.TextBox1.Text = "222222222222 ";
}
}
}
}
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
foreach (GridViewRow row in this.GridView1.Rows)
{
CheckBox checkbox = row.FindControl( "CheckBox1 ") as CheckBox;
if (checkbox.Checked)
{
if (string.IsNullOrEmpty(str))
{
str = row.Cells[0].Text;
}
else
{
str = str + ", " + row.Cells[0].Text;
}
}
}
this.TextBox1.Text = str;
}
刚刚测试了一下可以的,LZ再看看
运行后的结果 this.TextBox1.Text= 222222222222
也就是说 cbox.Checked 始终=false
为什么我明明提交的时候在所对应的checkbox里面打了勾
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataKeyNames = new string[] { "id " };
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cbox = (CheckBox)row.FindControl( "CheckBox1 ");
if (cbox.Checked == true)
{
this.TextBox1.Text = GridView1.DataKeys[row.RowIndex].Value.ToString();
}
else
this.TextBox1.Text = "222222222222 ";
}
}
}
}
------解决方案--------------------
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Empty;
foreach (GridViewRow row in this.GridView1.Rows)
{
CheckBox checkbox = row.FindControl( "CheckBox1 ") as CheckBox;
if (checkbox.Checked)
{
if (string.IsNullOrEmpty(str))
{
str = row.Cells[0].Text;
}
else
{
str = str + ", " + row.Cells[0].Text;
}
}
}
this.TextBox1.Text = str;
}
刚刚测试了一下可以的,LZ再看看