0

我在 ASPX 中有这个 for 循环

 for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            strFname += ds.Tables[0].Rows[i]["first_name"].ToString();
            strLname += ds.Tables[0].Rows[i]["last_name"].ToString();
            str = String.Format("{0} {1} ", strFname, strLname);


        }

我希望 Fname 将在 The Fname 旁边,并且在下一行中相同(直到最后一个)我希望在每一行中,名字将在姓氏旁边(在每一行中只有一个 Fname 和一个 Lname)和下一行同样的事情。我该怎么做?谢谢!

4

4 回答 4

1

Response.Write打印到 HTML 输出流。http://msdn.microsoft.com/en-us/library/ms525585(v=vs.90).aspx

Response.write(str);

您还可以使用简写符号进行输出

<%= str %>

或者

<%: str %>
于 2012-11-19T17:28:54.890 回答
1

使用Repeater控制更适合您的情况。在其标记中,您可以使用td在 LastName 旁边显示 FirstName 并tr连续显示它们。见例子

于 2012-11-19T17:30:50.267 回答
1

Since you did not display your full code, we are not sure how you are displaying this on the full page. You can use a Repeater, Gridview, Listview, or some custom display container.

Below is an example of how to do it with a ListView:

Default.aspx:

<asp:ListView ID="lvData" runat="server" ItemPlaceholderID="phItem" OnItemDataBound="lvData_ItemDataBound">
  <LayoutTemplate>
    <table>
      <thead>
        <tr>
          <th>Fullname</th>
        </tr>
      </thead>
      <tbody>
        <asp:PlaceHolder ID="phItem" runat="server" />
      </tbody>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr>
      <td><asp:Literal ID="litFullname" runat="server" /></td>
    </tr>
  </ItemTemplate>
</asp:ListView>

Default.aspx.cs

protected void lvData_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  //Get the data item that was passed in, in this case which number for this row.
  var data = (int)e.Item.DataItem;

  //Create temp first and last names
  var firstName = "First" + data.ToString();
  var lastName = "Last" + data.ToString();

  //Display it to the listview
  var litFullname = (Literal)e.Item.FindControl("litFullname");
  litFullname.Text = string.Format("{0} {1}", firstName, lastName);
}
于 2012-11-19T17:40:00.220 回答
0
str = String.Format("{0}&nbsp;{1}<br />", strFname, strLname);
于 2012-11-19T17:13:29.113 回答