简体   繁体   中英

basic c#, asp.net looping and css style

What do i try to achieve:

Trying to get the following result roughly

[day number] [FixtureTeamHome] v [FixtureTeamAway]

[month name] [Shortdescription], [Venue]

On the Page_load I retrieve the Team Details page which has all the fixtures involved First of all is this the correct way, secondly i got the result needed but how would i add a style this as currently s += "

  • is not being recognized.

    thank you for your advice, help and directions

     public partial class WebForm6 : System.Web.UI.Page { public string TeamTitle = null; public string TeamTitle1 = null; protected void Page_Load(object sender, EventArgs e) { DataView TeamRecordSet = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); foreach (DataRowView DRV in TeamRecordSet) { lblTeamName.Text = DRV["TeamName"].ToString(); lblTeamNameShortInfo.Text = DRV["TeamShortInfo"].ToString(); Image1.ImageUrl = "~/style/images/teams/" + DRV["TeamPhoto"].ToString(); } DataView TeamFixtureRecordSet = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty); string s = "<ul class=\\"latest-posts\\" id=\\"TeamFIxtures\\">"; foreach (DataRowView DRV1 in TeamFixtureRecordSet) { int FixtureMonth = (int)DRV1["FixtureMonth"]; string strMonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(FixtureMonth); s += "<li><span class=\\"date\\"><em class=\\"day\\">"; s += "</em><em class=\\"month\\">" + strMonthName + "</em></span>"; // Fixture ================================================================ // Home Team int FixtureTeamHomeID = (int)DRV1["FixtureTeamHomeID"]; TeamNameHome(FixtureTeamHomeID); s += "<a href=\\"#\\">" + TeamTitle + "</a>"; TeamTitle = null; s += " v "; // Away Team int FixtureTeamAwayID = (int)DRV1["FixtureTeamAwayID"]; TeamNameAway(FixtureTeamAwayID); s += "<a href=\\"#\\">" + TeamTitle1 + "</a><br />"; TeamTitle1 = null; // Venue s += "<a href=\\"#\\">" + DRV1["FixtureShortInfo"].ToString() + "</a><br /><br />"; s += "<a href=\\"#\\">" + DRV1["VenueID"].ToString() + "</a></li>"; } s += "</ul>"; lblTeamFIxtures.Text = s; } public void TeamNameHome(int arg1) { using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["WCDB_local"].ConnectionString)) { string sqlSelect = @"select TeamName from Teams where TeamID = @TeamID"; using (SqlCommand cmd = new SqlCommand(sqlSelect, conn)) { cmd.Parameters.AddWithValue("@TeamID", arg1); conn.Open(); TeamTitle = cmd.ExecuteScalar().ToString(); } } } public void TeamNameAway(int arg2) { Int32 myTeamID = arg2; using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["WCDB_local"].ConnectionString)) { string sqlSelect = @"select TeamName from Teams where TeamID = @TeamID"; using (SqlCommand cmd = new SqlCommand(sqlSelect, conn)) { cmd.Parameters.AddWithValue("@TeamID", arg2); conn.Open(); TeamTitle1 = cmd.ExecuteScalar().ToString(); } } } 

    I think i might have sussed it, and slowly beginning to understand the logic behind it , could i have an opinion if this is the correct way to approach this ? thanks again guys, every little advice helps....

    ASPX FILE:

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="repeater.aspx.cs" Inherits="WorldCup.repeater" %> <!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"> <div> <asp:Label ID="lblID" runat="server"></asp:Label><br /><br /> <asp:Repeater ID="FixtureRepeater" runat="server" DataSourceID="SqlDataSource1"> <ItemTemplate> <asp:HyperLink ID="lblTeamNameHome" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "FixtureTeamHomeID", "?tid={0}") %>'><%# Eval("TEAMHOME") %></asp:HyperLink> v <asp:HyperLink ID="lblTeamNameAway" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "FixtureTeamAwayID", "?tid={0}") %>'><%# Eval("TEAMAWAY") %></asp:HyperLink><br /> <asp:Label ID="lblShortdescription" runat="server"><%# Eval("FixtureShortInfo") %></asp:Label><br /> <asp:HyperLink ID="lblVenue" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "VenueID", "?tid={0}") %>'><%# Eval("VENUE")%></asp:HyperLink><br /><br /> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WCDB_local %>" SelectCommand="SELECT Fixtures.FixtureID, Fixtures.GroupID, Fixtures.VenueID, Fixtures.FixtureTime, Fixtures.FixtureDate, Fixtures.FixtureTeamHomeID, Fixtures.FixtureTeamAwayID, Fixtures.FixtureShortInfo, Fixtures.FixtureDescription, Fixtures.FixtureImage, Fixtures.FixtureResult, Teams_1.TeamName AS TEAMHOME, Teams_2.TeamName AS TEAMAWAY, Venues_1.VenueName AS VENUE FROM Fixtures INNER JOIN Teams AS Teams_1 ON Fixtures.FixtureTeamHomeID = Teams_1.TeamID INNER JOIN Teams AS Teams_2 ON Fixtures.FixtureTeamAwayID = Teams_2.TeamID INNER JOIN Venues AS Venues_1 ON Fixtures.VenueID = Venues_1.VenueID WHERE (Fixtures.FixtureTeamHomeID = @id) OR (Fixtures.FixtureTeamAwayID = @id)"> <SelectParameters> <asp:ControlParameter Name="id" Type="Int32" ControlID="lblID" /> </SelectParameters> </asp:SqlDataSource> </div> </form> 

    CS FILE

     using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Configuration; using System.Data.SqlClient; namespace WorldCup { public partial class repeater : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string v = Request.QueryString["tid"]; if (v != null) { lblID.Text = v.ToString(); } else { Int32 defaultvalue = 3; lblID.Text = defaultvalue.ToString(); } } } } 

    once again thanks for your help and advice, it really helps @ Samjongenelen, thanks for the links

  • Basically, you're using ASP classic techniques with some other not so pretty code.

    No flame, but this makes the application hard to debug :) So this is not an answer to your question but a lookup tip

    IMO a good place to start would be researching data binding / 'Live binding', which is basically creating the (design) skeleton in ASPNET and then binding a datasource to it. Easy to implement and maintain

    See http://www.codeproject.com/Articles/8659/Mastering-ASP-NET-DataBinding http://msdn.microsoft.com/en-us/magazine/ee819084.aspx

    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