我创建了一个网格视图,可以在页脚上动态获取按钮。但是当我点击1按钮时每行需要我在页脚上有2个按钮。按钮1单击新行添加。
但是新行需要另一行有按钮的行。
<asp:GridView ID="grvCharacter" runat="server" ShowFooter="True" AutoGenerateColumns="False" OnRowCommand="grvCharacter_RowCommand"
CellPadding="1" ForeColor="#333333" GridLines="None" HorizontalAlign="Center" OnRowDeleting="grvCharacter_RowDeleting"
Width="70%" Style="text-align: left">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Type" />
<asp:TemplateField HeaderText="Person Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" MaxLength="50" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:LinkButton Style="align-content:center" runat="server" ID="lnkView" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="VIEW" >Add</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie">
<ItemTemplate>
<asp:TextBox ID="txtMovie" runat="server" MaxLength="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtMovie"
ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAddSeq" runat="server" ToolTip="Add movie prequel or sequel" Text="Add Sequel/Prequel" OnClick="ButtonAdd_Click" />
<asp:Button ID="ButtonAddReb" runat="server" ToolTip="Add another movie with same charecter" Text="Add Reboot" OnClick="ButtonAddReb_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
答案 0 :(得分:1)
您可以在GridView中的页脚模板中添加一个用于添加新行的按钮。根据您的要求,这可能不是确切的解决方案,但应该在您的方向上给您一个想法。你可以尝试这样的事情:
<asp:TemplateField HeaderText="">
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
接下来,您可以处理此按钮的OnClick
事件,以便在每次单击此按钮时添加新行。这将保持gridview中数据的状态,并在底部添加一个新行,同时保持页脚模板不变。您可以在代码隐藏文件中尝试这样的事情:
用于设置GridView的初始行和标题:
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);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
每次单击页脚按钮时添加新行:
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtProduct");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
rowIndex++;
}
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
在添加新行后,在GridView中设置以前的数据:
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtProduct");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
rowIndex++;
}
}
}
}