首页 > 代码库 > Repeater动态添加行
Repeater动态添加行
<table class="table table-striped table-bordered table-hover">
<asp:Repeater ID="rp_List" runat="server" onitemcommand="rp_List_ItemCommand">
<HeaderTemplate>
<thead>
<tr>
<th>
ID
</th>
<th>
中文内容
</th>
<th>
英文内容
</th>
<th>
是否上传声音<%--(添加时<span class="text-danger">必填</span>)--%>
</th>
<th width="80px">
头像
</th>
<th width="80px">
对话排序
</th>
<th width="80px">
最大时间
</th>
<th width="80px">
声音时间
</th>
<th>
操作
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.ItemIndex+1 %>
<asp:HiddenField ID="hidid" runat="server" Value=http://www.mamicode.com/‘<%#Eval("id") %>‘ />
</td>
<td>
<asp:TextBox ID="txtCountentzh" runat="server" Text=‘<%#Eval("Contentzh") %>‘ class="form-control"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtCountenten" runat="server" Text=‘<%#Eval("Contenten") %>‘ class="form-control"></asp:TextBox>
</td>
<td>
<%--<asp:FileUpload ID="FileUpload" runat="server" CssClass="form-control" />--%>
<asp:Label ID="lblsound" runat="server" Text=‘<%#Eval("IsHave") %>‘></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPhoto" runat="server" class="form-control" Text=‘<%#Eval("photo") %>‘></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtOrder" runat="server" class="form-control" Text=‘<%#Eval("orderby") %>‘></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtMaxsecond" runat="server" class="form-control" Text=‘<%#Eval("maxsecond") %>‘></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtSoundSize" runat="server" class="form-control" Text=‘<%#Eval("soundsize") %>‘></asp:TextBox>
</td>
<td>
<%--<a href="http://www.mamicode.com/DuihuaDelete.aspx?id=<%#Eval("id") %>&page=<%=CurrentPage %>&pianzhangid=<%=pzid %>" class="btn btn-danger" onclick="return confirm(‘是否删除该数据‘)">删除</a>--%>
<%--<a href="http://www.mamicode.com/DuihuaEdit.aspx?id=<%#Eval("id") %>&pianzhangid=<%=pzid %>" class="btn btn-primary">编辑</a>
<a href="http://www.mamicode.com/DuihuaiDetail.aspx?id=<%#Eval("id") %>" class="btn btn-default btn-primary" title="详情">详情</a>--%>
<asp:LinkButton ID="lbtnDelete" runat="server" CssClass="btn btn-danger" OnClientClick=‘return confirm("确定删除?")‘ CommandName="del" CommandArgument=‘<%#Eval("id") %>‘>删除</asp:LinkButton>
<asp:LinkButton ID="lbtnEdit" runat="server" CssClass="btn btn-default" CommandName="edit" CommandArgument=‘<%#Eval("id") %>‘>编辑</asp:LinkButton>
</td>
</ItemTemplate>
<FooterTemplate>
<tr id="Tr1" runat="server" visible="<%#rp_List.Items.Count==0 %>">
<td colspan="9" class="indent15">
未找到符合条件的信息
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>
后台代码:
protected void btnAddNewRow_Click(object sender, EventArgs e)
{
//首先,恢复数据源
DataTable dt = DefineDataTableSchema(hfRptColumns.Value);
foreach (RepeaterItem item in rp_List.Items)
{
DataRow newRow = dt.NewRow();
newRow["id"] = ((HiddenField)item.FindControl("hidid")).Value;
newRow["Contentzh"] = ((TextBox)item.FindControl("txtCountentzh")).Text;
newRow["Contenten"] = ((TextBox)item.FindControl("txtCountenten")).Text;
newRow["photo"] = ((TextBox)item.FindControl("txtPhoto")).Text;
newRow["orderby"] = ((TextBox)item.FindControl("txtOrder")).Text;
newRow["maxsecond"] = ((TextBox)item.FindControl("txtMaxsecond")).Text;
newRow["IsHave"] = ((Label)item.FindControl("lblsound")).Text;
newRow["soundsize"] = ((TextBox)item.FindControl("txtSoundSize")).Text;
dt.Rows.Add(newRow);
}
//添加一行
DataRow row = dt.NewRow();
row["Contentzh"] = "";
row["Contenten"] = "";
row["photo"] = "";
row["orderby"] = "0";
row["maxsecond"] = "1";
row["IsHave"] = "未上传";
row["soundsize"] = "1";
dt.Rows.Add(row);
rp_List.DataSource = dt;
rp_List.DataBind();
}
/// <summary>
/// 根据repeater相对应的列名,定义数据源datatable的schema
/// </summary>
/// <param name="columns">列名</param>
/// <returns></returns>
public DataTable DefineDataTableSchema(string columns)
{
DataTable dt = new DataTable();
string[] columnsAry = columns.Split(‘,‘);
foreach (string str in columnsAry)
{
dt.Columns.Add(str);
}
return dt;
}