Note that there are some explanatory texts on larger screens.

plurals
  1. PODataBind Repeater to List<Student> Fails to Update Data - PostBack
    primarykey
    data
    text
    <p>I am trying to create a very simple ASP.NET page that allows a user to enter student data. When the form is submitted, the List of student objects is updated and the Repeater (which is databound to the list) reflects the new data. The user should be able to keep adding new students. </p> <p>Mine fails to perform. I have no idea why. I have tried changing the postback and databinding methods many times.</p> <pre><code> /* * 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&lt;Student&gt; myStudents; protected void Page_Load(object sender, EventArgs e) { myStudents = new List&lt;Student&gt;(); /* 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; } } } </code></pre> <p>Here is the code for the Repeater:</p> <p></p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server"&gt; &lt;HeaderTemplate&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;First Name&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Last Name&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Student ID&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Program&lt;/b&gt;&lt;/td&gt; &lt;td&gt;&lt;b&gt;Option&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "FName")%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "LName") %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "STID")%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Program") %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Option") %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;AlternatingItemTemplate&gt; &lt;tr bgcolor="#e8e8e8"&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "FName")%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "LName") %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "STID")%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Program") %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Option") %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/AlternatingItemTemplate&gt; &lt;SeparatorTemplate&gt; &lt;tr&gt; &lt;td colspan="5"&gt;&lt;hr /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/SeparatorTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload