asp.net添加自定义用户控件并传值

创建了一个自定义控件 singleControl.ascx

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Data;
8
9 public partial class Controls_singleControl : System.Web.UI.UserControl
10 {
11
12 private string ChapterName="";
13 private DataSet ds = null;
14
15 public string chaptername
16 {
17 get
18 {
19 return ChapterName;
20 }
21 set
22 {
23 ChapterName = value;
24
25 }
26
27 }
28
29 public DataSet DS
30 {
31 get
32 {
33 return ds;
34 }
35 set
36 {
37 ds = value;
38
40 }
41 }
42
43 protected void Page_Load(object sender, EventArgs e)
44 {
45
46 Label24.Text = ChapterName;
47 SPGridView.DataSource = ds;
48 SPGridView.DataBind();
49
50 }
51 }

创建一个传值页面 UserTest.aspx

页面代码必须将自定义控件加载进来:<%@ Register src="../Controls/singleControl.ascx" tagname="singleControl" tagprefix="uc1" %>

然后我放置了一个容器  <asp:Panel ID="Panel1" runat="server"></asp:Panel>
然后写代码(我使用了存储过程 只要看第31~34行代码即可):            
    

View Code
 1  private void lable()
2 {
3 //根据试卷ID获取课程编号
4 SqlParameter[] Params1 = new SqlParameter[1];
5 DataBase dbA = new DataBase();
6 Params1[0] = dbA.MakeInParam("@paperid", SqlDbType.Int, 4, HidPaperID.Value);
7 DataSet ds = dbA.GetDataSet("Proc_GetCourseID",Params1);
8 int CourseID = int.Parse(ds.Tables[0].Rows[0]["CourseID"].ToString());
9
10 //根据课程编号获取该课程的章节内容
11 SqlParameter[] Params3 = new SqlParameter[1];
12 DataBase dbB = new DataBase();
13 Params3[0] = dbB.MakeInParam("@courseid", SqlDbType.Int, 4, CourseID);
14 DataSet ds3 = dbB.GetDataSet("Proc_GetChapterNameByCourseID", Params3);
15
16 foreach (DataRow row in ds3.Tables[0].Rows)
17 {
18
19
20
21 SqlParameter[] Params5 = new SqlParameter[4];
22 DataBase dbC = new DataBase();
23 Params5[0] = dbC.MakeInParam("@userid", SqlDbType.VarChar, 50, HidUserID.Value);
24 Params5[1] = dbC.MakeInParam("@paperid", SqlDbType.Int, 4, HidPaperID.Value);
25 Params5[2] = dbC.MakeInParam("@ChapterID", SqlDbType.VarChar, 50, int.Parse(row["chapterid"].ToString()));
26 Params5[3] = dbC.MakeInParam("@CourseID", SqlDbType.Int, 4, CourseID);
27 DataSet dset = dbC.GetDataSet("Proc_SingleProblemExminationListByChapterID", Params5);
28
29 if (dset.Tables[0].Rows.Count > 0)
30 {
31 ASP.controls_singlecontrol_ascx uc = new ASP.controls_singlecontrol_ascx();//加载用户定义控件
32 uc.chaptername = row["chaptername"].ToString();
33 uc.DS = dset;
34 Panel1.Controls.Add(uc);
35 }
36
37 }
38
39 }


   


              
             

转载于:https://www.cnblogs.com/panan/archive/2011/11/30/2268575.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值