将表单数据从母版页传递到其他页面 ASP.NET

发布于 2024-07-07 23:18:38 字数 1678 浏览 8 评论 0原文

我以前也问过类似的问题,但我并不清楚我遇到的问题是什么。 我的问题是,当页面更改时,我无法从驻留在母版上的文本框获取数据。 发生的情况如下:

  1. 带有文本框和按钮的 MasterPage。
  2. 将数据输入到文本框中并单击按钮。
  3. 表单的操作设置为 search.aspx,webapp 导航到那里。
  4. 此函数获取文本框的内容:

Public Function oSearchString(ByVal oTextBoxName As String) As String  
    If Master IsNot Nothing Then  
        Dim txtBoxSrc As New TextBox  
        txtBoxSrc = CType(Master.FindControl(oTextBoxName), TextBox)  
        If txtBoxSrc IsNot Nothing Then  
            Return txtBoxSrc.Text  
        End If  
    End If  
    Return Nothing  
End Function

当此代码执行时,即使文本框中输入了文本,它也会返回 ""。 我尝试将默认值放入框中,并且该值可以很好地通过(即 产生 "searchbox")。

现在我已经从主页提交了搜索表单,我现在位于搜索页面 (search.aspx)。 如果我再次输入搜索字符串,代码将返回我输入文本框中的任何内容。 我尝试将上面的代码从 Master 修改为 PreviousPage 但这根本不起作用,因为文本框控件驻留在在母版页上。

希望我已经很好地阐述了背景信息,如果需要进一步说明,请告诉我。


编辑:使用Request.Form("searchbox") 不会产生Nothing。 我检查了 Request.Form() 对象,发现我的文本框的 ID 实际上是 ctl00$searchbox。 使用它作为 ID 或其索引(在本例中为 3)给了我正确的结果。 最好重写该函数来检查 Request.Form() 中的所有键是否包含 searchbox 的键,或者有没有办法获得实际的 ID文本框? 对于前一个选项,这是我想出的:

Public Function oSearchString(ByVal oTextBoxName As String) As String
    For Each oKey As String In Request.Form.AllKeys
        If oKey.Contains(oTextBoxName) Then
            Return Request.Form(oKey)
        End If
    Next
    Return ""
End Function

I have asked a similar question before, but I didn't have a firm grasp on what the problem I was encountering was. My problem is I cannot get the data from a TextBox residing on the master when the page changes. Here is what happens:

  1. MasterPage with TextBox and Button.
  2. Data is entered into the TextBox and Button is clicked.
  3. Form's action is set to search.aspx, webapp navigates there.
  4. This function gets the TextBox's content:

Public Function oSearchString(ByVal oTextBoxName As String) As String  
    If Master IsNot Nothing Then  
        Dim txtBoxSrc As New TextBox  
        txtBoxSrc = CType(Master.FindControl(oTextBoxName), TextBox)  
        If txtBoxSrc IsNot Nothing Then  
            Return txtBoxSrc.Text  
        End If  
    End If  
    Return Nothing  
End Function

When this code executes, it returns "", even though there is text entered into the box. I tried putting a default value into the box, and that gets passed through just fine (i.e. <asp:TextBox ID="searchbox" runat="server" text="searchbox"></asp:TextBox> yields "searchbox").

Now that I have submitted the search form from the home page, I am on the search page (search.aspx). If I enter the search string again, the code returns whatever I put into the textbox. I tried modifying the code above from Master to PreviousPage but that did not work at all since the textbox control resides on the Master page.

Hopefully I laid out the background information well enough, let me know if further clarification is required.


EDIT: Using Request.Form("searchbox") yields Nothing. I inspected the Request.Form() object and found that my textbox's ID is actually ctl00$searchbox. Using that as the ID or its Index (3 in this case) gives me the proper result. Would it be best to rewrite the function to check all the keys in Request.Form() for keys containing searchbox, or is there a way I can get the actual ID of the textbox? For the former option, this is what I came up with:

Public Function oSearchString(ByVal oTextBoxName As String) As String
    For Each oKey As String In Request.Form.AllKeys
        If oKey.Contains(oTextBoxName) Then
            Return Request.Form(oKey)
        End If
    Next
    Return ""
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

顾冷 2024-07-14 23:18:38

对于要加载到控件内部页面的值,您必须位于回发内部,这就是它在从搜索页面发布时起作用的原因。

要在输入来自其他区域时获取值,您需要使用 Request.Form("Element") 从发布的表单中获取值。 如果您需要这样做,只需确保您知道搜索框的 id 就可以了。

For the value to be loaded to the page inside the control, you must be inside a postback, thus the reason it works when posting from the Search page.

To get the value when the input is from other areas, you will need to use Request.Form("Element") to get the value from the posted form. If this is the way you need to go, just be sure you know the id of the search box and you should be fine.

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