repeater 的编辑功能

很简单的实现.纪录下吧.编辑和编辑的时候对数据的检测.

 aspx:

function showerror(errordiv,errorstr)
{
document.getElementById(errordiv).innerText=errorstr;
}

function checkerror2(tb1,tb2,tb3)
{
var o1=document.getElementById(tb1);
var o2=document.getElementById(tb2);
var o3=document.getElementById(tb3);
var error="";
if(o1.value=="")error+="·discount can't empty! ";
if(o2.value=="")error+="·amountfrom can't empty! ";
if(o3.value=="")error+="·amountto can't empty! ";
if(error=="")return true;
else
{
showerror("error",error);
return false;
}
}
</script>

<asp:Repeater ID="rpUserGroupList" runat="server"  OnItemCommand="itemcmd" OnItemDataBound="rpdb">
            <ItemTemplate>
            <asp:Panel ID="plview" runat="server">
            <tr onmouseover="this.style.backgroundColor='#DDEAFC'" onmouseout="this.style.backgroundColor=''">
               <td style="20px"><asp:ImageButton ID="ImageButtonedit" runat="server" ImageUrl="../icon/edt.gif" CommandArgument='<%#Eval("dc_id")%>' CommandName="edit" ToolTip="Edit"/></td><td style="20px"><asp:ImageButton ID="ImageButtondel" runat="server" ImageUrl="../icon/del.gif" CommandArgument='<%#Eval("dc_id")%>' CommandName="del" OnClientClick="return confirm('delete?')" ToolTip="Delete"/></td>
                    <td><%#Eval("dc_discount")%>%</td><td><%#Eval("dc_amoutfrom")%></td><td><%#Eval("dc_amoutto")%></td></tr>
             </asp:Panel>
             <asp:Panel ID="pledit" runat="server" Visible="false">
               <tr onmouseover="this.style.backgroundColor='#DDEAFC'" onmouseout="this.style.backgroundColor=''">
                <td style="20px">
                    <asp:ImageButton ID="ImageButtonsave" runat="server" ImageUrl="../icon/save.gif" CommandArgument='<%#Eval("dc_id")%>' CommandName="save" Width="16px" Height="16px"  ToolTip="Save"/></td><td style="20px"><asp:ImageButton ID="ImageButtoncancel" runat="server" ImageUrl="../icon/scancel.gif" CommandArgument='<%#Eval("dc_id")%>' CommandName="cancel" ToolTip="Cancel"/></td>
                    <td><cc1:LBTextBox ID="tbdiscount2" runat="server" TBTYPE="onlyint" MaxLength="2" Width="48px" Text='<%#Eval("dc_discount")%>'></cc1:LBTextBox><img src="../icon/del.gif"  id="codeerror2" style="display:none" runat="server"/></td>
                    <td><cc1:LBTextBox ID="tbfrom2" runat="server" TBTYPE="onlyint" Width="48px" Text='<%#Eval("dc_amoutfrom")%>'></cc1:LBTextBox></td>
                    <td><cc1:LBTextBox ID="tbto2" runat="server" TBTYPE="onlyint" Width="48px" Text='<%#Eval("dc_amoutto")%>'></cc1:LBTextBox></td>
            </tr>
            </asp:Panel>
            </ItemTemplate>
            </asp:Repeater>

 cs:

protected void itemcmd(object sender, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string cmd = e.CommandName;
                int index = int.Parse(e.CommandArgument.ToString().Trim());

                switch (cmd)
                {
                    case "del":
                        {
                            lanvin.appcode.webhelp.checkaccess(me.gID, "ms_discount_del", me.isAdmin);

                            BLL.Tb_Discount blldis = new BLL.Tb_Discount();
                            blldis.Delete(index);
                            Function.JScript.AlertAndRedirect("success", "discount.aspx");
                            break;
                        }
                    case "edit":
                        {

                            lanvin.appcode.webhelp.checkaccess(me.gID, "ms_discount_update", me.isAdmin);
                            e.Item.FindControl("plview").Visible = false;
                            e.Item.FindControl("pledit").Visible = true;
                            break;
                        }
                    case "cancel":
                        {
                            e.Item.FindControl("plview").Visible = true;
                            e.Item.FindControl("pledit").Visible = false;
                            PageChange(this.LBPager1, new LBControl.LBPager.pagerarg(LBPager1.CurPage));
                            break;
                        }
                    case "save":
                        {

                            lanvin.appcode.webhelp.checkaccess(me.gID, "ms_discount_add", me.isAdmin);
                            try
                            {
                                BLL.Tb_Discount blldis = new BLL.Tb_Discount();
                                Model.Tb_Discount discount = blldis.GetModel(index);
                                string sdiscount = ((LBControl.LBTextBox)e.Item.FindControl("tbdiscount2")).Text;
                                string from = ((LBControl.LBTextBox)e.Item.FindControl("tbfrom2")).Text;
                                string to = ((LBControl.LBTextBox)e.Item.FindControl("tbto2")).Text;
                                discount.dc_AmoutFrom = int.Parse(from);
                                discount.dc_AmoutTo = int.Parse(to);
                                discount.dc_Discount = int.Parse(sdiscount);
                                blldis.Update(discount);
                                Function.JScript.AlertAndRedirect("success", "discount.aspx");
                            }
                            catch
                            {
                                Function.JScript.AlertAndRedirect("other error!", "discount.aspx");
                            }
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }

protected void rpdb(object sender, RepeaterItemEventArgs e)
        {
            LBControl.LBTextBox tbdis = (LBControl.LBTextBox)e.Item.FindControl("tbdiscount2");
            LBControl.LBTextBox tbform = (LBControl.LBTextBox)e.Item.FindControl("tbfrom2");
            LBControl.LBTextBox tbto = (LBControl.LBTextBox)e.Item.FindControl("tbto2");

            ImageButton saveimg = (ImageButton)e.Item.FindControl("ImageButtonsave");
            saveimg.Attributes.Add("OnClick", "return checkerror2('" + tbdis.ClientID + "','" + tbform.ClientID + "','" + tbto.ClientID + "')");
        }

原文地址:https://www.cnblogs.com/lsfv/p/1674150.html