【问题标题】:Showing an editable textbox in my GridView在我的 GridView 中显示可编辑的文本框
【发布时间】:2014-02-26 16:53:38
【问题描述】:

我正在显示一个 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 的可编辑按钮的情况下执行此操作。

【问题讨论】:

  • 你在 onCheckChanged 上做了什么

标签: c# asp.net visual-studio gridview


【解决方案1】:

下一个代码应该可以工作:

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 标记添加处理程序(和自动回发)

<asp:TemplateField HeaderText="Passed">
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"></asp:CheckBox>
    </ItemTemplate>
</asp:TemplateField>

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2010-11-29
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多