(Original text updated!)
I am slowly getting into ASP.NET, currently trying to make a small webforms app for a hackathon.
The page that I am having problems with is supposed to show a list of companies to which person can apply.
Since the list of those companies can change, company always can be added or removed, I want to make some kind of table which loads values dynamically. The table appears when a user clicks "View all companies", it shows a table with each row including the name of company and a button to move to the page of this company. At this stage, I am trying to make something even more simple, for the sake of testing.
So, the button should just display some kind of text.
But here's the problem.
When I click on the button, it just resets the page and as I understand, runs "Page_Load". The text is also resets to the value from Page_Load.
Now, I'll try to give code and try to give the best descriptions.
Here's the Page_Load, on first load it just displays the name of the user, and hides the table with company names. On non-first load, it should run click button events.
protected void Page_Load(object sender, EventArgs e)
{
HRGlobalHub.Code.Start.Firebase.thisFirebaseClient = new FireSharp.FirebaseClient(HRGlobalHub.Code.Start.Firebase.thisFirebaseConfig);
pnlCompaniesView.Visible = false;
if (!IsPostBack)
{
if (thisUser == null)
lblWelcomeUser.Text = "Welcome";
else
lblWelcomeUser.Text = "Welcome, " + thisUser.FirstName + " " + thisUser.LastName;
gCompany = new Code.Company.Company();
}
else
{
if (Action=="Apply")
btnApplyClick(this, e);
else if (Action == "Suggest")
btnSuggestClick(this, e);
if (Action == "View")
btnViewClick(this, e);
}
}
The code for showing all companies comes here:
protected async void btnViewAllCompanies_Click(object sender, EventArgs e)
{
ResetCompaniesTable();
lblWelcomeUser.Text = "ViewAll";
FirebaseResponse response = await HRGlobalHub.Code.Start.Firebase.thisFirebaseClient.GetTaskAsync("HRGlobalHub/Database/Companies/CompaniesList/");
HRGlobalHub.Code.Company.CompaniesNamesList thisData = response.ResultAs<HRGlobalHub.Code.Company.CompaniesNamesList>();
string[] CompanyArray = thisData.CompanyNameListVar.Split(',');
for (int i = 1; i < CompanyArray.Length; i++)
{
response = await HRGlobalHub.Code.Start.Firebase.thisFirebaseClient.GetTaskAsync("HRGlobalHub/Database/Companies/CompaniesDatabase/" + CompanyArray[i]);
HRGlobalHub.Code.Company.Company result = response.ResultAs<HRGlobalHub.Code.Company.Company>();
tbtTestTable.Rows.Add(NewCompanyRow(result));
}
pnlCompaniesView.Visible = true;
pnlCompaniesView.Width = 1100;
tbtTestTable.Width = 1100;
lblWelcomeUser.Text = tbtTestTable.Visible.ToString();
}
This part loads first just the names of companies, then full companies data from the server. Each company turns into a table row. It also creates OnClientClick button events, that should load on non-first Page Load.
public TableRow NewCompanyRow(HRGlobalHub.Code.Company.Company thisCompany)
{
TableRow newRow = new TableRow();
Button btnApply = new Button();
btnApply.Text = "Apply";
btnApply.OnClientClick += gCompany = thisCompany;
btnApply.OnClientClick += Action = "Apply";
Button btnSuggest = new Button();
btnSuggest.Text = "Suggest";
btnSuggest.OnClientClick += gCompany = thisCompany;
btnSuggest.OnClientClick += Action = "Suggest";
Button btnView = new Button();
btnView.Text = "View";
btnView.OnClientClick += gCompany = thisCompany;
btnView.OnClientClick += Action = "View";
TableCell[] Cells = new TableCell[6];
for (int i = 0; i < Cells.Length; i++)
Cells[i] = new TableCell();
Cells[0].Text = thisCompany.Name;
Cells[1].Text = thisCompany.FieldOfWork;
Cells[2].Text = thisCompany.Employees.Count.ToString();
Cells[3].Controls.Add(btnView);
Cells[4].Controls.Add(btnApply);
Cells[5].Controls.Add(btnSuggest);
for (int i = 0; i < Cells.Length; i++)
Cells[i].Width = 150;
newRow.Width = 900;
for (int i = 0; i < Cells.Length; i++)
newRow.Cells.Add(Cells[i]);
btnApply.Width = 150;
btnSuggest.Width = 150;
btnView.Width = 150;
return newRow;
}
Again, since it's just testing stage for this page, the button should only display the name of the company that it gets as variable.
void btnApplyClick(Object sender, EventArgs e, HRGlobalHub.Code.Company.Company thisCompany)
{
lblWelcomeUser.Text = "Apply to " + thisCompany.Name;
}
This works but only partly: whatever I try the code from Page Load always runs the last "mentioned" Action (which is "View") and always goes for the company from the last row.
Please help me solve this!
Evgenie