利用ProfileBase,解决web aplication,profilecommon类不存在的问题。
2009-03-09 11:26 AnyKoro 阅读(1649) 评论(3) 收藏 举报在VS2008中,如果你新建的是web site的话,那么在你使用profile的时候,系统就会自动生成一个profileCommon类。但是,如果你使用的是web application的话,这个profileCommon类没有办法自动生成的,园子里面有人写过关于使用辅助工具生成web profile再改成profileCommon的方法。我这里解决这个问题主要是利用ProfileBase类,它是ProfileCommon类的基类。接下来我要做的就是在注册的同时,改变profile。下面就上程序了
1
<table id="outside" border="0">2

3
<tr>4

5
<td align="center" colspan="2">6

7
<span class="boldBlue">创建用户名和密码</span>8

9
</td>10

11
</tr>12

13
<tr>14

15
<td align="center" colspan="2">16

17
18

19
</td>20

21
</tr>22

23
<tr>24

25
<td align="right">26

27
<asp:Label ID="Label1" runat="server" AssociatedControlID="LocationUpdatePanel">所在地区</asp:Label></td>28

29
<td>30

31
<asp:UpdatePanel ID="LocationUpdatePanel" runat="server">32

33
<ContentTemplate>34

35
<asp:DropDownList ID="ProvinceDropDownList" runat="server" DataSourceID="ProvinceSqlDataSource"36

37
DataTextField="Province" DataValueField="ProvinceId" AutoPostBack="true">38

39
</asp:DropDownList>40

41
<asp:DropDownList ID="CityDropDownList" runat="server"42

43
DataSourceID="CitySqlDataSource" DataTextField="City" DataValueField="CityId" AutoPostBack="true">44

45
</asp:DropDownList>46

47
<asp:Label ID="Label_Status" runat="server" Text="" EnableViewState="false" ForeColor="Red"></asp:Label>48

49
</ContentTemplate>50

51
</asp:UpdatePanel>52

53
</td>54

55
</tr>56

57
</table>58

59
60

61
<asp:CreateUserWizard ID="createUserWizard" runat="server" LoginCreatedUser="true" 62
OnCreatedUser="createUserWizard_CreatedUser" OnCreateUserError="createUserWizard_CreateUserError">63

64
<WizardSteps>65

66
<asp:CreateUserWizardStep ID="createUserWizardStep" runat="server">67

68
<ContentTemplate>69

70
<table border="0">71

72
73

74
<tr>75

76
<td align="right">77

78
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名</asp:Label></td>79

80
<td>81

82
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>83

84
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"85

86
ErrorMessage="请填写用户名" ToolTip="请填写用户名" ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>87

88
</td>89

90
</tr>91

92
<tr>93

94
<td align="right">95

96
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码</asp:Label></td>97

98
<td>99

100
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>101

102
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"103

104
ErrorMessage="请填写密码" ToolTip="请填写密码" ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>105

106
</td>107

108
</tr>109

110
<tr>111

112
<td align="right">113

114
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">确认密码</asp:Label></td>115

116
<td>117

118
<asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>119

120
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"121

122
ErrorMessage="请填写确认密码" ToolTip="请填写确认密码"123

124
ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>125

126
</td>127

128
</tr>129

130
<tr>131

132
<td align="right">133

134
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">电子邮箱</asp:Label></td>135

136
<td>137

138
<asp:TextBox ID="Email" runat="server"></asp:TextBox>139

140
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"141

142
ErrorMessage="请填写邮箱地址" ToolTip="请填写邮箱地址" ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>143

144
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Email"145

146
ErrorMessage="请正确输入邮箱地址" ValidationGroup="CreateUserWizard" ValidationExpression="^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((".[a-zA-Z0-9_-]{2,3}){1,2})$"></asp:RegularExpressionValidator>147

148
</td>149

150
</tr>151

152
<tr>153

154
<td align="right">155

156
<asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">安全问题</asp:Label></td>157

158
<td>159

160
<asp:TextBox ID="Question" runat="server"></asp:TextBox>161

162
<asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question"163

164
ErrorMessage="Security question is required." ToolTip="Security question is required."165

166
ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>167

168
</td>169

170
</tr>171

172
<tr>173

174
<td align="right">175

176
<asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">安全回答</asp:Label></td>177

178
<td>179

180
<asp:TextBox ID="Answer" runat="server"></asp:TextBox>181

182
<asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"183

184
ErrorMessage="请填写安全回答" ToolTip="请填写安全回答"185

186
ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>187

188
</td>189

190
</tr>191

192
<tr>193

194
<td align="center" colspan="2">195

196
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"197

198
ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="密码和确认密码必须一致"199

200
ValidationGroup="createUserWizard"></asp:CompareValidator>201

202
</td>203

204
</tr>205

206
<tr>207

208
<td align="center" colspan="2" style="color: red">209

210
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>211

212
</td>213

214
</tr>215

216
</table>217

218
</ContentTemplate>219

220
<CustomNavigationTemplate>221

222
<table border="0" cellspacing="5" style="width: 100%; height: 100%;">223

224
<tr align="right">225

226
<td align="right" colspan="0">227

228
<a href="javascript:SubmitRegistration();"><img border="0" src="images/submitbutton.gif" /></a>229

230
<asp:LinkButton ID="StepNextButton" runat="server" CommandName="MoveNext" ValidationGroup="createUserWizard" />231

232
</td>233

234
</tr>235

236
</table>237

238
</CustomNavigationTemplate>239

240
</asp:CreateUserWizardStep>241

242
<asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>243

244
</WizardSteps>245

246
</asp:CreateUserWizard>247

Id为outside的table其实是我为了尽可能不改写membership还采取的一个方法,在外部看来是和注册在一起的,但是其实质却是在CreateUserWizard控件之外的。同时,我把他直接存入profile中,为使用profile所做的准备工作这里就不说了。Profile下面有两个属性,一个是Province,另一个是City。接下贴下后台的程序:
1
protected void Page_Load(object sender, EventArgs e)2

3

{4

5
if (IsPostBack)6

7

{8

9
if (ProvinceDropDownList.SelectedItem.Text == "请选择")10

11

{12

13
Label_Status.Text = "请选择省份";14

15
return;16

17
}18

19
if (CityDropDownList.SelectedItem.Text == "请选择")20

21

{22

23
Label_Status.Text = "请选择城市";24

25
return;26

27
}28

29
}30

31
}32

33

public string Province
{ get
{ return this.ProvinceDropDownList.SelectedItem.Text; } }34

35

public string City
{ get
{ return this.CityDropDownList.SelectedItem.Text; } }36

37
protected void createUserWizard_CreatedUser(object sender, EventArgs arguments)38

39

{40

41
//这里初始化了一个profileBase类42

43
ProfileBase profile=new ProfileBase();44

45
//获得刚才注册的UserName46

47
String UserName = (createUserWizard.CreateUserStep.ContentTemplateContainer.FindControl("UserName") as TextBox).Text.Trim();48

49
//根据刚才注册的UserName初始化所需要的Profile50

51
profile.Initialize(createUserWizard.UserName,true);52

53
//初始化完成就可以给profile的属性赋值了54

55
profile.SetPropertyValue("Province",Province);56

57
profile.SetPropertyValue("City",City);58

59
//将改变保存,一切完成了。现在打开对应的profile数据库表,你会看到你刚添加的用户信息所对的profile值60

61
profile.Save();62

63
}64

好了,这些就是一些主要的内容,这个是我第一次写,有不足之处和谬误之处,忘大家包涵和指正。希望对大家有用
安内网
作者:AnyKoro
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
浙公网安备 33010602011771号