|
-
Nov 17th, 2003, 05:35 AM
#1
Thread Starter
Junior Member
Identifying the Button clicked in Page_Load
I have a webpage with two buttons. In the LOAD event of my page, I want to identify which button has been clicked. How do I do this? I'm currently having problems because when I click a button, the LOAD event is fired first, before the button_click event.
Thanks.
-
Nov 24th, 2003, 05:28 PM
#2
Hyperactive Member
Hi, I'm not too sure exactly what you are after but have you thought of using the button click handler to capture the ID of the button that was clicked and then adding that ID to viewstate? You could then request the id from the viewstate in the page load event and this would tell you what button was clicked...
VB Code:
public function button_clicked(sender as object, e as eventargs) handles button1.click, button2.click
viewstate.add("ButtonID",sender.id.toString())
end function
' then in the page load event
response.write("The button " & viewstate("ButtonID") & " was clicked")
Hope this gives you some idea
Cheers
MarkusJ
-
Nov 24th, 2003, 11:16 PM
#3
Thread Starter
Junior Member
hi markus! thanks for replying. anyway, like i've said, the problem is that the load event is fired first before the button_click event. thus, in the load event of your code below, ViewState("ButtonID") will return Nothing since the program has not yet processed the button_click event.
-
Nov 25th, 2003, 12:17 AM
#4
Hyperactive Member
So...I almost replied with just a hack but figured I'd go another direction. What you're trying to do, kinda, is make the page event lifecycle fit your app. What I think you should do is make your app fit the page lifecycle. Code in the Page.OnLoad eventhandler shouldn't depend on which button clicked. That's the whole point of having event handlers for your buttons. Just my two cents, i'd be pissed if someone on my team snuck in a "which button was clicked"-dependency in the page load. I'd at least expect them to stick some comments around that piece fully explaining why they did it.
Ok, in the spirit of trying to provide answers to the original questions and not just posting opinions, here's my hack. I'm working on a project right now where the design team(the front end html folks) came up with a modal dialog box framework. It really sux, way overly complicated, really the only person who fully understands it is the guy who wrote it. Anyway, when it pops up the window, it's really a frameset, the buttons go in one frame at the bottom of the window and the main aspx piece goes into another frame. The buttons aren't server side buttons and even if they were they're in another window, actually dynamically generated in javascript so they're not server controls. So I cooked up a lame hack so that i could still use button event handlers that were kicked off by plain old school html buttons in another frame. Maybe you can pick it apart and use some of it or none of it, i really don't like it and favor writing good code if you're able to. The concept is absolutely the exact same one the .net uses itself, set a hidden variable indicating what control caused the event and then post the form, on the server side then determine who clicked what(for simplicity all of the code(server side and clientside) is in one page):
Code:
<script runat="server" language="C#">
protected override void OnLoad(System.EventArgs e)
{
if (Request.Form["__ButtonClicked"] != null)
{
switch(Request.Form["__ButtonClicked"].ToLower())
{
case "btnsave":
//do some stuff
btnSave_Click(null, null);
break;
case "btndelete":
//do some stuff
btnDelete_Click(null,null);
break;
default:
break;
}
}
}
private void btnSave_Click(object sender, System.EventArgs e)
{
Response.Write("Saving...");
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
Response.Write("Deleting...");
}
</script>
<script language="javascript">
function __post(button)
{
buttonClicked = document.getElementById("__ButtonClicked");
if (buttonClicked)
{
buttonClicked.value = button.id;
document.forms[0].submit();
}
}
</script>
<html>
<body>
<form runat="server">
<input type="hidden" name="__ButtonClicked" id="__ButtonClicked"/>
<input type="submit" name="btnSave" id="btnSave" onclick="javascript:__post(this)" value="Save"/>
<input type="submit" name="btnDelete" id="btnDelete" onclick="javascript:__post(this)" value="Delete"/>
</form>
</body>
</html>
-
Nov 25th, 2003, 01:44 AM
#5
Thread Starter
Junior Member
hey pvb, thanks. that works. and thanks for your comments. much appreciated.
anyway, the reason why i decided to implement this kind of code is basically because i was having problems saving the state of a dynamically generated table. This table was displayed in my page by using "myLabel.Controls.Add(myTable)". the problem with this is whenever i refresh the page, the contents of myLabel disappears (myTable becomes blank). so i decided to generate myTable on the page_load event instead of the button_click event so that it will be generated everytime the page is refreshed (and thus came the need to identify which button was clicked on the load event).
but anyway, i already found a solution to this. what i did was this: instead of using the method myLabel.Controls.Add(myTable) to display the table, i used myLabel.Text = <HTML code of myTable>. When I did this, refreshing the page does not remove the contents of myLabel anymore. So now, I will be placing the table-generation code back to the button_click event instead of the page_load event.
I don't exactly know why the Table does not save it's state when I use the Controls.Add() method. But what the heck, I'm glad that I got my code working now!
-
Nov 25th, 2003, 04:24 AM
#6
Hyperactive Member
General rule of thumb for any dynamically generatred controls: they must always be re-added EXACTLY the same way on every subsequent page request BEFORE viewstate is processed. Best place to do this is in the Page.OnInit(or it's eventhandler). Since i don't really know what your dynamically gend code looks like I made some up, here's the result(the dyno textbox control maintains it's value on postback):
Code:
<script language="C#" runat="server">
protected TextBox txtFirstName;
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
//Recreate dynamically generated stuff here, the same way
//every time(control creation order must be exact in order
//for viewstate to work properly).
HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
txtFirstName = new TextBox();
txtFirstName.ID = "txtFirstName";
cell.Controls.Add(txtFirstName);
row.Cells.Add(cell);
table.Rows.Add(row);
lblTableHolder.Controls.Add(table);
}
protected void btnSave_Click(object sender, System.EventArgs e)
{
Response.Write("Saving..." + txtFirstName.Text);
}
protected void btnDelete_Click(object sender, System.EventArgs e)
{
Response.Write("Deleting..." + txtFirstName.Text);
}
</script>
<html>
<body>
<form runat="server">
<asp:Label ID="lblTableHolder" Runat="server"/><br/>
<asp:Button ID="btnSave" Runat="server" Text="Save" OnClick="btnSave_Click"/>
<asp:Button ID="btnDelete" Runat="server" Text="Delete" OnClick="btnDelete_Click"/>
</form>
</body>
</html>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|