将表单数据从母版页传递到其他页面 ASP.NET
我以前也问过类似的问题,但我并不清楚我遇到的问题是什么。 我的问题是,当页面更改时,我无法从驻留在母版上的文本框获取数据。 发生的情况如下:
- 带有文本框和按钮的 MasterPage。
- 将数据输入到文本框中并单击按钮。
- 表单的操作设置为 search.aspx,webapp 导航到那里。
- 此函数获取文本框的内容:
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:
- MasterPage with TextBox and Button.
- Data is entered into the TextBox and Button is clicked.
- Form's action is set to search.aspx, webapp navigates there.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于要加载到控件内部页面的值,您必须位于回发内部,这就是它在从搜索页面发布时起作用的原因。
要在输入来自其他区域时获取值,您需要使用 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.