private void timer1_Tick(object sender, EventArgs e)
{
try
{
this.Text = this.comboBox1.SelectedIndex.ToString() + "/" + (object)this.comboBox1.Items.Count;
string str = new WebClient().DownloadString("webpage.com" + this.comboBox1.SelectedItem.ToString());
if (!str.Contains("Message"))
this.listBox1.Items.Add((object)this.comboBox1.SelectedItem.ToString());
else if (str.ToLower().Contains(this.comboBox1.SelectedItem.ToString()))
;
++this.comboBox1.SelectedIndex;
}
catch
{
if (this.comboBox1.SelectedIndex != this.comboBox1.Items.Count - 1)
return;
this.timer1.Stop();
}
}
I want to check whether the url page contains a specific html class name in the page its on html code
If you really don't want to use HtmlAgilityPack then you could try something like this:
str.Contains("class=\"" + nameOfClass)
I would also recommend to remove whitespaces before comparison.
Related
I am having the following problem: I want to call a method whenever a specific li is clicked. Problem is that the li is dynamically created in a literal.text string, where I do import things from my database.
Whenever I try to call a method it does not work. I want to call a method whenever the user clicks on each li and get that li information inside my method (haven't wrote the method code yet, because I can't get it called.)
Thoughts?
protected void Page_Load(object sender, EventArgs e)
{
string conString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source="
+ Server.MapPath("~/ebookstoredb.mdb");
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
string query = "SELECT * FROM CATEGORY";
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
OleDbDataReader reader = cmd.ExecuteReader();
String msg = "";
while (reader.Read())
{
lit1.Text += "<ul>" + "<li runat=\"server\" OnClick=\"ProductsInfo\">" + reader["ID"]
+ "," + reader["Name"]
+ "</li>"
+ "</ul>";
}
reader.Close();
}
con.Close();
}
}
protected void ProductsInfo(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
Unfortunately, you cannot create server-side events on a Literal Control.
However, you can add client-side javascript functionality to post back a request.
In your aspx using:
<div>
<asp:Literal runat="server" ID="lit1"></asp:Literal>
</div>
Your aspx.cs should contain:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lit1.Text += "<ul>" + "<li \' onclick=\'javascript: __doPostBack(\"getProduct\", \"1\");\'>"
+ "Product " + "1"
+ "</li>"
+ "</ul>";
}
if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"] == "getProduct")
{
getProduct_Click(null, null);
}
}
private void getProduct_Click(object sender, System.EventArgs e)
{
Response.Write("You Clicked on " + Request.Form["__EVENTARGUMENT"]);
}
This will set up each li to pass their own value to the event argument hidden control and perform a postback to the server.
You can then check if the event target is the required one and call a method with the value that was posted back.
Just change the sample lit1 text above to iterate through your data.
I've never used literals before, but from reading about them... with a literal displaying just static html, you will need to have each li call a javascript function that will then do a postback to the method you want. So build your li like this:
lit1.Text += "<ul><li><a onclick=\"CallProductInfo(" + reader["ID"]+ ")\">" + reader["ID"]
+ "," + reader["Name"]
+ "</a></li></ul>";
Then you have to have a javascript function that does the actual postback to your server side code or redirects to the products info page with the passed id. That is, in your page (not the code behind), have a script something like this for the postback...
<script>
function CallProductInfo(id)
{
__doPostBack('ProductInfoId', id);
}
</script>
In you code behind, in your page load event handler, you'd have something like this:
if (Request["__EVENTTARGET"] == "ProductInfoId")
{
ProductInfo(Convert.ToInt64(Request["__EVENTARGUMENT"]));
}
Ok so guys i found out what was the problem. Our mr.genious instructor told us today that we can use a datagrid..Sigh.
Thanks everyone for your replies!
I am just wondering if anyone could give me indication as to how to remove a piece of text if a statement is not satisfied after onSelectedChange event.
My code,
protected void currency_SelectedIndexChanged(object sender, EventArgs e)
{
if (stm_currency.SelectedItem != null)
{
lblResults.Text = "" +
stm_merchant.SelectedItem.Text + " statement for " +
stm_month.SelectedItem.Text + " " +
stm_year.SelectedItem.Text;
}
else
{
lblResults.Text.Remove(0);
}
}
change this line of code
lblResults.Text = "";
It would set it to be an empty string.
The remove method returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
http://msdn.microsoft.com/en-us/library/d8d7z2kk(v=vs.110).aspx
You should use lblResults.Text = ""; or lblResults.Text = string.Empty;
You should check to see if the label needs invoked first.
delegate void setLabelText(string s);
public void invokeSetLabelText(string s)
{
if (this.lblResults.InvokeRequired)
{
setLabelText d = new setLabelText(invokeSetLabelText);
this.Invoke(d, new object[] { s });
}
else
lblResults.Text = s;
}
protected void currency_SelectedIndexChanged(object sender, EventArgs e)
{
if (stm_currency.SelectedItem != null)
invokeSetLabelText(string.Format("{0} statement for {1} {2}",
stm_merchant.SelectedItem.Text,
stm_month.SelectedItem.Text,
stm_year.SelectedItem.Text));
else
invokeSetLabelText(string.Empty);
}
I want to have a textbox that displays the word Seq (which is a column name), then lists values from mylist underneath it. So far, the values from the list show up but the word Seq doesn't
private void button7_Click(object sender, EventArgs e)
{
if (seq1)
{
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
foreach (object o in SeqIrregularities)
{
textBox1.Text = String.Join(Environment.NewLine, SeqIrregularities);
}
}
}
You're reassigning the value of textBox1.Text to your list of values, rather than appending the list of values to the textbox contents.
Try this:
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
textBox1.Text += Environment.NewLine + String.Join(Environment.NewLine, SeqIrregularities);
You also don't need to loop through your irregularities if what you're doing is creating a concatenated string of them.
Another way to do it (which may be clearer):
string irregularities = String.Join(Environment.NewLine, SeqIrregularities);
string displayString = " Seq" + Environment.NewLine + irregularities;
textBox1.Text = displayString;
change your code to this:
private void button7_Click(object sender, EventArgs e)
{
if (seq1)
{
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
foreach (object o in SeqIrregularities)
{
textBox1.Text += String.Join(Environment.NewLine, SeqIrregularities);
}
}
}
You were overwriting your text in each iteration of your foreach-statement. You have to use += instead of = in your foreach-statement.
I'm creating an RTF Editor and I need help with the search functions. I have already created the find and replace code but I cant figure out how to code the find next and replace all code. Any help will be much appreciated. The following is the code that I have already. ( I am using Visual studio 2010 c# )
private void buttonFind_Click(object sender, EventArgs e)
{
RichTextBox frm1TB = ((Form1)this.Owner).rTB;
int foundAt;
foundAt = frm1TB.Text.IndexOf(txtSearch.Text);
if (foundAt == -1)
{
MessageBox.Show("Not Found");
}
else
{
frm1TB.SelectionStart = foundAt;
frm1TB.SelectionLength = txtSearch.TextLength;
frm1TB.Focus();
btnFindnext.Enabled = true;
btnReplaceall.Enabled = true;
btnReplace.Enabled = true;
}
}
private void buttonfindNext_Click(object sender, EventArgs e)
{
}
private void buttonreplace_Click(object sender, EventArgs e)
{
RichTextBox frm1TB = ((Form1)this.Owner).rTB;
btnFind_Click(sender,e);
frm1TB.SelectedText = txtReplace.Text;
}
private void buttonreplaceAll_Click(object sender, EventArgs e)
{
}
you can use the this overload of indexOf, define the startIndex as the index of the last result you've found + the length of the search string. now the indexOf will give you the location of the string in txtSearch.Text in the RTF box after the last occurrence.
to replace all just Replace
I think you can just to this:
frm1TB.Rtf = frm1TB.Rtf.Replace("replace what", "with this");
In my page when I call searchBtn_Click the selectedvalue will be carried into the variable ind only if the selection hasnt changed. So if a User selects Automotive, then clicks the search button, and then they change the selection to Government, it will refresh the page and display Automotive, am I missing something in the postback or doing something wrong here?
protected void Page_Load(object sender, EventArgs e)
{
string industry = "";
if (Request.QueryString["ind"] != null)
{
industry = Request.QueryString["ind"].ToString();
if (industry != "")
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
}
}
protected void searchBtn_Click(object sender, EventArgs e)
{
string ind = IndustryDropDownList.SelectedValue;
Response.Redirect("Default.aspx?ind=" + ind);
}
Simply replace your code with this code
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
string industry = "";
if (Request.QueryString["ind"] != null)
{
industry = Request.QueryString["ind"].ToString();
if (industry != "")
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
}
}
}
You don't need to use Redirect and QueryString.
Use SelectedValue at Page_PreRender (In your sample clear Page_Load completely).
you better try this in search button click
but remember your dropdowndlist's value-member==display-member to do this.. i had the same problem and this is how i solved it.
string ind = IndustryDropDownList.Text.Tostring().Trim();
Response.Redirect("Default.aspx?ind=" + ind);
i knw this is not the best way but it did work for me..
You're not leveraging the ViewState of asp.net forms (good mentality for MVC 3 though). But since you are using asp.net, you should change your code to this:
The logic in your page load is not necessary, unless you want the user to set the industry as the enter the page. Since I assumed you do, I left some logic in there. It checks for postback because it doesn't need to execute after the initial page load.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack() && Request.QueryString["ind"] != null)
{
SetIndustry(Request.QueryString["ind"].ToString());
}
}
protected void SetIndustry(String industry)
{
indLabel.Text = "Industry: " + industry;
IndustryDropDownList.SelectedValue = industry;
}
You don't have to redirect the page, since Page_Load will be called every time the page posts back. With .NET, your controls remember their last values automatically.
protected void searchBtn_Click(object sender, EventArgs e)
{
SetIndustry(IndustryDropDownList.SelectedValue);
}