0

Trying to complete an asp.net search form, but this simple task is stumping me and preventing retrieving the data. just cannot pass a value to the text input.

The form's html is

<div id="ctl00_cntMain_Updatepanel1">
    <div class="flyout">
        <h2>ENTITY SEARCH</h2>
        <div id="ctl00_cntMain_pnlSearchBox"
            onkeypress="javascript:return WebForm_FireDefaultButton(event, &#39;ctl00_cntMain_lnkSearchIcon&#39;)">
          <table cellpadding="0" cellspacing="0" border="0" width="100%">
            <tr>
              <td style="width: 30px; padding: 10px 10px 10px 10px;">
                <select name="ctl00$cntMain$drpSearchOptions"
                onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl00$cntMain$drpSearchOptions\&#39;,\&#39;\&#39;)&#39;, 0)"
                id="ctl00_cntMain_drpSearchOptions" class="dropdownSearch">
                  <option selected="selected" value="0">Search by</option>
                  <option value="CoName">Company Name</option>
                  <option value="CoNo">Company No.</option>
                  <option value="IDNo">Owner ID</option>
                </select>
              </td>
              <td style="padding: 10px 10px 10px 10px;">
              <input name="ctl00$cntMain$txtSearch" type="text" id="ctl00_cntMain_txtSearch" class="textBoxSearch"
              style="width:100%;" /> 
              <input type="hidden" name="ctl00$cntMain$wtmkSearch_ClientState" id="ctl00_cntMain_wtmkSearch_ClientState" /></td>
              <td style="width: 10px; padding: 10px 10px 10px 10px;">
                <a id="ctl00_cntMain_lnkSearchIcon" onmouseover="this.style.color=&#39;#78ae6a&#39;;"
                onmouseout="this.style.color=&#39;#00727D&#39;;"
                href="javascript:WebForm_DoPostBackWithOptions(new%20WebForm_PostBackOptions(&quot;ctl00$cntMain$lnkSearchIcon&quot;,%20&quot;&quot;,%20true,%20&quot;Search&quot;,%20&quot;&quot;,%20false,%20true))"
                style="color:#00727D;"></a>
              </td>
            </tr>
            <tr>
              <td colspan="3">
              <span id="ctl00_cntMain_RequiredFieldValidator1" style="color:Red;display:none;">Type in your search query</span> 
              <span id="ctl00_cntMain_RequiredFieldValidator15" style="color:Red;display:none;">Select search option</span></td>
            </tr>
          </table>
        </div>
    </div>
</div>

This is my VBA code to interact with the form

CoName = "Any Company Name"

If CoName <> "" Then
    For Each ele In objIE.document.getElementById("ctl00_cntMain_drpSearchOptions") 
        If ele.Value = "CoName" Then ele.Selected = True: Exit For
    Next
    objIE.document.getElementsByName("ctl00$cntMain$txtSearch")(0).Value = CoName

    'have tried getElementById("ctl00_cntMain_txtSearch").value = CoName but did not succeed
    objIE.document.getElementById("ctl00_cntMain_lnkSearchIcon").Click

    Do While .Busy = True Or objIE.readyState <> 4: DoEvents: Loop
End If  

Appears that value of text input is not set correctly, it seems to appear as "placeholder" text

0

1 Answer 1

0

Try the following which has shorter syntax for selecting option then puts Focus on input field before assigning value

objIE.document.querySelector("[value=CoName]").Selected = True

With objIE.document.querySelector("#ctl00_cntMain_txtSearch")
    .Focus
    .Value = "some value"
End With

objIE.document.querySelector("#ctl00_cntMain_lnkSearchIcon").click
Sign up to request clarification or add additional context in comments.

9 Comments

code works, but still the same result, i've noticed selecting different options do not trigger "onchange javascript" resulting in the error. if i intercept it manually, it triggers the javascript and rest of code runs fine. so, how do i ensure the javascript runs, at least once it seems?
javascript associated with what?
To register change with option then objIE.document.querySelector("#ctl00$cntMain$drpSearchOptions").FireEvent "onchange"
did find your other post, code now works
so can this one be closed as a duplicate? Please indicate the post link
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.