Html部分:
<asp:Repeater ID="rptsubject" runat="server" OnItemDataBound="rptsubject_ItemDataBound" OnItemCommand="rptsubject_ItemCommand" >
<HeaderTemplate>
<table id="tableinfo" border="1px" style="border-collapse: collapse;" bordercolor="#6898be" cellpadding="2px" cellspacing="0">
<tr style="height:22px; background-color:#bee4fb; text-align:center;">
<th style="width:65px; font-weight:normal;">序号</th>
<th style="width:300px; font-weight:normal;">科目名称</th>
<th style="width:80px; font-weight:normal;">修改</th>
<th style="width:80px; font-weight:normal;">删除</th>
</tr>
<tbody id="trId">
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr style="height:17px; background-color:White; text-align:center;">
<td><asp:Label ID="lblNum" runat="server" Text="Label"></asp:Label></td>
<td><%# Eval("SubjectName")%></td>
<td><asp:ImageButton ID="lbtEdit" ImageUrl="../images/bm_update.gif" CommandName="Edit" CommandArgument='<%# Eval("SubjectId") %>' runat="server" /></td>
<td><asp:ImageButton ID="lbtDelete" ImageUrl="../images/bm_delete.gif" CommandName="Delete" CommandArgument='<%# Eval("SubjectId") %>' runat="server" /></td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr style="height:17px; background-color:White; text-align:center;">
<td style="background-color:#fff;"><asp:Label ID="lblNum2" runat="server" Text="Label"></asp:Label></td>
<td style="background-color:#fff;"><asp:TextBox ID="txtA" MaxLength="20" Text='<%# Eval("SubjectName")%>' runat="server" Width="50px"></asp:TextBox></td>
<td style="background-color:#fff;"><asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# Eval("SubjectId") %>' CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# Eval("SubjectId") %>' CommandName="Cancel" Text="取消"></asp:LinkButton></td>
<td style="background-color:#fff;"></td>
</tr>
</asp:Panel>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr style="height:17px; text-align:center;">
<td><asp:Label ID="lblNum" runat="server" Text="Label"></asp:Label></td>
<td><%# Eval("SubjectName")%></td>
<td><asp:ImageButton ID="lbtEdit" ImageUrl="../images/bm_update.gif" CommandName="Edit" CommandArgument='<%# Eval("SubjectId") %>' runat="server" /></td>
<td><asp:ImageButton ID="lbtDelete" ImageUrl="../images/bm_delete.gif" CommandName="Delete" CommandArgument='<%# Eval("SubjectId") %>' runat="server" /></td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr style="height:17px; background-color:White; text-align:center;">
<td style="background-color:#fff;"><asp:Label ID="lblNum2" runat="server" Text="Label"></asp:Label></td>
<td style="background-color:#fff;"><asp:TextBox ID="txtA" MaxLength="20" Text='<%# Eval("SubjectName")%>' runat="server" Width="50px"></asp:TextBox></td>
<td style="background-color:#fff;"><asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# Eval("SubjectId") %>' CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# Eval("SubjectId") %>' CommandName="Cancel" Text="取消"></asp:LinkButton></td>
<td style="background-color:#fff;"></td>
</tr>
</asp:Panel>
</AlternatingItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
C#部分:
protected void rptsubject_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("lblNum")).Text = Convert.ToString(e.Item.ItemIndex + 1);
((Label)e.Item.FindControl("lblNum2")).Text = Convert.ToString(e.Item.ItemIndex + 1);
((ImageButton)e.Item.FindControl("lbtDelete")).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗')");
if (e.Item.ItemIndex.ToString() != CurrentId.ToString())
{
((Panel)e.Item.FindControl("plItem")).Visible = true;
((Panel)e.Item.FindControl("plEdit")).Visible = false;
}
else
{
((Panel)e.Item.FindControl("plItem")).Visible = false;
((Panel)e.Item.FindControl("plEdit")).Visible = true;
}
}
}
protected void rptsubject_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
CurrentId = Convert.ToInt32(e.Item.ItemIndex.ToString());
}
else if (e.CommandName == "Cancel")
{
CurrentId = -1;
}
else if (e.CommandName == "Update")
{
Panel Pn = (Panel)e.Item.FindControl("plEdit");
TextBox SubjectName = (TextBox)Pn.FindControl("txtA");
string subject = "";
if (SubjectName != null)
{
subject =HttpUtility.HtmlEncode(SubjectName.Text.Trim().ToString());
}
if (subject == "")
{
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, GetType(), "mes", "alert('请输入科目名称!')", true);
return;
}
string sql = "update Subject set SubjectName=@subject where SubjectId=" + e.CommandArgument.ToString();
SqlParameter[] sp ={ new SqlParameter("@subject", SqlDbType.NVarChar) };
sp[0].Value = subject;
new BasicInfo().DeleteDBItem(sql,sp);
DataBindToTable();
}
else if (e.CommandName == "Delete")
{
int del = Convert.ToInt32(e.CommandArgument.ToString());
string sql = "delete from Subject where SubjectId=" + del;
new BasicInfo().DeleteDBItem(sql);
DataBindToTable();
}
DataBindToTable();
}