如何将值从Listview内部的控件传递到外部的另一个控件
我有一个列表视图,当时显示一条记录。这是一个非常简单的示例:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="bol_id" ItemPlaceholderID="itemPlaceholder">
<ItemTemplate>
Textbox inside<asp:TextBox ID="txtInside" runat="server" Text='<%# Eval("bol_id") %>'></asp:TextBox>
</ItemTemplate>
</asp:ListView>
Textbox Outside<asp:TextBox ID="txtOutside" runat="server" Text=""></asp:TextBox>
一切正常,ItemTemplate 内的文本框从数据库获取数据并进行填充。我还有一个寻呼机,可以毫无问题地一次移动一条记录。
现在,我想获取 ItemTemplate 外部的文本框以获得与内部文本框相同的值。但我没能让它发挥作用。当我尝试此代码和类似代码时:
此代码放置在页面加载后面的代码中:
Dim tb As TextBox = DirectCast(ListView1.FindControl("txtInside"), TextBox)
txtOutside.Text = tb.Text
我得到的只是错误:
Object reference not set to an instance of an object
任何人都有一个好的解决方案如何访问此控件并将其值传递给其他控件?
I have a Listview displaying one record at the time. Here is a very simple example:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="bol_id" ItemPlaceholderID="itemPlaceholder">
<ItemTemplate>
Textbox inside<asp:TextBox ID="txtInside" runat="server" Text='<%# Eval("bol_id") %>'></asp:TextBox>
</ItemTemplate>
</asp:ListView>
Textbox Outside<asp:TextBox ID="txtOutside" runat="server" Text=""></asp:TextBox>
Everything works fine, the Textbox inside the ItemTemplate, gets its data from the database and gets populated. I also have a pager that moves one record at the time without a problem.
Now, I would like to get the Textbox outside the ItemTemplate to get the same value as the Textbox inside. But I do not manage to get it working. When I try this and similar code:
This code is placed in the Code Behind Page Load:
Dim tb As TextBox = DirectCast(ListView1.FindControl("txtInside"), TextBox)
txtOutside.Text = tb.Text
all I get is the error:
Object reference not set to an instance of an object
Anyone, have a good solution how to access this control and pass its value to the other control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要考虑的事项:
这些类型的错误消息可能会有点误导,因此我发现最好从头开始,不要对正在发生的事情做出任何假设。
祝你好运!
Things to consider:
These types of error messages can be a bit misleading so I have found it best to start from the beginning and make no assumptions about what is happening.
Good luck!
感谢您的回答。
我不断尝试,直到找到解决方案。这就是我所做的:
在 ListView 部分中,我添加了一个
OnPreRender
部分:然后在后面的代码中我添加了以下内容:
现在它可以工作了!不知道这是否是最好的方法,但它确实有效。
Thanks for your answer.
I kept on trying until I found a solution. This is what I did:
In the ListView part, I added a
OnPreRender
part:Then in the Code Behind I added this:
And now it works! Don't know if it is the best way, but it works.