A little help in asp.net C# jumping from method to method - c#

My problem is that I can get from page_load to show_books function
but I cant get from show_books to book_detail.
Every time I try to fire book_details it gets me to page_load.
my code is this:
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(book_show);
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(book_details);
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}
I'm sorry if my question is annoyingly newbie
but I would like some help .
I just started to learn asp.net

well i don't understand why you need to do this,but code below works,hope it helps
protected void Page_Load(object sender, EventArgs e)
{
//Here I create dynamic linkbuttons that calls datashow
lnk_button.Command += new CommandEventHandler(this.book_show);
lnk_button.CommandName = "testClick";
lnk_button.CommandArgument = "1";
lnk_button.ID = "11";
lnk_button.Text = "blah";
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_show(object sender, EventArgs e)
{
//Here I show all books from a category
//and I create dynamic linkbuttons that calls book_details
book_button.Command += new CommandEventHandler(this.book_details);
book_button.CommandName = "testClick2";
book_button.CommandArgument = "2";
book_button.ID = "22";
book_button.Text = "blah2";
}
protected void book_details(object sender, EventArgs e)
{
//and Here I show the details of each book
}

public void book_show()
{
book_details() ;
}
public void book_details()
{
}

Related

WebView events not firing in Xamarin.Forms

I want to get the HTML from a WebView page, so I make use of the EvaluateJavaScriptAsync function to get the body. However, this throws an NRE, probably because I am fetching the document before the page has loaded. Hence, I add a lambda expression to the Navigated
event.
var wv = new WebView();
wv.Source = "https://www.bbc.com/news";
wv.Navigated += async (s, e) =>
{
var x = await wv.EvaluateJavaScriptAsync("document.body.innerHTML");
Debugger.Break();
};
However, the execution never breaks, i.e. the Navigated event is not getting fired. I've tried other events, and realised the Navigating event also never fires. This doesn't work for any other website as well. Any idea of what's going on?
Alternatively, is there any other way to do dynamic web scraping in Xamarin that wouldn't be too computationally expensive on mobile? Thanks.
Here is a demo about getting the html from webview page by using CoreWebView2.ExecuteScriptAsync().
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.webView2.Source = new System.Uri(textBox1.Text, System.UriKind.Absolute);
}
private async void webView2_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
object obj = await webView2.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");
textBox2.Text = obj.ToString();
}
private async void button2_Click(object sender, EventArgs e)
{
object obj = await webView2.CoreWebView2.ExecuteScriptAsync("document.body.innerHTML");
textBox2.Text = obj.ToString();
}
private async void button3_Click(object sender, EventArgs e)
{
object obj = await webView2.CoreWebView2.ExecuteScriptAsync("document.body.innerText");
textBox2.Text = obj.ToString();
}
private async void button4_Click(object sender, EventArgs e)
{
object obj = await webView2.CoreWebView2.ExecuteScriptAsync("$('.f3')[0].innerHTML");
textBox2.Text = obj.ToString();
}

Problems with Session losing its data in .Net

I have allready asked this question before, but now I have found more details and some more weird stuf.
I have a web project that has 2 .aspx files that connect data with sessions. Without any extra code this works, when i add my extra code the session no longer works. Does anybody have an idea why?
Code where sessions work:
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = 5 when i look while debugging
}
Code where sessions don't work: (i use 2 classlibraries (logic and dataAccess where I get data from json webservice and parse it to my forms).
Form1:
protected void Page_Load(object sender, EventArgs e)
{
Logic logic = new Logic();
logic.login(credentials);
List<AppointmentExtParticipant> opleidingVolgers = logic.getOpleidingVolgers();
foreach (AppointmentExtParticipant app in opleidingVolgers)
{
if (app.contact != null)
{
Relation rel = logic.getRelationData(app.contact.FK_RELATION);
DropDownListUsers.Items.Add(app.ToString() + " " + rel.ToString());
}
}
Session["data"] = "5";
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["opleidingvolger"]); // s = 5 when i look while debugging
Response.Redirect("~/Form2.aspx", false);
}
Form 2:
protected void Page_Load(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
protected void ButtonOk_Click(object sender, EventArgs e)
{
string s = (string)(Session["data"]); // s = null when i look while debugging
}
Ofcourse I simplified the names here a bit so people could understand, thnx!
edit:
logic login:
dataaccess login:
Here i get data from my webservice hosted on another url.
Sessions generally work by using cookies.You are using HttpWebRequests which creates new sessionSo you need to preserve session cookie between requests. Thats why we use CookieContainer so add
CookieContainer container = new CookieContainer();
HttpWebRequest req = WebRequest.Create(
"") as HttpWebRequest;
req.CookieContainer = container;

Asp.net c# button clicked event does not add new string to arraylist

I am creating shopping cart and I am having problems to add new string to arraylist. It only adds once. I have tried declare arraylist in load page method and also tried to assign to session variable, as in code bellow.
Result: I get only one string in arraylist even button been pressed manytimes.
ArrayList test;
protected void Page_Load(object sender, EventArgs e)
{
test = new ArrayList();
Session["array"] = test;
}
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList test = (ArrayList)Session["array"];
test.Add("a");
label1.Text = test.Count +"";
Session["array"] = test;
}
Is because in every postback your Page_Load method is called, so test is always set as new ArrayList();
try this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
test = new ArrayList();
Session["array"] = test;
}
}

Asp.net Session state Trying to keep contents of textbox when returned to page

ive two pages, one with a text box and a button, the other with a button a label. What i want to do is to display contents of the textbox on page 1, in the label of the page2 on button click. and then when i click the button to return to page1. preverse whats entered in the textbox on page1. sorry if its confusing. heres my code
page1.aspx
protected void Button1_Click(object sender, EventArgs e)
{
Session["fstName"] = txtBox.Text;
Response.Redirect("Page2.aspx");
}
page2.aspx
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
lblPage2.Text = a;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("WebForm1.aspx");
}
Where do you set the value of the text box when returning to WebForm1.aspx? It should be very similar to what you have for the label on Page2.aspx. Something like:
protected void Page_Load(object sender, EventArgs e)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
At worst, you may need to wrap some error checking around it. Maybe something like:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
if (Session["fstName"] != null)
{
string a = Session["fstName"].ToString();
txtBox.Text = a;
}
}

Display a message in the font type and size selected by the user from two listboxes

I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}

Categories

Resources