将用户控件加载到字符串并在其中提交表单
我正在做的是创建一个网站,其中的设计是在 html 文件中完成的,然后使用 System.IO.StreamReader 将其读入母版页。 在 html 模板内有诸如 #USER.LOGIN# 之类的关键字 我用函数等替换。
问题是我正在用有登录表单的用户控件替换#USER.LOGIN#。 我有一个将用户控件读入字符串的函数并且它可以工作。 但由于用户控件已加载到字符串 alle,因此事件不会跟随。
因此,当我提交登录表单时,什么也没有发生(页面帖子),但无法从表单中获取任何字段...
注意: 我正在使用网址重写,因此网址为 http://www.domain.com/account/login 其中 account 是 account.aspx,login 是帐户所在的模式。
用于替换 StreamReader 循环中关键字的代码(pr 行)
If InStr(line, "#USER.LOGIN#") Then
line = line.Replace("#USER.LOGIN#", vbCrLf & userfunc.GetMyUserControlHtml("uc", "account_login.ascx", "/account/login/") & vbCrLf)
End If
以及读取 usercontrol 的函数
Public Shared Function GetMyUserControlHtml(contextKey As String, controllerfile As String, Optional ByVal formaction As String = "")
Dim myId As Guid = New Guid()
Return userfunc.RenderUserControl("~\Controllers\" & controllerfile, "", myId, formaction)
End Function
Public Shared Function RenderUserControl2(path As String, Optional ByVal formaction As String = "") As String
Using pageHolder As New Page(), _
viewControl As UserControl = DirectCast(pageHolder.LoadControl(path), UserControl), _
output As New StringWriter(), _
tempForm As New HtmlForm()
If formaction <> "" Then
tempForm.Action = formaction
Else
tempForm.Action = HttpContext.Current.Request.RawUrl
End If
tempForm.Controls.Add(viewControl)
pageHolder.Controls.Add(tempForm)
HttpContext.Current.Server.Execute(pageHolder, output, False)
Dim outputToReturn As String = output.ToString()
Return outputToReturn
End Using
End Function
您会如何执行此操作? 我需要将用户登录名硬编码在用户控件中,但仍然能够使用模板关键字将其放置在任何地方。 这也将与其他功能一起使用(时事通讯注册、shoutbox 等)
What i'm doing is creating a website where the design is done i html files that are then read into the masterpage using System.IO.StreamReader.
and inside the html templates there are keywords like #USER.LOGIN#
that I replace with functions etc.
The issue is that i'm replacing #USER.LOGIN# With a usercontrol where there is a login form.
I have a function that reads the usercontrol into a string and it works.
but since the usercontrol is loaded to string alle the events are not following.
so when I submit the login form nothing nothing happends (the page posts) but cannot get any of the fields from the form...
NOTE:
i'm using url-rewriting so urls are http://www.domain.com/account/login
where account is account.aspx and login is the mode the account is in.
Code for replacing the keyword in the streamreader loop (pr line)
If InStr(line, "#USER.LOGIN#") Then
line = line.Replace("#USER.LOGIN#", vbCrLf & userfunc.GetMyUserControlHtml("uc", "account_login.ascx", "/account/login/") & vbCrLf)
End If
And the functions to read usercontrol
Public Shared Function GetMyUserControlHtml(contextKey As String, controllerfile As String, Optional ByVal formaction As String = "")
Dim myId As Guid = New Guid()
Return userfunc.RenderUserControl("~\Controllers\" & controllerfile, "", myId, formaction)
End Function
Public Shared Function RenderUserControl2(path As String, Optional ByVal formaction As String = "") As String
Using pageHolder As New Page(), _
viewControl As UserControl = DirectCast(pageHolder.LoadControl(path), UserControl), _
output As New StringWriter(), _
tempForm As New HtmlForm()
If formaction <> "" Then
tempForm.Action = formaction
Else
tempForm.Action = HttpContext.Current.Request.RawUrl
End If
tempForm.Controls.Add(viewControl)
pageHolder.Controls.Add(tempForm)
HttpContext.Current.Server.Execute(pageHolder, output, False)
Dim outputToReturn As String = output.ToString()
Return outputToReturn
End Using
End Function
How would you guyz do this?
I need the userlogin to be hardcoded in the usercontrol but still be able to place it anywhere using the template keyword.
This will also be used with other functions (newsletter signup, shoutbox etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议是在网络配置上注册您的控件..
您仍然可以使用“#USER.LOGIN#”,但不要用控件替换它...
将其替换为类似的内容,
这只是一个快速编写的内容..但您可以随时尝试,如果它有效,
您可以像这样保存 HTML,而不是放置实际的“#USER.LOGIN#”
,然后在中创建一个公共函数您的代码名为
GetLoginControl()
并返回您需要的 HTML 标记的response.writewhat i would suggest is register you control on the web config..
you can still use "#USER.LOGIN#" but instead of replacing it with a control...
replace it with a something like this
this is just a quick write up.. but you could always try if it works
you can save your HTML like this istead of placing an actual "#USER.LOGIN#"
and then create a public function in your code behind named
GetLoginControl()
and return a response.write of the HTML Mark up you need