Telerik Radlistview不显示InsertItemTemplate

时间:2014-07-01 16:36:09

标签: c# listview telerik

我正在尝试创建一个使用列表的radlistview来获取它的数据源。我正在尝试在此列表视图中编写CRUD功能。无论我做什么,我都无法通过某些奇怪的原因来展示InsertItemTemplate。

form1.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="ISEMAILAPP.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        <Scripts>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
            </asp:ScriptReference>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
            </asp:ScriptReference>
            <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
            </asp:ScriptReference>
        </Scripts>
    </telerik:RadScriptManager>
    <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" >


        <telerik:RadListView ID="messagelist" ItemPlaceholderID="itemPlaceholder" runat="server" onitemcommand="messagelist_ItemCommand" InsertItemPosition="LastItem">
          <LayoutTemplate>
                <table id="Table1" runat="server">
                    <tr id="Tr1" runat="server">
                    <th>&nbsp;</th>
                    <th>Start</th>
                    <th>End</th>
                    <th>Subject</th>
                    <th>Message</th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder"></tr>
                </table>
            </LayoutTemplate>
            <EmptyDataTemplate>
                            <table id="Table1" runat="server">
                    <tr id="Tr1" runat="server">
                    <th>&nbsp;</th>
                    <th>emptyStart</th>
                    <th>End</th>
                    <th>Subject</th>
                    <th>Message</th>
                    </tr>
                    <tr runat="server" id="itemPlaceholder"></tr>
                </table>
            </EmptyDataTemplate>

            <ItemTemplate>

                <tr>
                <td><asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" AutoPostback="true" ></asp:Button> &nbsp;<telerik:RadButton ID="deletebutton" runat="server" Text="Delete"
         AutoPostBack="true"  CommandName="Delete" >     </telerik:RadButton></td>
                    <td>
                        <asp:Label runat="server" ID="startdate" Text='<%# Eval("Start") %>' />

                    </td>
                    <td>
                        <asp:Label runat="server" ID="enddate" Text='<%# Eval("End") %>' />
                    </td>
                    <td>
                        <asp:Label runat="server" ID="subjectline" Text='<%# Eval("Subject") %>' />
                    </td>
                                        <td>
                        <asp:Label runat="server" ID="messageline" Text='<%# Eval("Message") %>' />
                    </td>
                </tr>


            </ItemTemplate>

            <EditItemTemplate>

                            <tr style="background-color:Gray;">
                            <td><telerik:RadButton ID="savebutton" runat="server" Text="Save"
         AutoPostBack="true"  CommandName="Update" >     </telerik:RadButton>  
         <asp:Button ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"
                            Width="70px" /></td>
                    <td>   

                        <telerik:RadDateTimePicker ID="startdate" runat="server" SelectedDate='<%# Bind("Start") %>' ></telerik:RadDateTimePicker>

                    </td>
                    <td>
                        <telerik:RadDateTimePicker ID="enddate" runat="server" SelectedDate='<%# Bind("End") %>' ></telerik:RadDateTimePicker>
                    </td>
                    <td>
                        <asp:TextBox ID="subjectline" runat="server" Text='<%# Bind("Subject") %>'></asp:TextBox>
                    </td>
                                        <td>
                        <telerik:RadEditor  Content='<%# Bind("Message") %>' ID="messageline" runat="server" ></telerik:RadEditor>
                    </td>
                </tr>

            </EditItemTemplate>

            <InsertItemTemplate>
            <tr >
                            <td><telerik:RadButton ID="insertbutton" runat="server" Text="Add New"
         AutoPostBack="true"  CommandName="Insert" >     </telerik:RadButton>  
         <asp:Button ID="BtnCancel" runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="false"
                            Width="70px" /></td>
                    <td>   

                        <telerik:RadDateTimePicker ID="startdate" runat="server"  ></telerik:RadDateTimePicker>

                    </td>
                    <td>
                        <telerik:RadDateTimePicker ID="enddate" runat="server"  ></telerik:RadDateTimePicker>
                    </td>
                    <td>
                        <asp:TextBox ID="subjectline" runat="server" ></asp:TextBox>
                    </td>
                                        <td>
                        <telerik:RadEditor  ID="messageline" runat="server" ></telerik:RadEditor>
                    </td>
                </tr>

            </InsertItemTemplate>

        </telerik:RadListView>



        </telerik:RadAjaxPanel>








    </form>
