RadioButtonList做選票例子

先添加一個類作爲靜態數據連接方法:
public  static   SqlConnection  creatconnection()
  {
   System.Data.SqlClient.SqlConnection   con =new System.Data.SqlClient.SqlConnection("server=(local);database=test;uid=sa;pwd=;");
     return con;
 
  }


.cs文件:
private void Page_Load(object sender, System.EventArgs e)
  {
   if (!Page.IsPostBack)
   {
   
    SqlConnection con=db.creatconnection();
    con.Open();//打開數據庫
    SqlCommand cmd =new SqlCommand("select * from vote ",con);
    this.RadioButtonList1.DataTextField="voatitem";//幫定表的名字以選擇他的數據
    SqlDataReader str=cmd.ExecuteReader();//讀取數據庫數據
    
    this.RadioButtonList1.DataSource=str;
    this.RadioButtonList1.DataBind();
   }
  
  
   
   // 在此處放置用戶代碼以初始化頁面
  }

  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   for (  int i=0 ; i<=this.RadioButtonList1.Items.Count-1;i++)/

   {

  
    if ( this.RadioButtonList1.Items[i].Selected)
    {
     
     Label2.Text="您選擇了"+"<br> "+this.RadioButtonList1.Items[i].Text+"<br>"+"感謝您對本站的支持!";
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章