web組件設(shè)計(jì),利用接口(IPostBackDataHandler)產(chǎn)生數(shù)據(jù)回傳的問題
2024-07-21 02:24:14
供稿:網(wǎng)友
1.組件源文件 iposttest.cs
1using system;
2using system.web.ui;
3using system.web.ui.webcontrols;
4using system.componentmodel;
5
6namespace mywebcontrol
7{
8 /// <summary>
9 /// iposttest 的摘要說明。
10 /// </summary>
11 [defaultproperty("text"),
12 toolboxdata("<{0}:iposttest runat=server></{0}:iposttest>")]
13 public class iposttest : system.web.ui.webcontrols.webcontrol,ipostbackdatahandler
14 {
15 private string user;
16 private string pwd;
17
18 // 控件名稱設(shè)置
19 private string username
20 {
21 get
22 {
23 return this.uniqueid + ":user";
24 }
25 }
26
27 private string pwdname
28 {
29 get
30 {
31 return this.uniqueid + ":pwd";
32 }
33 }
34
35 // 公共屬性
36 public string uservalue
37 {
38 get
39 {
40 return user;
41 }
42 }
43
44 public string pwdvalue
45 {
46 get
47 {
48 return pwd;
49 }
50 }
51
52 /// <summary>
53 /// 將此控件呈現(xiàn)給指定的輸出參數(shù)。
54 /// </summary>
55 /// <param name="output"> 要寫出到的 html 編寫器 </param>
56 protected override void render(htmltextwriter output)
57 {
58 //output.addattribute(htmltextwriterattribute.name,this.uniqueid);
59 output.renderbegintag(htmltextwritertag.table);
60 output.renderbegintag(htmltextwritertag.tr);
61 output.renderbegintag(htmltextwritertag.td);
62
63 output.addattribute(htmltextwriterattribute.name,username);
64 output.addattribute(htmltextwriterattribute.value,((user == null)?string.empty:user));
65 output.addattribute(htmltextwriterattribute.type,"text");
66 output.renderbegintag(htmltextwritertag.input);
67 output.renderendtag();//input
68
69 output.addattribute(htmltextwriterattribute.name,this.uniqueid);
70 output.addattribute(htmltextwriterattribute.type,"hidden");
71 output.addattribute(htmltextwriterattribute.value,"dd");
72 output.renderbegintag(htmltextwritertag.input);
73 output.renderendtag();//input hidden
74
75 output.renderbegintag(htmltextwritertag.br);
76 output.renderendtag();//br
77
78 output.addattribute(htmltextwriterattribute.name,pwdname);
79 output.addattribute(htmltextwriterattribute.value,((pwd == null)?string.empty:pwd));
80 output.addattribute(htmltextwriterattribute.type,"text");
81 output.renderbegintag(htmltextwritertag.input);
82 output.renderendtag();//input
83
84 output.renderendtag();//td
85 output.renderendtag();//tr
86 output.renderendtag();//table
87 }
88 // ipostbackdatahandler 成員
89
90 void ipostbackdatahandler.raisepostdatachangedevent()
91 {
92 // todo: 添加 iposttest.raisepostdatachangedevent 實(shí)現(xiàn)
93 }
94
95 bool ipostbackdatahandler.loadpostdata(string postdatakey, system.collections.specialized.namevaluecollection values)
96 {
97 // todo: 添加 iposttest.loadpostdata 實(shí)現(xiàn)
98 user = values[username];
99 pwd = values[pwdname];
100 return false;
101 }
102
103 }
104}
105
2.測試方法 將組件編譯后添加入自定義控件工具欄,拖入到測試頁面,增加一button按鈕控件,3.得出結(jié)論 當(dāng)組件內(nèi)部存在多個(gè)input子控件時(shí),必須有一個(gè)子控件的 name 為 this.uniqueid,這樣才能取得其他子控件的值回傳。 a.當(dāng)我試著將this.uniqueid加載到table上時(shí),卻取不到其子控件input的值。 b.上面的類型為hidden的input,可以取消,但必須將this.uniqueid加載到另外的任意一個(gè)控件上,否則取不到其值。 我這么加一個(gè)hidden類型的input,純粹只是為了對應(yīng)而已,沒什么其他目的。 c.我實(shí)在對web控件了解不多,一步步學(xué),不知道大家有沒有遇到過我這樣的問題。希望高手能夠指點(diǎn)一下感激不盡!