关闭

读取数据库图片

标签: 数据库buttondatasetobjectstring
904人阅读 评论(1) 收藏 举报
分类:

还是给段代码你自己看看,免得有人说我刻薄啊,
自动生成按钮,自动添加事件:

protected System.Web.UI.HtmlControls.HtmlTable tb;

HtmlTableRow trCar = new HtmlTableRow();    
   HtmlTableCell tdCar = new HtmlTableCell();
   DataTable dtCompanyName=ExecuteSqlReadDs("select  distinct id,CompanyName from insure_param");

   //全部品牌
   Button btnAll = new Button();
   btnAll.Text = "全部公司";
   btnAll.ID  = "btnAll";
   btnAll.CommandArgument = "";    
   btnAll.Width   = 170;
   //btnAll.CssClass = "button";
                            //自动添加事件
   btnAll.Click += new System.EventHandler(this.btn_Click);
   tdCar.Controls.Add(btnAll);
   Label lblSpaceAll = new Label();
   lblSpaceAll.Text = "/t";
   tdCar.Controls.Add(lblSpaceAll);
    
   //
   for ( int i=0;i<dtCompanyName.Rows.Count;i++)
   { 
    Button btn = new Button();
    btn.Text = dtCompanyName.Rows[i]["CompanyName"].ToString();
    btn.ID  = dtCompanyName.Rows[i]["id"].ToString();
    btn.CommandArgument = dtCompanyName.Rows[i]["id"].ToString();    
    btn.Width   = 170;
    //btn.CssClass = "button1";
    btn.Click += new System.EventHandler(this.btn_Click);
     
    tdCar.Controls.Add(btn);
    Label lblSpace = new Label();
    lblSpace.Text = "/t";
    tdCar.Controls.Add(lblSpace);
   }
   trCar.Controls.Add(tdCar);
   tb.Controls.Add(trCar);


事件为:
private void btn_Click(object sender, System.EventArgs e)
  {
   if(((Button)sender).CommandArgument=="")
    Response.Redirect("Default.aspx?pgid=insurelist");
   else
   Response.Redirect("Default.aspx?pgid=insurelist&ordercompany=" + ((Button)sender).CommandArgument);
  }


//用到的
public DataTable ExecuteSqlReadDs(string SqlString)     
  {
   SqlCommand SqlCmd = new SqlCommand();
   SqlCmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
   SqlCmd.CommandText = SqlString;
   SqlCmd.CommandType = CommandType.Text;
   SqlCmd.Connection.Open();
   //return Convert.ToInt32(SqlCmd.ExecuteScalar());
      SqlDataAdapter SqlAd = new SqlDataAdapter(SqlCmd);
      DataSet Rs = new DataSet();
      SqlAd.Fill(Rs);
      return Rs.Tables[0];
  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:122275次
    • 积分:1771
    • 等级:
    • 排名:千里之外
    • 原创:48篇
    • 转载:56篇
    • 译文:0篇
    • 评论:6条
    最新评论