views:

1390

answers:

1

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
+2  A: 

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.

Mitchel Sellers