2016-11-05 43 views
-2

我试过这段代码,但无法完成任务,请帮忙!我想找到控制并添加动态控制页面派生自母版页

public void data() 
     { 
      string connectionstring = ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString; 
      using (MySqlConnection con = new MySqlConnection(connectionstring)) 
      { 

       con.Open(); 

       MySqlCommand cmd = new MySqlCommand("select * from movie;", con); 

       MySqlDataReader dr = cmd.ExecuteReader(); 

       if (dr.HasRows) 
       { 

        Control abc = this.FindControl("divv"); 

        while (dr.Read()) 
        { 
         if (abc != null) 
         { 
          Label lbl = new Label(); 

          abc.Controls.Add(lbl); 

          lbl.Text = dr["mv_title"].ToString(); 
          //abc.Controls.Add(new LiteralControl("<br />")); 
         } 
        } 

       } 
      } 
     } 
+0

哪里是代码做? –

+0

这里是代码 – user6698813

+0

更改您的查询从电影 –

回答

0

试试此代码并通知error.I将使用datareader评论答案also.Sorry为有趣的评论。

SqlDataAdapter sda = new SqlDataAdapter(cmd); 
DataSet ds = new DataSet(); 
sda.Fill(ds); 
Control abc = this.FindControl("divv"); 
Label lbl = new Label(); 
abc.Controls.Add(lbl); 
lbl.Text = ds.Tables[0].Rows[0]["mv_title"].ToString(); 
con.close(); 
+0

这里是代码 – user6698813

+0

什么是错误来了? –

+0

的数据不会显示 – user6698813

0

我尝试以下代码

Control content = Page.Master.FindControl("ContentPlaceHolder1"); 

Control abc = content.FindControl("divv");