我在gridview中有一个复选框,我想在勾选复选框时在文本框中显示行数据。这是我的buttun代码:
protected void ButtonModifEnfant_Click(object sender, EventArgs e)
{
#region
try
{
int nbr_check = 0;
string myid = "";
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
CheckBox check = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
if (check.Checked)
{
myid = ((HiddenField)gv_enfant.Rows[i].Cells[0].FindControl("codeenfant")).Value;
//ButtonModifEnfant.Enabled = true;
//ButtonSuppEnfant.Enabled = true;
//myid = gv_enfant.Rows[i].Cells[0].Text;
//nbr_check++;
}
else
{
//ButtonModifEnfant.Enabled = false;
//ButtonSuppEnfant.Enabled = true;
}
}
if (nbr_check >= 1)
{
//ButtonModifEnfant.Enabled = true;
Response.Write("<script>alert ('Error!')</script>");
return;
}
else if (nbr_check == 1)
{
//ButtonModifEnfant.Enabled = true;
}
ModalPopupExtenderModifierEnfant.Show();
c.cmd = c.cn.CreateCommand();
c.cmd.CommandText = "select prenom , DateNaissance , Scolarise , Activite from Enfants where codeEnfants = " + myid;
if (c.cn.State == ConnectionState.Closed)
{
c.cn.Open();
}
SqlDataReader read = c.cmd.ExecuteReader();
if (read.HasRows)
{
read.Read();
TextBox_NPmodif.Text = read[0].ToString();
TextBox_DNmodif.Text = read[1].ToString();
TextBox_Scolarisemodif.Text = read[2].ToString();
TextBox_Activitemodif.Text = read[3].ToString();
}
read.Close();
//gv_enfant.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (c.cn.State == ConnectionState.Open)
{
c.cn.Close();
}
}
#endregion
}
这是我的复选框代码:
protected void CheckBoxEnfants_CheckedChanged(object sender, EventArgs e)
{
int chek = 0;
for (int i = 0; i < gv_enfant.Rows.Count; i++)
{
CheckBox chbox = (CheckBox)gv_enfant.Rows[i].Cells[0].FindControl("CheckBoxenfant");
if (chbox.Checked)
{
chek++;
}
}
if (chek != 1)
{
ButtonModifEnfant.Enabled = false;
ButtonSuppEnfant.Enabled = false;
}
else
{
ButtonModifEnfant.Enabled = true;
ButtonSuppEnfant.Enabled = true;
}
}
和gridview的html代码:
<asp:GridView ID="gv_enfant" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1"
Width="533px">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxenfant" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxEnfants_CheckedChanged" />
<asp:HiddenField ID="codeenfant" runat="server" Value='<%# Eval("codeEnfants") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="prenom" HeaderText="prenom" SortExpression="prenom" />
<asp:BoundField DataField="DateNaissance" HeaderText="Date Naissance" DataFormatString="{0:d}"
SortExpression="DateNaissance" />
<asp:BoundField DataField="Scolarise" HeaderText="Scolarise" SortExpression="Scolarise" />
<asp:BoundField DataField="Activite" HeaderText="Activite" SortExpression="Activite" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CVtechConnectionString10 %>"
SelectCommand="SELECT * FROM [Enfants] WHERE ([ppr] = @ppr)">
<SelectParameters>
<asp:SessionParameter Name="ppr" SessionField="code" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
谢谢
答案 0 :(得分:0)
这可能会对你有帮助。
if (check.Checked)
{
txtBox.Text=gv_enfant["yourColumnName",i].Value.ToString();
}
答案 1 :(得分:0)
试试吧,
<强>前端强>
<asp:CheckBox ID="CheckBoxenfant" runat="server" index="<%# Container.DataItemIndex %>" AutoPostBack="true" OnCheckedChanged="CheckBoxEnfants_CheckedChanged" />
<asp:TextBox ID="textbx" runat="server" Enabled="false" Text='<%# Eval("codeEnfants") %>' />
<强>代码隐藏强>
protected void CheckBoxEnfants_CheckedChanged(object s, EventArgs e)
{
byte index = byte.Parse((s as CheckBox).Attributes["index"]);
(gv_enfant.Rows[index].FindControl("textbx") as TextBox).Enabled = (gv_enfant.Rows[index].FindControl("CheckBoxenfant") as CheckBox).Checked;
}