Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to enter some data from a windows form application written c# to a web page and I'm stuck. If anyone can help I would greatly appreciate it. The application has a form and a browser in another tab. I input data to the form and on submit it brings up the web page I want data entered into. If the page is not open I auto log into it using the first block of code below to input my user name and password. This part works great. Now with the page open I tried two different approaches to try and get my form data into the web page and they both fail to do anything. No errors or exceptions so I'm assuming the code is working but its not finding anything. I hope I have given enough information...
Thanks in advance for anyone that looks at this!

Edit: I added a line of code to write a text file with the output results of the element collection I was using to try and find the textbox I wanted and this is where the code seems to be stopping.
The text file only contained System.__ComObject for about 25 lines so that's all I'm getting for the collections array. No elements so now I know why its stopping. Is there a way to do this? Or is this all server side and no way to access those fields?


C#
public object Document
       {
           get { return _ActiveWebBrowser.Document; }
       }

       ///
       /// Load the doc, get the elements, complete the forms values and submit
       ///
       public void LoadDocument(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
       {
           //Reference to "Microsoft HTML Object Library"
           MSHTML.IHTMLDocument3 document = null;
           int maxwait = 10000;
           try
           {
               //Make sure it is loaded and ready
               for(int count=0;count < maxwait;count++)
               {
                   if(_ActiveWebBrowser.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                       break;
               }

               if (_ActiveWebBrowser.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
               {
                   //Get the document
                   document = _ActiveWebBrowser.Document as MSHTML.IHTMLDocument3;

                   //I'm getting elements by name input
                   //but I also tried by tagname
                   MSHTML.IHTMLElementCollection colHTML = document.getElementsByName("frmSearch");

                   //Loop over them to find the ones you want

                   foreach (MSHTML.HTMLInputElement el in colHTML)
                   {
                       //Example gets an input element with name=username
                       if ((el.type == "TextBox") && (el.name == "txtTicketNo"))
                       {
                           //Ticketbox.text is the textbox on my form
                           //but I also tried "12345" in place of it                          //to eliminate any possible issue with my form data
                           el.value = Ticketbox.text;
                       }
                       if (el.type == "btnSearch")
                       {
                           MSHTML.HTMLInputElement btnSubmit = el;
                           btnSubmit.click();
                       }

                   }

               }
               else
                   throw new Exception("Timeout waiting for form object to load.");
           }
           catch
           {
               //Do something
           }
       }


Here's the view source of the page I'm trying to input to

         <pre lang="xml"><HTML>
          <HEAD>
               <META id="META4" name="META4">
               </META>
               <TITLE id="TITLE5">
               </TITLE>
          </HEAD>
          <BODY bottomMargin="0" leftMargin="0" rightMargin="0" topMargin="0">
               <form id="frmSearch" name="frmSearch">
                    <Table id="Table7" border="0" cellPadding="0" cellSpacing="0" class="case_bodybg" width="100%">
                         <TR id="TR8">
                              <TD id="TD9" class="case_bodybg">
                                   <Table id="Table10" border="0" cellPadding="1" cellSpacing="5" class="case_bodybg">
                                        <TR id="TR11" class="normaltext">
                                             <TD id="TD23" width="120">Ticket No.
                                             </TD>
                                             <TD id="TD24" width="5">
                                             </TD>
*****This is the box I want to fill "txtTicketNo"*******
                                             <TD id="TD25" width="180">
                                                  <input class="textbox_medium" tabIndex="2" maxLength="9" name="txtTicketNo" size="15" type="TextBox" id="txtTicketNo"></input>
                                             </TD>
                                             </TR>
                                        <TR id="TR13" align="Center" class="normaltext">
                                             <TD id="TD292" align="Center" vAlign="Bottom" colSpan="11">
                                                  <Table id="Table293" align="Center">
                                                       <TR id="TR294" align="Center" vAlign="Bottom">
                                                            <TD id="TD287" align="Center" vAlign="Bottom">
                                                                 <img id="btnSearch" align="Center" dataSrc="dsSearchOrder" name="btnSearch" src="../../../images/btn_Find.gif" style="cursor:hand;" ActionEvent="onclick" validateControl="True" validateFunction="ValidateSave" callbackFunction="Order_callBack" gridStylePath="Ticket.xml" ResultSetType="grid" onClick="SavePage">
                                                                 </img>
                                                            </TD>
                                                            <TD id="TD288" align="Center" vAlign="Bottom">
                                                                 <img id="btnClear" align="Center" name="btnClear" src="../../../images/btn_Clear.gif" style="cursor:pointer;" onClick="clearFields('frmSearch')">
                                                                 </img>
                                                            </TD>
                                                            <TD id="TD308" align="Center" vAlign="Bottom">
                                                                 <img id="ImgCTSMSearch" name="ImgCTSMSearch" src="../../../images/btn_ctsmsearch.gif" style="cursor:pointer;" onClick="callCTSM();">
                                                                 </img>
Posted
Updated 15-Nov-10 8:13am
v5
Comments
Uwe Keim 15-Nov-10 11:29am    
In fact, you have given way _too_ much information, but you are omitting the important information about the error that you see. Just saying "both fail to do anything" is to few information.
Henry Minute 15-Nov-10 11:30am    
Awful lot of code for anyone to read through. You might consider removing the first block, if you are sure that it works and if at all possible try to reduce the rest.
deaddog4201 15-Nov-10 11:49am    
Sorry I removed portions I hope are irrelevant to finding an answer. And as I stated above there are no errors. The page loads, it logs in, page load completes and nothing gets filled into the "txtTicketNo" box I am trying to populate.

1 solution

Where you loop through your objects in looking for the textbox, you could possibly look for
(el.id == "txtTicketNo"))

instead of
(el.name == "txtTicketNo"))


I'm also not convinced of looking for a type of "textbox". For some reason that looks wrong to me, but it may just be the type of day I'm having.
 
Share this answer
 
v2
Comments
deaddog4201 15-Nov-10 17:48pm    
I have tried Id and name neither work. The reason I used "Textbox" as the type filed if you look at the source for the page below that is the type name of the field im trying to input.

[code]
<TD id="TD25" width="180">
<input class="textbox_medium" tabIndex="2" maxLength="9" name="txtTicketNo" size="15" type="TextBox" id="txtTicketNo"></input>
[/code[
fjdiewornncalwe 19-Nov-10 8:14am    
Put a breakpoint in your source code on the "if ((el.type == "TextBox") && (el.name == "txtTicketNo"))" call and see if you even hit it. If you do, check the type of each element coming through until you find the one you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900