⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-15-03

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 15-03: The code-behind page supporting the HTML

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace ProgrammingCSharpWeb
{
   // page constructor
   public class WebForm1 : System.Web.UI.Page
   {
      // declare the controls
      protected 
         System.Web.UI.WebControls.RadioButtonList 
            rbl1;
      private 
         System.Data.SqlClient.SqlConnection 
            myConnection;
      private System.Data.DataSet myDataSet;
      private 
         System.Data.SqlClient.SqlCommand myCommand;
      protected System.Web.UI.WebControls.Label Label1;
      protected System.Web.UI.WebControls.TextBox txtName;
      protected System.Web.UI.WebControls.Button Order;
      protected System.Web.UI.WebControls.Button Cancel;
      protected System.Web.UI.WebControls.Label lblFeedBack;
      private   System.Data.SqlClient.SqlDataAdapter 
            dataAdapter;
   
      public WebForm1()
      {
         Page.Init += 
            new System.EventHandler(Page_Init);
      }

      protected void Page_Load(object sender, EventArgs e)
      {
         // the first time we load the page, get the data and
         // set the radio buttons
         if (!IsPostBack)
         {
            string connectionString = 
               "server=localhost; uid=sa; pwd=oWenmEany; database=northwind";         
            myConnection = 
               new  System.Data.SqlClient.SqlConnection(connectionString);
            myConnection.Open();

            // create the dataset and set a property
            myDataSet = new System.Data.DataSet();
            myDataSet.CaseSensitive=true;

            // create the SqlCommand  object and assign the
            // connection and the select statement
            myCommand = new System.Data.SqlClient.SqlCommand();
            myCommand.Connection=myConnection;
            myCommand.CommandText = 
               "Select ShipperID, CompanyName from Shippers";

            // create the dataAdapter object and pass in the
            // Sql Command object and establish the table mappings
            dataAdapter = new System.Data.SqlClient.SqlDataAdapter();
            dataAdapter.SelectCommand= myCommand;
            dataAdapter.TableMappings.Add("Table","Shippers");

            // Tell the dataAdapter object to fill the dataset
            dataAdapter.Fill(myDataSet);

            // set up the properties for the RadioButtonList
            rbl1.RepeatLayout = 
               System.Web.UI.WebControls.RepeatLayout.Flow;
            rbl1.DataTextField = "CompanyName";
            rbl1.DataValueField = "ShipperID";

            // set the data source and bind to it
            rbl1.DataSource = myDataSet.Tables["Shippers"].DefaultView;
            rbl1.DataBind();

            // select the first button
            rbl1.Items[0].Selected = true;

         }
      }

      protected void Page_Init(object sender, EventArgs e)
      {
         InitializeComponent();
      }

      private void InitializeComponent()
      {
         this.Order.Click += 
            new System.EventHandler(this.Order_Click);
         this.Load += 
            new System.EventHandler(this.Page_Load);
      }

      // handle clicking the order button
      public void Order_Click (
         object sender, System.EventArgs e)
      {
         // create the message by getting
         // the values from the controls
         string msg;
         msg = "Thank you " + txtName.Text +". You chose " ;
         // iterate over the radio buttons
         for (int i = 0;i<rbl1.Items.Count;i++)
         {
            // if it is selected, add it to the msg.
            if (rbl1.Items[i].Selected)
            {
               msg = msg + rbl1.Items[i].Text;
               lblFeedBack.Text = msg;
            }  // end if selected
         }     // end for loop
      }        // end Order_Click
   }           // end class WebForm1
}              // end namespace ProgrammingCSharpWeb

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -