How to search in website and retrieve data using loop - c#

Pardon me if my question title is not expressive ,but what I want to say is I have a simple tool that should search in a website and get the result which is 3 kinds of data (name,phone,address) I had written some codes for opening the website inside web browser tool and applied a loop statement to search for 30 numbers and it really works but the problem is it works for only one time and the second problem is in retrieving data from the other page which opens when the search button is clicked ,so suppose that I will start my sequential based number in search as 737000000 it should search for this number and opens the other page which contains the data and put these data in 3 text boxes finally back to previous page and search for 737000001 (adding 1 each time)
My codes are
private void searchBtn_Click(object sender, EventArgs e)
{
for (int i = 0; i < 30; i++)
{
string temp = string.Format("{0:D6}", i);
string formula = textBox1.Text + temp;
// Get all input elements
HtmlElementCollection inputs = webBrowser1.Document.GetElementsByTagName("input");
// Select "txtSearch" input
HtmlElement input = inputs["q"];
if (input != null)
{
string value = input.GetAttribute("VALUE");
if (!string.IsNullOrEmpty(value))
{
// Already searched but no results
}
else
{
// Input search text
string searchText = formula;
input.SetAttribute("VALUE", searchText);
var elements = webBrowser1.Document.GetElementsByTagName("button");
foreach (HtmlElement element in elements)
{
element.InvokeMember("click");
}
}
}
}
the data in the other page when I view the page source appears like
<table class="table table-responsive table-striped">
<thead>
<tr>
<th>name</th>
<th>number</th>
<th>address</th>
</tr>
</thead>
<tbody>
<tr>
<td>Mat Adam</td>
<td><b class="text-success">737000000</b></td>
<td>New York</td>
</tr> </tbody>
</table>

Related

ASP.NET Blazor uncheck all checkboxes in foreach loop with function

My Problem is that i have a list of checkboxes which are generated by a foreach loop.
How can i uncheck all the checkboxes with one function. Here is my code:
<tbody>
#foreach (var mandant in supportUserResult)
{
<tr>
<td><input type="checkbox" #onchange="eventArgs => { AddChosenSupportUserToList(mandant, eventArgs.Value); }" /></td>
<td>#(mandant.Scope)</td>
<td>#(mandant.Name)</td>
<td>#(usercategory)</td>
<td>#(mandant.FullName)</td>
<td>#(mandant.MailAddress)</td>
<td>#(mandant.Language)</td>
</tr>
}
</tbody>
I know how i can uncheck one with the #bind value, but ist not clear for me how to do that with all the generated checkboxes. And its important, that checkbox call the function 'AddChosenSupportUSerToList'.
What you're doing now is triggering an event when the checkbox is fired, without saving the state of your checkbox in your code.
You want to bind the actual state to a property. Once you've done so, you can then create a button to 'reset' each checkbox to the default state (this is how I interpret your desired behavior).
This post shows how to bind the state for one checkbox. For your purposes, you incorporate in your supportUserResult loop:
2 useful examples;
Blazor Checkbox two-way binding and change event
Blazor - Create select box dynamically
Using that example, you get something like this:
#for (int i = 0; i < supportUserResult.Count(); i++)
{
var mandant = supportUserResult[i];
<tr>
<td><input type="checkbox" #onchange="eventArgs => { ValueHasChanged(i, mandant, eventArgs.Value); }" /></td>
<td>#(mandant.Scope)</td>
<td>#(mandant.Name)</td>
<td>#(usercategory)</td>
<td>#(mandant.FullName)</td>
<td>#(mandant.MailAddress)</td>
<td>#(mandant.Language)</td>
</tr>
}
#code {
// this is your view model
var checkBoxValues = new List<bool>();
// this method will add a new item to the collection of select boxes
void ValueHasChanged(int i, Mandant mandant, eventArgs.Value )
{
if (eventArgs.Value) {
AddChosenSupportUserToList(mandant, eventArgs.Value)
}
checkBoxValues[i] = eventArgs.Value;
}
}
I'm not in a position to test the above for errors but this should help you.

Blazor navigation from within a loop?

