سؤال

This is our CMIS(Computer Management Information System) Server page: (Note: The CMIS server I cannot modify, the page is automatic generation.)

<form method="POST" action="bl_view_invoice_controler.jsp" name="fm1" onSubmit="return checkValid()">
    //please attention the form name is 'fm1'
    <TABLE id=AutoNumber7 style="BORDER-COLLAPSE: collapse" 
        borderColor=#111111 cellSpacing=0 cellPadding=2 width="100%" 
        border=0>
        <TBODY>
            <TR>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
            </TR>
            <TR>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
                <TD width="40%"><input type="text" class="underline1" name="ivcd_item" size="15"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_unitprice" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_quantity" size="8"></TD>
                <TD width="20%"><input type="text" class="underline1" name="ivcd_totalfc" size="8"></TD>
            </TR>
        </TBODY>
    </TABLE>
    <script language="JavaScript"> //this script store the value of the table
        fm1.ivcd_item['0'].value="B747-400";
        fm1.ivcd_totalfc['0'].value="500.00";
        fm1.ivcd_unitprice['0'].value="1";
        fm1.ivcd_quantity['0'].value="";

        fm1.ivcd_item['2'].value="B747-800";
        fm1.ivcd_totalfc['2'].value="250.00";
        fm1.ivcd_unitprice['2'].value="";
        fm1.ivcd_quantity['2'].value="";
    </script>           
</FORM>

My question is, How to use C# get the value of the table, suach as 'B747-400' or 'B747-800'?

هل كانت مفيدة؟

المحلول

I would also say "use HtmlAgilityPack", if there were no scripts to be executed after the page is downloaded. So My solution is: first downloading the page using WebClient then parsing it using the WebBrowser component.

WebBrowser web = new WebBrowser();
web.DocumentCompleted += (sender, args) =>
    {
        var result = web.Document
            .GetElementsByTagName("input")
            .Cast<HtmlElement>()
            .Where(e => e.GetAttribute("name") == "ivcd_item")
            .Select(e=>e.GetAttribute("value"))
            .ToArray();
    };
web.DocumentText = htmlstring;

نصائح أخرى

I think you can do like this..

1, place a hidden text , like

<input type="hidden" id="tableInfo" runat="server" />

2, put a button , like

<input type = "button" id = "btnSample" onclick = "getTableInfo();" />

3, implement your getTableInfo function by javascript like

function getTableInfo()
{
     var tb=$("tableName");
     var data=[];
     for(var i=1;i<tb.rows.length;i++) {
        data.push(tb.rows[i].cells[0].firstChild.value);
        data.push(tb.rows[i].cells[1].firstChild.value);
        data.push(tb.rows[i].cells[2].firstChild.value);
     }
     $("tableInfo").value=data.join("`");

     __doPostBack("Button1" , ""); // Call you server side function here
}

4, Then in your Button1's server side function , your can get the tableinfo from the hidden textbox

Use HTML Agility Pack to scrape the HTML and get your value.

If you make the td runat=server and give it an id, you can refer to it by that id in the C# code behind

<td runat="server" id="myTd">Value</td>

As explained above give an ID to element and set runat="server" to access any element in C# code. Above code is an example for you and rightly written.

<td runat="server" id="myTd">Value</td>

use:

<input type="text" class="underline1" name="ivcd_item" id="ivcd_item" runat="server" size="15">

then in code-behind:

ivcd_item.Value;

You could also use the ASP.Net TextBox instead of the input type text here.

Edit:

I see you can't edit the page and I presume you only have access to it using regular HTTP requests?. Then you can use scraping to get the values from the page. I think it's the only way. You can do a webrequest to get the HTML and look at the fields in the form, as in: http://crazorsharp.blogspot.com/2009/06/c-html-screen-scraping-part-1.html

You could ofcourse also use JQuery for this, get the content of the page using a get request and scrape the html. See this link for more info: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

If I am not wrong, what you are looking for is this..

create a WinForms project.. and a WebBrowser to the Form from tools

put following code in Form_Load event method..

webBrowser1.Navigate("<your URL here>");

put the following code in the DocumentCompleted event of the browser

        HtmlElementCollection coll = webBrowser1.Document.
                   GetElementsByTagName("input").GetElementsByName("ivcd_item");
        string ivcd_item;
        foreach( HtmlElement el in coll)
        {
            //condition here to check of the index of the element and 
            //other stuffs required
            ivcd_item= el.GetAttribute("value");
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top