简体   繁体   中英

How to get items of a repeater row and column into an array or list in asp.net

i have a repeater bounded with records from from a source. the content of the repeater looks like this

  Science       5             67

  Art           3             57

at the end of the day, i want to get the result of this repeater like this {Science, Art}{5,3}{67,57} so i can save into the database. If you have any other way i can get to save that table in the database for a particular Student, Kindly suggest.

<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
  <HeaderTemplate></HeaderTemplate>
  <ItemTemplate>
    <tr>
      <td class="ctg-type">
        <strong> <%#Eval("SubjectName") %> </strong> <span><strong> <%#Eval("Unit") %></strong></span>
      </td>
      <td class="cgt-des">
        <%# Eval("Score") %>
      </td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

protected void Page_Load(object sender, EventArgs e)
{
  repeater.DataSource = getSudentRecord();
  repeater.DataBind();
}

public DataTable getStudentRecord()
{
  using (SqlCommand command = new SqlCommand("StudentRecord", con))
  {
    command.CommandType = CommandType.StoredProcedure;
    con.Open();
    command.ExecuteNonQuery();
    var data = new SqlDataAdapter(command);
    var Table = new DataTable();
    data.Fill(Table);
    con.Close();
    return Table;
  }
}

This approach will parse the DataTable twice:

        DataTable foo = getSudentRecord();

        string serializedFormat = "{{{0}}},{{{1}}},{{{2}}}";
        List<string> subjectnames = new List<string>();
        List<string> units = new List<string>();
        List<string> scores = new List<string>();
        foreach (DataRow row in foo.Rows)
        {
            subjectnames.Add(row["SubjectName"].ToString());
            units.Add(row["Unit"].ToString());
            scores.Add(row["Score"].ToString());
        }

        string serialized = String.Format(serializedFormat,
            String.Join(",",subjectnames.ToArray()),
            String.Join(",", units.ToArray()),
            String.Join(",", scores.ToArray())
            );


        //finally fill the repeater
        repeater.DataSource = foo;
        repeater.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM