gridview中的复选框

时间:2013-11-05 17:32:30

标签: c# asp.net gridview

我有一个gridview,在gridview中有一个复选框...当在特定行中选中复选框时...该行的特定“金额”应除以2并且值shopuld将显示在金额文本框...我真的不知道如何做到这一点......有人可以帮助我吗?

<asp:gridview ID="Gridview2" runat="server" ShowFooter="true" 
              CssClass="vutblrow" TabIndex="3" HeaderStyle-CssClass="vutblhdr"
              CellPadding="4" ForeColor="#333333" GridLines="None"  
              Width="25%" PagerStyle-Mode="NumericPages" 
              AutoGenerateColumns="false" 
              onrowcreated="Gridview2_RowCreated" Height="16px">
    <PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />    
        <asp:TemplateField HeaderText="City To">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server" 
                             CssClass="txtBoxNormalmedium">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:TextBox ID="TextBox7" runat="server" 
                             CssClass="txtBoxNormalmedium">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="shared?">
            <ItemTemplate>
                <asp:CheckBox ID="chck7" runat="server" 
                              CssClass="txtBoxNormalmedium">
                </asp:CheckBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City From">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server" 
                             CssClass="txtBoxNormalmedium">
                </asp:TextBox>
            </ItemTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                            onclick="ButtonAdd_Click" 
                            CssClass="btnNormalAdd" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton2" runat="server" 
                                onclick="LinkButton2_Click" 
                                CssClass="lnkbut">
                    Remove
                </asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:gridview>

1 个答案:

答案 0 :(得分:0)

您需要将OnCheckChanged声明添加到网格视图的复选框TemplateField,如下所示:

<asp:TemplateField HeaderText="shared?">
    <ItemTemplate>
        <asp:CheckBox ID="chck7" runat="server" 
                      CssClass="txtBoxNormalmedium" 
                      OnCheckChanged="chck7_CheckChanged"> 
        </asp:CheckBox>
    </ItemTemplate>
</asp:TemplateField>

现在在您的代码隐藏中,您需要处理OnCheckChanged事件以确定是否已选中复选框并相应地更新金额文本框,如下所示:

protected void chk1_CheckedChanged(object sender, EventArgs e)
{
    // Attempt to cast the sender to CheckBox type
    CheckBox theCheckBox = sender as CheckBox;

    // Check to see if check box was found before we try to use it
    if(theCheckBox != null)
    {
        // Get the grid view row object
        GridViewRow theGridViewRow = theCheckBox.Parent.Parent as GridViewRow;

        // Check to see if grid view row was found before we try to use it
        if(theGridViewRow != null)
        {
            // Is the check box checked or not?
            if(theCheckBox.Checked)
            {
                // Yes, it is checked
                // Find amount text box and perform logic
                TextBox theAmountTextBox = theGridViewRow.FindControl("TextBox7") as TextBox;

                // Check to see if amount text box was found before we try to use it
                if(theAmountTextBox != null)
                {
                    // Do logic here to update amount text box value
                }
            }
            else
            {
                // No, it is not checked
                // Do something here if need be; otherwise get rid of else
            }
        }
    }
}