我正在显示一个Gridview,它会显示一个问题列表,其中包含一个复选框,用于传递或失败。我想要做的是当他们勾选失败的复选框时,下一栏中会出现一个文本框以说明原因。我的Gridview的代码如下
<asp:GridView ID="QuestionsGrid" runat="server" AutoGenerateColumns="False"
BorderStyle="None" class="gridView"
GridLines="None" ShowFooter="True" TotalRows="0"
Width="950px" CellPadding="5" CssClass="gridView"
EmptyDataText="No rows found."
style="margin-left: auto; margin-right: auto;"
OnRowDataBound="QuestionsGrid_RowDataBound" IgnoreFlagIdpsc=""
OnRowEditing="QuestionsGrid_RowEditing" onRowCommand="QuestionsGrid_RowCommand" >
<Columns>
<asp:BoundField ItemStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
</asp:BoundField>
<asp:BoundField ItemStyle-HorizontalAlign="Left" >
<ItemStyle HorizontalAlign="Left" Width="500px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Passed" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Value" >
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="100px" Font-Size="10pt"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我尝试使用CheckBox设置事件onCheckChanged,但这不起作用。我想这样做而不使用GridView的可编辑按钮。
答案 0 :(得分:0)
下一个代码应该有效:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
var checkbox = sender as CheckBox;
//version 1: show the text box on click
checkbox.Parent.FindControl("TextBox1").Visible = true;
//version 1: show the text box based on checkbox state
checkbox.Parent.FindControl("TextBox1").Visible = checkbox.Checked;
}
不要忘记使用
从aspx标记添加处理程序(和autopostback)<asp:TemplateField HeaderText="Passed">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="true" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>