Postback in Web Controls

W

Wizzy

I have a encapsulated a Datagrid control and text boxes etc to make a
customized datagrid control.

I have a "search" button on the page. If the user clicks on Search or even
if the "Search " button is the currently selected button and the user presses
"Enter" key the postback occurs and the control is rebound, but if the user
simply enters text in the search criteria and hits the "Enter" key, the
postback event is not thrown and the control loses it's data.

I want to prevent this from happening.

Thank you,

Wizzy
 
P

Phillip Williams

There are 2 options; both using Javascript by trapping the event OnKeyPress
when the Enter key is pressed during typing in the TextBox:
1- event.returnValue=false; which basically disables the effect of pressing
the Enter key
2- Trigger the submit button click event

You may view the code for demo# 9 from my samples:
http://www.societopia.net/samples/
 
W

Wizzy

Hi Phillip,

Thanks for your reply. This should take care of the first situation.

I also have a series of radio button controls with AutoPostBack set to true.
Again the application works fine, but if I click an already selected radio
button (or click on a selected radio button) the same thing happens .

Thanks in Advance,

Wizzy
 
P

Phillip Williams

You are welcome.

Regarding your question on the RadioButtonList...

ASP.NET creates an onClick event for the AutoPostBack=true setting on the
RadioButton and the RadioButtonList, which means that a RadioButton will fire
up on every click regardless of the change in status (checked/unchecked).

To achieve the effect that you want, I would replace the RadioButtonList
with individual RadioButtons that are wired to the same event handling
method, e.g.
<asp:RadioButton AutoPostBack=True ID="Radiobutton1" Runat="server"
Text="value1" OnCheckedChanged
="Radiobutton_CheckedChanged"></asp:RadioButton>
<asp:RadioButton AutoPostBack=True ID="Radiobutton2" Runat="server"
Text="value2" OnCheckedChanged
="Radiobutton_CheckedChanged"></asp:RadioButton>
<asp:RadioButton AutoPostBack=True ID="Radiobutton3" Runat="server"
Text="value2" OnCheckedChanged
="Radiobutton_CheckedChanged"></asp:RadioButton>

And in the codebehind:

protected void Radiobutton_CheckedChanged(object sender, System.EventArgs e)
{
//disable the autopostback for the selected radio button
RadioButton rb = (RadioButton)sender;
rb.AutoPostBack =false;
//uncheck the other radio buttons
}
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top