AspxCallback is not updating information - c#

I have an AspxCallback control that is supposed to update textbox text when i click the Button. But nothing happens when I click the button.
Here is my sample code for the test:
C#:
protected void callback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
{
txtTest.Text = "Text for Textbox";
}
ASP.NET:
<asp:Button ID="btnTest" runat="server" Text="CLICK" OnClientClick="callback.PerformCallback(); return false;" />
<br />
<asp:TextBox ID="txtTest" runat="server" Width="200" Height="25"></asp:TextBox>
<dx:ASPxCallback ID="callback" runat="server" ClientInstanceName="callback"
oncallback="callback_Callback">
</dx:ASPxCallback>

"Your problem resides on the fact that the TextBox is not inside a CallBack Panel.
The way a callback works is like an ajax call that can update only the Ajax enabled so to say controls. Those controls can be put inside a callback panel for this exact reason.
<dxcp:ASPxCallbackPanel ID="ASPxCallbackPanel1" runat="server" Width="223px" BackColor="#FFFFC0" ClientInstanceName="callbackPanel1" Height="78px" oncallback="callback_Callback">
<PanelCollection>
<dxp:panelcontent runat="server">
<asp:Button ID="btnTest" runat="server" Text="CLICK"
OnClientClick="callbackPanel1.PerformCallback(); return false;" />
<br />
<asp:TextBox ID="txtTest" runat="server" Width="200" Height="25"></asp:TextBox>
</dxp:panelcontent>
</PanelCollection>
</dxcp:ASPxCallbackPanel>
I think this will solve your problem. Now your code will update the TextBox properly.

Related

How to execute event when user presses return on web page

I have an ASP.NET website and I have a forgot password page where the user enters their email address in a text box and when they click the button to retrieve password, the event runs as fine.
Only problem is if the user types in their email address and presses ENTER instead, it runs a search (I have a search bar at the top of the page) and so the result comes back as 'search query not found'. But this search bar at the top is on a different ASP.NET page.
So anyway, I want the event onclick to run when the user presses enter and not run a search query. Does anyone have any ideas? I've searched on this site but not really found the answer I need.
There are couple ways you can do this. If it is a webform and you have your textbox wrapped with an asp:panel you can do as below:
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
If its not a webform or you want to move away from that try below:
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
And in the codebehind fire your button click in the page load based on the parameters of the postback.
Specify the "defaultbutton" property to the ID of (event you want to fire).
You can specify the "defaultbutton" property at the Form level (in the form tag)
Or else you can define them at panel level in the tag.
The form level setting is overridden at the panel level setting.
The Event Handler for the specified button gets fired simulating a true submit button functionality.
<form id="sampleform" runat="server" defaultbutton="button1">
<div>
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<asp:Button ID="button2" runat="server" Text="Cancel" OnClick="Button2_Click" />
<asp:Button ID="button1" runat="server" Text="Ok" OnClick="button1_Click" />
<asp:Panel ID="panel1" runat="server" defaultbutton="Button5">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Button5" OnClick="Button5_Click" />
</asp:Panel>
</div>
</form>
In this example, Button1 is the default button for the form (Type something in Textbox 1 and hit enter, button1_Click gets fired). But for "Panel 1", default button will be Button 5 (Type in Textbox3 or Textbox 5 and hit enter).
You can have any number of panels with different default button for each panel.
Adding an ASP Panel around the text box and button with the id of the button name as the property for DefaultButton was the best way to do this.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>

asp:Button and asp:LinkButton and asp:Button submitting on enter in Chrome

