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