c#根据查询在Webform上动态创建表并添加文本框而不是值

时间:2019-02-19 16:05:33

标签: c# sql asp.net sql-server dynamic

我对C#非常陌生。我决定开发一个系统,使用C#中的Webforms记录下注条目,以进行年度竞赛。希望这是正确学习C#的最好方法。

我可以基于一个简单的查询在Web表单上动态创建表格,该查询返回主队和客队的得分以及其他辅助数据。但是,为了使用相同或相似的查询,我需要创建相同的表,该表将插入文本框来替换得分,该得分将由下注者对每个游戏得分的预测填充。游戏数量有限。

欢迎任何有用的建议。讽刺和贬义的言论不值得赞赏,因为我想大家都可以赞赏。如果需要,我可以PM SQL查询,以根据需要快速构建数据库。

到目前为止,这是我的代码:

entry_form.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="entry_form.aspx.cs" Inherits="Six_Nations_Championship.ShowData" %>



  <!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">

  <head id="Head2" runat="server">
    <title>Six Nations Sweep - Entry Form</title>
    <link href="StyleSheet.css" rel="stylesheet" />
  </head>

  <body>
    <form id="form2" runat="server">
      <div id="main_div">
        <div id="banner">
        </div>
        <div id="entry_form" class="form-style-9">
          <div id="Div4">
            <table>
              <tr>
                <td>
                  <b class="field-style field-sptdt atdgn-left">First Name</b>
                </td>
                <td>
                  <asp:TextBox ID="FName" CssClass="textbox" runat="server" BackColor="White"></asp:TextBox>
                </td>
              </tr>
              <tr>
                <td>
                  <b class="field-style field-sptdt atdgn-left">Last Name</b>
                </td>
                <td>
                  <asp:TextBox ID="LName" CssClass="textbox" runat="server" BackColor="White"></asp:TextBox>
                </td>
              </tr>
              <tr>
                <td>
                  <b class="field-style field-sptdt atdgn-left">Mobile Number</b>
                </td>
                <td>
                  <asp:TextBox ID="Mobile" CssClass="textbox" runat="server" BackColor="White"></asp:TextBox>
                </td>
              </tr>
              <tr>
                <td>
                  <b class="field-style field-sptdt atdgn-left">Email Address</b>
                </td>
                <td>
                  <asp:TextBox ID="Email" CssClass="textbox" runat="server" BackColor="White"></asp:TextBox>
                </td>
              </tr>
            </table>
          </div>
          <table class="results">
            <tr>
              <td>
                <asp:PlaceHolder ID="DBDataPlaceHolder" runat="server"></asp:PlaceHolder>
              </td>
            </tr>
          </table>
        </div>
      </div>
    </form>
  </body>

  </html>

entry_form.aspx.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;

namespace Six_Nations_Championship
{
            public partial class ShowData : System.Web.UI.Page
        {
            SqlDataAdapter da;
            DataSet ds = new DataSet();
            StringBuilder htmlTable = new StringBuilder();

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                    BindData();
            }

            private void BindData()
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = @"Data Source=SEAN_LAPTOP-HP;Initial Catalog=Six_Nations;Integrated Security=True";
                SqlCommand cmd = new SqlCommand("SELECT match_schedule.gameid AS Game, Home_Teams.country_name AS Home, match_schedule.home_country_score AS HomeScore,  match_schedule.away_country_score AS AwayScore, Away_Teams.country_name AS Away, match_schedule.venue, match_schedule.gametime AS Date FROM Away_Teams INNER JOIN match_schedule ON Away_Teams.away_teamid = match_schedule.away_teamid INNER JOIN Home_Teams ON match_schedule.home_teamid = Home_Teams.home_teamid", con);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();

                htmlTable.Append("<table class='results'>");
                htmlTable.Append("<tr><th>Game ID</th><th>First Name</th><th>Last Name</th><th>Home</th><th>Score</th><th>Score</th><th>Away</th></tr>");

                if (!object.Equals(ds.Tables[0], null))
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {

                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            htmlTable.Append("<tr style='color: White;'>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Game"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Home"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["HomeScore"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["AwayScore"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Away"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Venue"] + "</td>");
                            htmlTable.Append("<td>" + ds.Tables[0].Rows[i]["Date"] + "</td>");
                            htmlTable.Append("</tr>");
                        }
                        htmlTable.Append("</table>");
                        DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
                    }
                    else
                    {
                        htmlTable.Append("<tr>");
                        htmlTable.Append("<td align='center' colspan='4'>There is no Record.</td>");
                        htmlTable.Append("</tr>");
                    }
                }
            }
            }
}

0 个答案:

没有答案