So I'm trying to make a Blazor application that displays a customers data. When all the customers are displayed I would like to be able to navigate to an individual customer. I am having trouble making this work. The main concept is in the code below as well I have the expected model.
#for(int i = 0; i < customers.Length-1; i++)
{
<tr>
<td>#customer.Id</td>
<td>#customer.CustomerId</td>
<td>#customer.LastName</td>
<td>#customer.FirstName</td>
<td>#customer.Name1056Form</td>
<td>#customer.TrapezeClientId</td>
<td>#customer.CustomerNote</td>
<td #onclick="NavToCustomer(i)"> Details</td>
</tr>
private void NavToCustomer(int i)
{
int Id = i;
navigation.NavigateTo("/Customer/" + Id);
}
}
<td #onclick="()=>NavToCustomer(#customer.Id)"> Details</td>
private void NavToCustomer(int custID)
{
navigation.NavigateTo("/Customer/" + custID);
}
Explanation: the vanilla #onclick sends its own arguments (you can see what they are by hovering over "#onclick" in your markup). Your event handler doesn't handle MouseEventArgs, so the signatures don't match. You want to send custom info, so you should use the lambda expression instead.
--edit--
On second thought, can you just directly do this?
<td #onclick="()=>navigation.NavigateTo("/Customer/" + customer.Id)"> Details</td>

iTextSharp xmlworker - how to set dotted table border from html

Hello all you smart people of StackOverflow.
I was given a task yesterday to convert this scanned image to PDF document.
As I don't have time to learn all tips and tricks of iText, I decided to use xmlWorker and create an HTML template of the document itself.
I was quite successful, the ending result is this:
HOWEVER!
Not everything went smoothly. If you take a closer look at the scanned document, you may notice that in the middle of the document there is a table with dashed border. And this is where my headache starts.
I've been googling for the past 15 hrs trying to find a solution for this, but was not successful. I've tried all kinds of CSS border definitions like:
border-left-style: dashed;
border-style: dashed;
border: dashed;
It seems that these CSS definitions are simply ignored.
So my question is this, is there a proper way to define an HTML table with dashed border so it can be properly converted to PDF document?
I am using latest iTextSharp from Nuget (v. 5.5.12).
Thank you in advance.
Edit:
Ok, so 24 hrs later I think I have an answer.
It is a combination of these two examples:
http://codejaxy.com/q/395523/c-23-html-asp-net-itextsharp-xmlworker-using-itextsharp-xmlworker-to-convert-html-to-pdf-and-write-text-vertically
One cell with different border types
Basically I implemented IPdfPCellEvent interface so I could use a CellEvent on a PdfCell:
public class DottedCell : IPdfPCellEvent
{
private readonly int _border = 0;
public DottedCell(int border)
{
_border = border;
}
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
var canvas = canvases[PdfPTable.LINECANVAS];
canvas.SaveState();
canvas.SetLineDash(0, 2, 2);
cell.Border = Rectangle.NO_BORDER;
if ((_border & Rectangle.TOP_BORDER) == Rectangle.TOP_BORDER)
{
canvas.MoveTo(position.GetRight(1), position.GetTop(1));
canvas.LineTo(position.GetLeft(1), position.GetTop(1));
}
if ((_border & Rectangle.BOTTOM_BORDER) == Rectangle.BOTTOM_BORDER)
{
canvas.MoveTo(position.GetRight(1), position.GetBottom(1));
canvas.LineTo(position.GetLeft(1), position.GetBottom(1));
}
if ((_border & Rectangle.RIGHT_BORDER) == Rectangle.RIGHT_BORDER)
{
canvas.MoveTo(position.GetRight(1), position.GetTop(1));
canvas.LineTo(position.GetRight(1), position.GetBottom(1));
}
if ((_border & Rectangle.LEFT_BORDER) == Rectangle.LEFT_BORDER)
{
canvas.MoveTo(position.GetLeft(1), position.GetTop(1));
canvas.LineTo(position.GetLeft(1), position.GetBottom(1));
}
canvas.Stroke();
canvas.RestoreState();
}
}
After that I overrode a iTextSharp.tool.xml.html.table.TableData class:
public class TableDataProcessor : TableData
{
bool HasBorderStyle(IDictionary<string, string> attributeMap, string borderPosition, string borderStyle)
{
var hasStyle = attributeMap.ContainsKey("style");
if (!hasStyle)
{
return false;
}
var borderLeft = attributeMap["style"]
.Split(';')
.FirstOrDefault(o => o.Trim().StartsWith("border-style-" + borderPosition + ":"));
if (borderLeft != null)
{
return borderLeft.Split(':').Any(o => o.Trim().ToLower() == borderStyle);
}
return false;
}
public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
{
var cells = base.End(ctx, tag, currentContent);
var attributeMap = tag.Attributes;
if (HasBorderStyle(attributeMap, "left", "dotted"))
{
var pdfPCell = (PdfPCell) cells[0];
pdfPCell.CellEvent = null;
pdfPCell.CellEvent = new DottedCell(Rectangle.LEFT_BORDER);
}
return cells;
}
}
The last step was to add that class to tag processor for a TD element:
var tagProcessorFactory = Tags.GetHtmlTagProcessorFactory();
tagProcessorFactory.AddProcessor(
new TableDataProcessor(),
new[] {HTML.Tag.TD}
);
htmlContext.SetTagFactory(tagProcessorFactory);
And it works:
HTML markup:
<table class="content-wrapper">
<tbody>
<tr>
<td class="pcnt_60 content-left top" valign="top">
<table>
<tr>
<td style='border-left: 0.5px; border-style-left: dotted;'>content goes here</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>

