如何将值从Listview内部的控件传递到外部的另一个控件

发布于 2024-10-14 22:49:53 字数 932 浏览 1 评论 0原文

我有一个列表视图,当时显示一条记录。这是一个非常简单的示例:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2024-10-21 22:49:53

需要考虑的事项:

  1. ListView 是否在另一个容器控件中?
  2. ListView1 是否返回 ListView 类的实例?
  3. 向表单添加一个按钮并将其单击事件连接到您的代码。现在有用吗? 4. 你的代码调用时ListView上是否已经绑定了数据?

这些类型的错误消息可能会有点误导,因此我发现最好从头开始,不要对正在发生的事情做出任何假设。

祝你好运!

Things to consider:

  1. Is the ListView in another container control?
  2. Is ListView1 returning an instance of the ListView class?
  3. Add a button to the form and wire up it's click event to your code. Does it work now? 4. Has data already been bound to the ListView when your code is being called?

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!

濫情▎り 2024-10-21 22:49:53

感谢您的回答。

我不断尝试,直到找到解决方案。这就是我所做的:

在 ListView 部分中,我添加了一个 OnPreRender 部分:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="bol_id" ItemPlaceholderID="itemPlaceholder" OnPreRender="ListView1_OnPreRender">

然后在后面的代码中我添加了以下内容:

Protected Sub ListView1_OnPreRender(ByVal sender As Object, ByVal e As EventArgs)

    If ListView1.EditIndex > -1 Then
        ' because I need to avoid error when in Edit mode
    Else

        Dim tb As TextBox = Nothing

        For Each item As ListViewDataItem In ListView1.Items

            tb = DirectCast(item.FindControl("txtInsideId"), TextBox)

            If tb.Text IsNot Nothing Then
                txtOutsideId.Text = tb.Text.ToString
            End If
        Next

    End If


End Sub

现在它可以工作了!不知道这是否是最好的方法,但它确实有效。

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:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="bol_id" ItemPlaceholderID="itemPlaceholder" OnPreRender="ListView1_OnPreRender">

Then in the Code Behind I added this:

Protected Sub ListView1_OnPreRender(ByVal sender As Object, ByVal e As EventArgs)

    If ListView1.EditIndex > -1 Then
        ' because I need to avoid error when in Edit mode
    Else

        Dim tb As TextBox = Nothing

        For Each item As ListViewDataItem In ListView1.Items

            tb = DirectCast(item.FindControl("txtInsideId"), TextBox)

            If tb.Text IsNot Nothing Then
                txtOutsideId.Text = tb.Text.ToString
            End If
        Next

    End If


End Sub

And now it works! Don't know if it is the best way, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文