在详细信息视图中设置不可见字段/属性的值
我有一个DetailsView,它有两个字段 - 一个是可见的,一个是不可见的。用户填写第一个,我要自动填充第二个。不幸的是,我无法找到一种方法来设置第二个不可见字段中的值。我尝试将这样的代码添加到 Page_Load:
If Not IsPostBack Then
DetailsView1.DefaultMode = DetailsViewMode.Insert
Dim txt1 As TextBox = DirectCast(DetailsView1.FindControl("Type"), TextBox)
txt1.Text = "administrator"
End If
但这会返回“对象引用未设置到对象实例”的错误。关于如何实现这一点的任何想法 - 使用上述方法还是其他方法?
希望的最终结果是,当通过DetailsView插入新记录时,该记录将包括用户名(由用户输入)以及“管理员”的“类型”
I have a DetailsView which has two fields - one that is visible, one that is not. The first the user fills out, the second I want to auto-populate. Unfortunately, I haven't been able to find a way to set the value in this second invisible field. I've tried adding code like this to the Page_Load:
If Not IsPostBack Then
DetailsView1.DefaultMode = DetailsViewMode.Insert
Dim txt1 As TextBox = DirectCast(DetailsView1.FindControl("Type"), TextBox)
txt1.Text = "administrator"
End If
But this returns an error of "Object reference not set to an instance of an object." Any ideas on how to accomplish this - either using the method above or another method?
The hoped for end result is that when a new record is inserted via the DetailsView that this record will include the username (entered by the user) as well as the "type" of "administrator"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该设置控件的样式:
这样浏览器就不会显示它们。
如果将服务器上的控件设置为不可见,则不会呈现 html。
编辑:
您还可以使用隐藏字段:
但是,最好将信息保存在服务器上的会话变量或其他内容中。客户端隐藏字段中的信息可以被组合)。
You should set the style of the controls :
That way the browser will not show them.
If you set the controls on the server to invisible, there will be no html rendered.
Edit:
You could also use a hidden field:
But, it may be better to keep the information on the server, in a session variable or something. Information in hidden fields on the client can be compoimised).
当我遇到这个问题时,我解决这个问题的方法是使控件可见并将其放在面板或另一个控件后面。
the way I solved this when I had this problem was by making the control visible and putting it behind a panel or another control.