C# session not saving while creating webforms

my area of programming expertise isn't web programming however I'm currently expanding my knowledge by studying C#.net webforms and I am working on an example.
I have a button which has this code:
protected void Button1_Click(object sender, EventArgs e)
{
Course course = new Course(txtId.Text, txtName.Text, txtDesc.Text);
// This line checks to see if I already have a course manager
cm = (courseManager)Session["CourseManager"];
// If I do not have a course manager, it will create one for me.
if (cm == null)
{
cm = new courseManager();
}
// This will add a course to my list of courses
cm.addCourse(course);
// This will make sure that my web page can see the list of courses.
Session.Add("CourseManager", cm);
// This will reset my text boxes
txtId.Text = "";
txtName.Text = "";
txtDesc.Text = "";
// This will print course to page
//lblId.Text = course.getId();
//lblName.Text = course.getName();
//lblDesc.Text = course.getDesc();
}
and then on the webform I have this:
<table id="courses">
<%
if (cm != null)
{
for (int i = 0; i < cm.getCourses().Count(); i++)
{
string id = cm.getCourses().ElementAtOrDefault(i).getId();
string name = cm.getCourses().ElementAtOrDefault(i).getName();
string desc = cm.getCourses().ElementAtOrDefault(i).getDesc();
%>
<tr>
<td><%=id %></td>
<td><%=name %></td>
<td><%=desc %></td>
</tr>
<%
}
}
%>
</table>
I am told this is all I need to get an active session going however the session doesn't seem to be saving the courseManager class to the session and it keeps creating new instances of the coursemanager class. Am I missing something that c#.net webforms need to keep items in a session?
regards,
Joey

webBrowser grab only the first Attribute in element (when all classes are the same name)

I made a html grabber for a webbrowser in C#, but I am stuck.
The webpage looks like this:
<tbody class="first class"
<tc class="second class">
<td class=" ">abcdefg</td>
<td class=" ">1233456</td>
<td class=" ">qwertyo</td>
</tc>
I use this code to get the content of the three classes inside "second class":
HtmlElementCollection theElementCollection = default(HtmlElementCollection);
theElementCollection = webBrowser1.Document.GetElementsByTagName("tbody");
foreach (HtmlElement curElement in theElementCollection)
{
curElement.GetElementsByTagName("tc");
curElement.GetElementsByTagName("td");
label1.Text = curElement.GetAttribute("InnerText");
return;
}
The problem is that this stores ALL 3 contents in label1, so it looks like this:
abcdefg123456qwerto
Because the classes all have the same name, I can't grab only one content.
Is there a way to only grab the first content inside an element, so that it looks like this?
abcdefg
untested
HtmlDocument document = WebBrowser.Document;
if (document != null) {
HtmlElementCollection tableCollection = document.GetElementsByTagName("tbody");
foreach (HtmlElement table in tableCollection) {
HtmlElementCollection trColl = table.GetElementsByTagName("tc");
foreach (HtmlElement row in trColl) {
tds = row.GetElementsByTagName("td");
if (tds != null && tds.Count > 1) {
string neededText = tds[0].InnerText;
// 1.iteration: neededText == abcdefg
// 2.iteration: neededText == 123456
// 3.iteration: neededText == qwertyo
}
}
}
}

Categories