如何在按钮单击时将文本框和复选框中的记录插入到gridview中?

时间:2015-09-29 02:09:43

标签: c# asp.net

我正在尝试从图像按钮上的文本框和复选框中插入记录单击到gridview。每次点击,我都希望将记录插入到gridview中,并且添加的旧记录应该保留,除非我从gridview中删除它。帮助将不胜感激。这是我在GUI中的代码。谢谢!

<tr>
                <td align="center" class="tblborder">
                    <strong>Team Member(s)</strong>
                </td>
                <td align="center" style="width:23%" class="tblborder">
                    <asp:TextBox ID="txtCTName" runat="server" Width="80%"></asp:TextBox>
                    <asp:imagebutton ID="btnCTName" name="btnChangeTeam" class="buttonsearch" runat="server" Height="16px" ImageUrl="~/Images/search.jpg" Width="16px" />
                </td>
                <td align="center" style="width:27%" class="tblborder">
                    <asp:TextBox ID="txtCTPosition" runat="server" Width="100%"></asp:TextBox>
                </td>
                <td align="center" style="width:10%" class="tblborder">
                    <asp:CheckBox ID="chkCTSignOff" Runat="server"/>
                </td>
                <td align="center" style="width:20%" class="tblborder">
                    <asp:Label ID="lblCTDate" runat="server"></asp:Label>
                </td>
                <td align="center" style="width:8%" class="tblborder">
                    <asp:ImageButton ID="imgAddCT" runat="server" ImageUrl="~/Images/addsign.png"
                        Height="20px" Width="20px" onclick="imgAddCT_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <asp:GridView ID="gvChangeTeam" runat="server" Width="100%" AutoGenerateColumns="False" Visible="False">
                        <Columns>
                            <asp:BoundField HeaderText="Name" DataField="TeamName" ItemStyle-Width="30%" />
                            <asp:BoundField HeaderText="Position" DataField="TeamPosition" ItemStyle-Width="20%"/>
                            <asp:BoundField HeaderText="Sign Off" DataField="TeamSignature" ItemStyle-Width="10%"/>
                            <asp:BoundField HeaderText="Date" DataField="TeamDate" ItemStyle-Width="20%"/>
                            <asp:buttonfield ButtonType="Image" ImageUrl="../Images/deletesign.png" commandname="ibtnDelete" HeaderText="Delete" ItemStyle-Width="5%" ControlStyle-Height="20" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>

1 个答案:

答案 0 :(得分:0)

您无法在回发时检索控件的数据源(本例中为GridView)。所以选项是使用视图状态,会话等基本上在回发期间保持原始数据。这些也称为ASP.NET状态管理技术。

我故意避免在这里使用ViewState,因为如果你的原始数据集或数据表很大,它会杀死页面。即使使用Session,也会出现问题,因为它会在默认会话超时值或您在配置中设置的任何值后被清除。

虽然这些技术中的每一种都有局限性,但我只是向您展示如何使用Session来完成。

ASPX

<form id="form1" runat="server">
    <div>
        <label>Username</label>
        <asp:TextBox runat="server" ID="txt1" />
        <asp:Button Text="Add" runat="server" OnClick="AddUsername" />
        <asp:GridView runat="server" ID="gv1">
            <Columns>
                <asp:BoundField DataField="Username" HeaderText="Username" />
            </Columns>
        </asp:GridView>
    </div>
</form>

代码隐藏

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var dt = GetInitialData();

        gv1.DataSource = dt;
        gv1.DataBind();

        Session["dt"] = dt;
    }
}

protected void AddUsername(object sender, EventArgs e)
{
    var dt = Session["dt"] as DataTable;

    var row = dt.NewRow();
    row["Username"] = txt1.Text;
    dt.Rows.Add(row);

    gv1.DataSource = dt;
    gv1.DataBind();

    Session["dt"] = dt;
}

private DataTable GetInitialData()
{
    var dt = new DataTable();
    dt.Columns.Add("Username");

    for (int i = 1; i <= 5; i++)
    {
        var row = dt.NewRow();
        row["Username"] = string.Format("user name {0}", i);
        dt.Rows.Add(row);
    }

    return dt;
}