【问题标题】:DataBind Repeater to List<Student> Fails to Update Data - PostBackDataBind Repeater 到 List<Student> 无法更新数据 - 回发
【发布时间】:2012-09-30 18:19:53
【问题描述】:

我正在尝试创建一个非常简单的 ASP.NET 页面,该页面允许用户输入学生数据。提交表单时,学生对象列表会更新,Repeater(数据绑定到列表)会反映新数据。用户应该能够继续添加新学生。

我的无法执行。我不知道为什么。我已经尝试过多次更改回发和数据绑定方法。

   /*
     * Student class representing a real-life student.
     */
    public class Student
    {

        /* Override default constructor */
        public Student(string first, string last, string studentid, string program, string option)
        {
            FName = first;
            LName = last;
            STID = studentid;
            Program = program;
            Option = option;
        }

        /* Property for the student's first name */
        public string FName
        {
            set; get;
        }

        /* Property for the student's last name */
        public string LName 
        {
            set; get;         
        }

        /* Property for the student ID */
        public string STID
        {
            set; get;
        }

        /* Property for the program of study */
        public string Program
        {
            set; get;
        }

        /* Property for the option within the program of study */
        public string Option
        {
            set; get;
        }

    }

    /* Class for the web form UI */
    public partial class _Default : System.Web.UI.Page
    {

        /* List of students to be displayed in the repeater control */
        private List<Student> myStudents;

        protected void Page_Load(object sender, EventArgs e)
        {

            myStudents = new List<Student>();



            /* Check postback value when the page loads - this is the first time */
            if (IsPostBack == false)
            {
                /* Bind the Collection to the Repeater control */
                Label1.Text = "" + myStudents.Count;
                Repeater1.DataSource = myStudents;
                Repeater1.DataBind();
            }


        }

        /* 
         * Submit button clicked to submit the form.
         */
        protected void Button2_Click(object sender, EventArgs e)
        {

            /* The forum has passed all of the validation rules for this case and we can add a new student to the list */
             if(Page.IsValid) {

                /*if its valid then create a new student object to put into the list of students
                  get the data from POST*/
                 myStudents.Add(new Student(FNameTextBox.Text, LNameTextBox.Text, SIDTextBox.Text, POSDropDownList.SelectedItem.Text, POListBox.SelectedItem.Text));

                 Label1.Text = "" + myStudents.Count;


              }

        }
    }

这是中继器的代码:

<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
         <table border="1">
            <tr>
               <td><b>First Name</b></td>
               <td><b>Last Name</b></td>
               <td><b>Student ID</b></td>
               <td><b>Program</b></td>
               <td><b>Option</b></td>
            </tr>
      </HeaderTemplate>

      <ItemTemplate>
         <tr>
            <td> <%# DataBinder.Eval(Container.DataItem, "FName")%> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "LName") %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "STID")%> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Program") %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Option") %> </td>
         </tr>
      </ItemTemplate>

      <AlternatingItemTemplate>
        <tr bgcolor="#e8e8e8">
          <td> <%# DataBinder.Eval(Container.DataItem, "FName")%> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "LName") %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "STID")%> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Program") %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "Option") %> </td>
        </tr>
       </AlternatingItemTemplate>

      <SeparatorTemplate>
        <tr>
        <td colspan="5"><hr /></td>
        </tr>
      </SeparatorTemplate>

      <FooterTemplate>
         </table>
      </FooterTemplate>
</asp:Repeater>

【问题讨论】:

    标签: asp.net list data-binding postback repeater


    【解决方案1】:

    DataBind 在您将新学生添加到数据源之前完成。

    添加

    Repeater1.DataBind();
    

    在您的 Click 事件中。

    【讨论】:

    • 更新 myStudents 后绑定中继器。您不必在每次点击时都这样做。仅当您对数据进行更改时。
    • 我试图这样做,但它似乎每次都会覆盖数据。我不确定发生了什么事。可能是我没有正确启用一些 VS 设置。
    【解决方案2】:

    我已经重构了你的代码,现在它应该可以工作了

    /* Class for the web form UI */
    public partial class _Default : System.Web.UI.Page
    {
    
        /* List of students to be displayed in the repeater control */
        private List<Student> myStudents = new List<Student>();
    
        protected void Page_Load(object sender, EventArgs e){
    
            /* Check postback value when the page loads - this is the first time */
            if (IsPostBack == false){
              this.bindRepeater();
            }
        }
    
     private void bindRepeater(){
      /* Bind the Collection to the Repeater control */
                Repeater1.DataSource = myStudents;
                Repeater1.DataBind();
                Label1.Text = "" + myStudents.Count;
                }
    
        /* 
         * Submit button clicked to submit the form.
         */
        protected void Button2_Click(object sender, EventArgs e)
        {
    
            /* The forum has passed all of the validation rules for this case and we can add a new student to the list */
             if(Page.IsValid) {
    
                /*if its valid then create a new student object to put into the list of students
                  get the data from POST*/
                 myStudents.Add(new Student(FNameTextBox.Text, LNameTextBox.Text, SIDTextBox.Text, POSDropDownList.SelectedItem.Text, POListBox.SelectedItem.Text));
                 this.bindRepeater();
    
    
              }
    
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我使用了重构后的代码,现在它将显示我输入的学生数据。但是,每次我尝试将新学生输入列表时,它似乎都会被覆盖!也许有一些设置我没有正确启用?
    • 是不是因为每次页面加载时我们都在实例化一个新的List,而之前的list丢失了?
    • 我试图在不同的时间点实例化列表(在页面数据和 postBack 但它仍然失败)......哦,我很困惑......
    • 我现在对在哪里实例化 List 感到很困惑,因为每次我按下提交按钮时都会调用 Page_Load 函数!有初始化函数吗?
    • 这可能有助于更好地理解页面生命周期。 msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx 可能会对这个话题有更多的了解。
    【解决方案3】:

    我明白了为什么我们不能保留这些价值观。我们需要利用会话。在我们进行任何数据绑定之前,需要声明一个会话。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多