</body>
</html>

form1.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;


namespace ISEMAILAPP
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        string messageKey = "messages";
        protected void Page_Load(object sender, EventArgs e)
        {
            messagelist.InsertItemPosition = RadListViewInsertItemPosition.LastItem;
            if (!Page.IsPostBack)
            {
                // If the session variable is empty, initialize an 
                // empty list as the datasource
                if (Session[messageKey] == null)
                {
                    Session[messageKey] = new List<mEvent>();


                }
                 foreach (RadListViewDataItem item in messagelist.Items)
                {
                    item.Edit = true;
                }
                 BindList();

            }

        }

        private void BindList()
        {
            messagelist.DataSource = null;
            messagelist.DataSource = (List<mEvent>)Session[messageKey];
            messagelist.InsertItemPosition = RadListViewInsertItemPosition.LastItem;
            messagelist.DataBind();
        }


        protected void Button1_Click(object sender, EventArgs e)
        {/*
            DateTime start = this.start.SelectedDate.Value;
            DateTime end = this.end.SelectedDate.Value;
            string s = subject.Text;
            string m = message.Content;
            // Grab the current list from the session and add the 
            // currently selected DropDown item to it.
            List<mEvent> messages = (List<mEvent>)Session[messageKey];
            messages.Add(new mEvent(start,end,s,m));
            subject.Text = string.Empty;
            message.Content = string.Empty;
            this.start.Clear();
            this.end.Clear();
           BindList();
          * */

        }





        private void UpdateBindList()
        {
           // messagelist.DataSource = null;
            messagelist.DataSource = (List<mEvent>)Session[messageKey];
            messagelist.InsertItemPosition = RadListViewInsertItemPosition.FirstItem;
            messagelist.Rebind();
        }
        // Basic author object, used for databinding
        private class mEvent
        {
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
            public String Subject { get; set; }
            public String Message { get; set; }
            public mEvent(DateTime start, DateTime end, string subject, string message)
            {
                Start = start;
                End = end;
                Subject = subject;
                Message = message;
            }
        }
        private string PopulateBody(string AppliesTo, string MtypeColor, string MtypeText, string Message)
        {
            var subjects = new List<string>();
            var messages = new List<string>();
            string body = string.Empty;
            string dateString = DateTime.Today.ToShortDateString(); 
            foreach (RadListViewDataItem item in messagelist.Items)
            {
                string currSubject = string.Empty;
                string currMessage = string.Empty;

                Label subject = (Label)item.FindControl("subjectline");
                Label message = (Label)item.FindControl("messageline");

                currSubject = subject.Text;
                currMessage = message.Text;
                subjects.Add(currSubject);
                messages.Add(currMessage);
            }

            using (StreamReader reader = new StreamReader(Server.MapPath("~/emailtemplate.htm")))
            {
                body = reader.ReadToEnd();
            }
            body = body.Replace("{CurrentDate}", dateString);
            //body = body.Replace("{AppliesTo}", AppliesTo);
           // body = body.Replace("{MtypeColor}", MtypeColor);
            //body = body.Replace("{MtypeText}", MtypeText);
            body = body.Replace("{Message}", Message);
            return body;
        }
        private void SendHtmlFormattedEmail(string recepientEmail, string subject, string body)
        {
            using (MailMessage mailMessage = new MailMessage())
            {
                mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["UserName"]);
                mailMessage.Subject = subject;
                mailMessage.Body = body;
                mailMessage.IsBodyHtml = true;
                mailMessage.To.Add(new MailAddress(recepientEmail));
                SmtpClient smtp = new SmtpClient();
                smtp.Host = ConfigurationManager.AppSettings["Host"];
                // smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                // NetworkCred.UserName = ConfigurationManager.AppSettings["UserName"];
                //NetworkCred.Password = ConfigurationManager.AppSettings["Password"];
                smtp.UseDefaultCredentials = true;
                //smtp.Credentials = NetworkCred;
                smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                smtp.Send(mailMessage);
            }
        }


        protected void SendEmail(object sender, EventArgs e)
        {
            /* string AppliesTo = TextBox2.Text;
             string Message = RadEditor1.Content;
             string MtypeColor = "#008AB0";
             string MtypeText = "General Broadcast";
             string Subject = TextBox1.Text;

             string body = this.PopulateBody(AppliesTo,MtypeColor, MtypeText, Message);
             this.SendHtmlFormattedEmail("zzz@zzz.org", Subject, body);
             * */
        }




        protected void messagelist_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.EditCommandName)
            {
                BindList();
                //messagelist.InsertItemPosition = RadListViewInsertItemPosition.None;
            }
            else   if (e.CommandName == RadListView.CancelCommandName)            
            {               
                //cancel action
            }
            else if (e.CommandName == RadListView.UpdateCommandName)
            {               
                //Perform the update
                RadListViewEditableItem curItem = (RadListViewEditableItem)e.ListViewItem;
                int curItemIndex = curItem.DisplayIndex;
                RadDateTimePicker dtpStart = (RadDateTimePicker)curItem.FindControl("startdate");
                RadDateTimePicker dtpEnd = (RadDateTimePicker)curItem.FindControl("enddate");
                TextBox txtBoxSubject = (TextBox)curItem.FindControl("subjectline");
                RadEditor reMessage = (RadEditor)curItem.FindControl("messageline");

                DateTime start = dtpStart.SelectedDate.Value;
                DateTime end = dtpEnd.SelectedDate.Value;
                string s = txtBoxSubject.Text;
                string m = reMessage.Content;

                // Grab the current list from the session and add the 
                // currently selected DropDown item to it.
                 List<mEvent> messages = (List<mEvent>)Session[messageKey];

                messages[curItemIndex] = new mEvent(start, end, s, m);
               // messages.Add(new mEvent(start, end, s, m));
                //  subject.Text = string.Empty;
                //  message.Content = string.Empty;
                // this.start.Clear();
                //    this.end.Clear();
                UpdateBindList();

            }
            else if (e.CommandName == RadListView.PerformInsertCommandName)
            {               
                //Perform the insert  
                RadListViewEditableItem curItem = (RadListViewEditableItem)e.ListViewItem;
                int curItemIndex = curItem.DisplayIndex;
                RadDateTimePicker dtpStart = (RadDateTimePicker)curItem.FindControl("startdate");
                RadDateTimePicker dtpEnd = (RadDateTimePicker)curItem.FindControl("enddate");
                TextBox txtBoxSubject = (TextBox)curItem.FindControl("subjectline");
                RadEditor reMessage = (RadEditor)curItem.FindControl("messageline");

                DateTime start = dtpStart.SelectedDate.Value;
                DateTime end = dtpEnd.SelectedDate.Value;
                string s = txtBoxSubject.Text;
                string m = reMessage.Content;

                // Grab the current list from the session and add the 
                // currently selected DropDown item to it.
                List<mEvent> messages = (List<mEvent>)Session[messageKey];

                messages[curItemIndex] = new mEvent(start, end, s, m);
                // messages.Add(new mEvent(start, end, s, m));
                //  subject.Text = string.Empty;
                //  message.Content = string.Empty;
                // this.start.Clear();
                //    this.end.Clear();

                messagelist.DataSource = null;
                messagelist.DataSource = (List<mEvent>)Session[messageKey];
                messagelist.DataBind();
            }
            else if (e.CommandName == RadListView.DeleteCommandName)
            {               
                //Perform the Delete
                RadListViewEditableItem curItem = (RadListViewEditableItem)e.ListViewItem;
                int curItemIndex = curItem.DisplayIndex;


                // Grab the current list from the session and add the 
                // currently selected DropDown item to it.
                List<mEvent> messages = (List<mEvent>)Session[messageKey];

                messages.RemoveAt(curItemIndex);
                // messages.Add(new mEvent(start, end, s, m));
                //  subject.Text = string.Empty;
                //  message.Content = string.Empty;
                // this.start.Clear();
                //    this.end.Clear();

                messagelist.DataSource = null;
                messagelist.DataSource = (List<mEvent>)Session[messageKey];
                messagelist.DataBind();
            } 
        }




    }
}

0 个答案:

没有答案