In Internet Explorer if I hit enter in the TextBox P it submits the Form using the onclick event of LoginButton.
This is what I want to happen.
In Google Chrome if I hit enter it submits the form with the onclick of Button1.
This is not what I want to happen.
Button1 is actually not visible on the form under normal circumstances. It is made visible under certain circumstances to do a different task then login.
How can I force this, browser independently, to always use LoginButton onclick when someone presses enter?
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
onclick="LoginButton_Click" />
<asp:Button
ID="Button1" runat="server"
Text="Submit" onclick="Button1_Click" />
You set the forms default button:
<form id="Form1" defaultbutton="SubmitButton" runat="server">
The Following works for me.
<form id="form1" runat="server" defaultbutton="LoginButton">
<div>
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
OnClick="LoginButton_Click" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" > </asp:button>
</div>
</form>
You'll probably want to set a default button in your Page_Load method, like this:
this.Form.DefaultButton = this.LoginButton.UniqueID;
I ended up using C# in my Page_Load(object sender, EventArgs e) method to set the DefaultButton. Found this on another stackoverflow post.
https://stackoverflow.com/a/2213237/2907463
this.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;

ASP.NET - Validating Form Before Standard Validation

I have an ASP.NET form where I have an ImageButton control as follows (please note that this only part of a bigger form):
<asp:CheckBox runat="server" ID="checkbox1" Text="Validate" Enabled="false" />
<asp:TextBox runat="server" ID="textbox1" MaxLength="30" />
<asp:RequiredFieldValidator ID="validator1" ControlToValidate="textbox1" Text="required!" runat="server" ValidationGroup="group1" />
<asp:ImageButton ID="button1" runat="server" Width="24" ValidationGroup="group1" />
Using Javascript, I could disable and enable the RequiredFieldValidator as follows:
function test() {
var checkbox1 = document.getElementById("<%=checkbox1.ClientID %>");
var ckOwnerIsOperator = document.getElementById('<%=ckOwnerIsOperator.ClientID %>');
if (checkbox1.checked)
ValidatorEnable(validator1, false);
}
What I am looking for is an event that fires before the validation on the button when the button is clicked. I know I could do this on checkbox click event, but it is not what I need. The checkbox click event has a problem where I lose the validator state if another control triggered an async postback, so I want the test() function to be called before the page validation occurs.
Thank you all
You could add onclientclick="test()" to the button.

very simple c# submit button

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either
TOP of the page
<div style="position:absolute; left: 70% ; top: 5%;">
<asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg" PostBackUrl="http://www.facebook.com/832586561" />
<asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />
<asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />
<asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />
</div>
Middle of Page
<div ID="formPanel" runat="server" style="position:absolute; top:235px;">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
</div>
You may set the default button via <form/> attribute.
<form defaultbutton="btnChangeCity" id="form1" runat="server">
...
</form>
Or use Panel control to set default button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>
You surround your controls with <div> which is a good idea, but to have it run at all you also need <form>. In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.
Read more here.

ListView ItemCommand Event Not Firing in Firefox

I am dynamically adding buttons to a listview and using the ItemCommand event to handle the button click event using the CommandName property for the button. It works fine in IE, but when I try in Firefox 5, it is hitting the page load event but not the ItemCommand event. Is there a work-around for Firefox?
Thanks!
<asp:ListView ID="lvItems" runat="server" OnItemDataBound="lvItems_ItemDataBound"
DataSourceID="odsItems" OnItemCommand="lvItems_ItemCommand" DataKeyNames="ItemID"
OnDataBound="lvItems_DataBound" OnPagePropertiesChanging="lvItems_PagePropertiesChanging">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server">
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<asp:Label ID="lbl" runat="server">
</asp:Label>
<asp:Button ID="btnAdd" runat="server" CommandName="Add" Text="Add" OnClientClick="this.disabled=true;" />
</div>
</ItemTemplate>
<EmptyDataTemplate>
No items found for the selected filters. Please try again.<br />
<br />
</EmptyDataTemplate>
</asp:ListView>
protected void lvItems_ItemCommand(object sender,ListViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
//code here;
}
}
You have to set UseSubmitBehaviour to false, because disabling a button on clientside will cancel the browsers submit. By the way, in IE it's exactly the same.
<asp:Button ID="btnAdd" runat="server" CommandName="Add" Text="Add"
UseSubmitBehavior="false" OnClientClick="this.disabled='true';" />
On this way ASP.NET will append the necessary client-script to postback at the end of your script:
__doPostBack('btnAdd','')
http://encosia.com/disable-a-button-control-during-postback/
OnclientClick and OnClick is not working at the same time?

Categories