一个itemtemplate中的多个控件 - GridView

时间:2016-01-31 13:35:34

标签: c# gridview

我在网络应用程序中有一个gridview,它在页面加载事件的每一列中显示文本框。现在我想要做的是在这个itemtemplate中添加另一个标签控件。显示当我在文本框中键入内容并单击“保存”时,我可以向我的数据库显示标签而不是文本框。我得到了保存部分工作正常,但不知道如何显示我在标签中输入的文本。我现在拥有的是点击保存按钮后,文本框保留在gridview中并且标签不显示。知道如何解决这个问题吗?

<asp:GridView ID='gvMain' ruant="server">
   <Columns>
      <asp:TemplateField HeaderText ="LastName">
         <ItemTemplate>
            <asp:TextBox ID="txtFName" runat="server"/>
            <asp:Label ID="lblFName" ruant="server" />
   </Columns>
</asp:GridView>

请参阅此链接以供参考。

http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx

1 个答案:

答案 0 :(得分:1)

我会将两个控件添加到单元格项目模板中,并根据我是保存行还是添加新行来隐藏/显示它们。所以,我的代码如下:

标记:

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
                <asp:TemplateField HeaderText="Header 1">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Column1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Header 2">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Column2") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Header 3">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("Column3") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterStyle HorizontalAlign="Right" />
                    <FooterTemplate>
                        <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                            OnClick="ButtonAdd_Click" />
                        <asp:Button ID="ButtonSave" runat="server" Text="Save"
                            OnClick="ButtonSave_Click" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
</form>

代码隐藏:

private void SetInitialRow() {
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dt.Rows.Add(dr);

    Table = dt;

    BindGrid();
    SwitchMode(false);
}

private void AddNewRowToGrid() {
    if(Table != null) {
        DataRow row = Table.NewRow();
        Table.Rows.Add(row);
        BindGrid();
        SwitchMode(false);
    }
    else {
        Response.Write("ViewState is null");
    }
}

private void SaveRow() {
    if(Table != null) {
        int rowIndex = Table.Rows.Count - 1;
        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
        TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
        TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
        Table.Rows[rowIndex]["Column1"] = box1.Text;
        Table.Rows[rowIndex]["Column2"] = box2.Text;
        Table.Rows[rowIndex]["Column3"] = box3.Text;
        BindGrid();
        SwitchMode(true);
    }
    else {
        Response.Write("ViewState is null");
    }
}

private void SwitchMode(bool add) {
    Button saveBtn = (Button)Gridview1.FooterRow.Cells[3].FindControl("ButtonSave");
    saveBtn.Visible = !add;
    Button addBtn = (Button)Gridview1.FooterRow.Cells[3].FindControl("ButtonAdd");
    addBtn.Visible = add;
    SwitchControl(add);
}

private void SwitchControl(bool add) {
    for(int i = 0; i < Table.Rows.Count; i++) {
        bool txtVisible = false;
        if (i == Table.Rows.Count - 1) {
            txtVisible = !add;
        }
        TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
        box1.Visible = txtVisible;
        TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
        box2.Visible = txtVisible;
        TextBox box3 = (TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox3");
        box3.Visible = txtVisible;
        Label label1 = (Label)Gridview1.Rows[i].Cells[1].FindControl("Label1");
        label1.Visible = !txtVisible;
        Label label2 = (Label)Gridview1.Rows[i].Cells[2].FindControl("Label2");
        label2.Visible = !txtVisible;
        Label label3 = (Label)Gridview1.Rows[i].Cells[3].FindControl("Label3");
        label3.Visible = !txtVisible;
    }
}

private DataTable Table {
    get {
        return ViewState["CurrentTable"] as DataTable;
    }
    set {
        ViewState["CurrentTable"] = value;
    }
}

private void BindGrid() {
    Gridview1.DataSource = Table;
    Gridview1.DataBind();
}

protected void Page_Load(object sender, EventArgs e) {
    if(!Page.IsPostBack) {
        SetInitialRow();
    }
}

protected void ButtonAdd_Click(object sender, EventArgs e) {
    AddNewRowToGrid();
}

protected void ButtonSave_Click(object sender, EventArgs e) {
    SaveRow();
}

所以,首先我看到Grid有一行,我可以通过TextBoxes用数据填充它,然后单击Save。然后,TextBoxes变为Labels,Add New Row可见。如果单击它,将出现带有TextBoxes的新行。