How to get default button to work?

T

tshad

I have a page with 2 buttons and was told I could get my choice of button to
act as the default button but add the following code to the my Page_Load:

Page.RegisterHiddenField("__EVENTTARGET", "btnSearch")

I have the following code and page:

****************************************************
<script runat="server">
Sub Page_Load(s as Object, e as EventArgs)
if not IsPostBack then
Page.RegisterHiddenField("__EVENTTARGET", "Button2")
end if
end sub

Sub Button1_Click(s as Object, e as EventArgs)
Label1.Text = "From Button 1"
end Sub

Sub Button2_Click(s as Object, e as EventArgs)
Label1.Text = "From Button 2"
end Sub

</script>

<form runat="server">
<table width="851" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>_
<asp:Label ID="Label1" runat="server" />
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
<asp:Button ID="Button1" Text="Button1" OnClick="Button1_Click"
runat="server" />&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" Text="Button2" OnClick="Button2_Click"
runat="server" />
</form>
********************************************************************************************************************************

But it doesn't do anything until I press one of the buttons with my mouse or
tab to a button.

Is there a way to get this to work?

Thanks,

Tom
 
B

bruce barker \(sqlwork.com\)

because there are no autopostback fields on your form, the server does not
process the "__EVENTTARGET" hidden field as buttons do not require the
hidden field. you need to fake the server out by calling:

Page.GetPostBackEventReference(this);


-- bruce (sqlwork.com)
 
T

tshad

bruce barker (sqlwork.com) said:
because there are no autopostback fields on your form, the server does not
process the "__EVENTTARGET" hidden field as buttons do not require the
hidden field. you need to fake the server out by calling:

Page.GetPostBackEventReference(this);

Where would I put this?

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top