is posible to excute codebehind Event first while dropdown change - asp.net

I have code like
<asp:DropDownList ID="SiteIDDropdownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SiteIDDropdownList_OnSelectedIndexChanged" onChange="javascript:markItUpApply();">
</asp:DropDownList>
Here I need fire first "OnSelectedIndexChanged" then "onChange"
is It possible?

"onChange" is a client side event whereas "OnSelectedIndexChanged" is a server side asp.net event, there is no way to fire these in a different order.
Try moving the code from onChanged into the OnSelectedIndexChanged event, of course you will need to convert it to .net code

You could remove the onchange attribute and register a startup script that will fire after the postback has completed which i think in effect will work just as you want.
In your OnSelectedIndexChanged Event you can use.
Page.ClientScript.RegisterStartupScript(this.GetType(), "testScript", "markItUpApply();", true);
EDIT:
In case you have update panel in the page then use this code
ScriptManager.RegisterStartupScript(this, GetType(), "testScript", "markItUpApply();", true);
hope this helps.

Well the case is that both are events and will fire when dropdown list onselected. But if you want to delay your codebehind call you can give this a try:
System.Threading.Thread.Sleep(2000);
But it will give you a delay for 2 sec and its not the best practices at all.

Related

ASP: ontextchanged not firing

Note: I am brand new at ASP.NET. I'm actually going to training on Monday, but we have a hot project that required me to dive in and get done as much as I can.
I've got a textbox on my page, and I would like it to call an event on the server whenever it is changed. I don't care if "changed" means changed and loses focus, or just whenever a keyup happens. Either case will work.
The issue is that, for some reason, whenever I change my textbox and then remove focus, the server-side method is not getting called. Viewing through the debugger in Chrome, I don't even see any sort of AJAX call being made that would inform the server that a textbox was changed.
My Code:
ASCX File
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="tempTagBuilder" runat="server"
CssClass="depBuilder_tempTagBuilder"
ontextchanged="tempTagBuilder_TextChanged" AutoPostBack="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
ASCX.cs File
//whenever the text is changed
protected void tempTagBuilder_TextChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Hit");
}
Anyone have a good idea of what my issue might be?
Update 1:
I got it working (somewhat). I had to go into the updatepanel's properties and add the textchanged event to the triggers collection. However, now whenever it sends the update it is emptying out the other textboxes on my page!
It doesn't fire because it's in an update panel most likely. Seems the updatepanel requires you to set a trigger for the event.
Problem with textbox inside updatepanel - not causing OnTextChanged event

I want iif conditiomn in my radio button

I want to put if condition in my radio button.
I have wrote this code but it does not give me result as I want. you will get idea from my code what actual I need to do. I have loaded user control two time in a single page so I want to call Java Script in page according to control.
<asp:RadioButton runat="server" GroupName="Pricing" class="2deditable iscreatedbydealer isinprivatelabel" onClick='<%this.ID=="ucPricing_Details_Sale"? "setSalePopupRetailPrice();":"setClearancePopupRetailPrice();"%>'
ID="rbManual" />
I think you can dynamic add the onclick event for the control in page load event of server side.
e.g
if("ucPricing_Details_Sale".Equals(this.ID))
{
this.rbManual.Attributes.Add("onclick", "setSalePopupRetailPrice();");
}
else
{
this.rbManual.Attributes.Add("onclick", "setClearancePopupRetailPrice();");
}
The on OnClick event will run on server side. Try using the OnClientClick event instead.
Edit: Deleted my last statement. Mistook me about the context of the call.
Also added code sample:
<asp:RadioButton runat="server"
OnClientClick='<%this.ID=="ucPricing_Details_Sale"?[...]"%>'
ID="rbManual" />

gridview delete command within updatepanel, and separate __doPostback() functionality

I have a gridview within an updatepanel, the rows have a delete button which removes that row.
Elsewhere, I run code to insert a row. Following this insert I run __doPostback() with the ID of the updatepanel, then in the updatepanel's load() event I call databind() on the gridview.
As soon as I implement the __doPostback() and databind, the inbuilt gridview delete stops working! :( The actual refresh/databind when adding the row works well.
How can I overcome this? I guess something may be awry in that when clicking on the delete button, the databind is conflicting with the inbuild delete/refresh functionality?
Thanks!
EDIT: Apologies if the question isn't described well...
Essentially, I wish to have a gridview with built-in delete functionality through the datasource and command column etc. inside of an updatepanel. I also want to update this panel seperately, but when I put in this separate update code (gridview.databind in the updatepanel.load) it breaks the standard delete functionality. Hope that is clear :)
You've tried put the UpdatePanelMode as Conditional and use UpdatePanel.Update() besides the ClientScript.RegisterStartupScript during your insertion block ?
I believe the problem is that the event is inside of the GridView and you can't access them as easily as you can with something like a Button. To register the GridView to make the Async events you need to attach it to the ScriptManager.
To do this you use the RegisterAsyncpostBackControl method.
Here is a general idea of how to do it.
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server">
<%-- your fields, etc --%>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
In your code behind you do
protected void Page_Load()
{
ScriptManager1.RegisterAsyncPostBackControl(GridView1);
}
It's been a while since I've done this but I believe this will allow the GridView to function as you'd expect, except you don't need the extra DataBind() I don't believe in this case.
You can also set the UpdatePanel to Conditional and fire an UpdatePanel1.Update() on top of this as Jeison suggested.
You can find some added details at http://msdn.microsoft.com/en-us/library/bb386452.aspx
If you still have trouble, let us know what happened.
It seems like you calling DataBind() of GridView every time the UpdatePanel load after the insert button click, it reload the data before delete reaches the DataSource.
EDIT
If so, you can add boolean eventArgument in __doPostBack(updatePanelId, "true"). And using this you could add a condition in your updatepanel load event like
if(this.updatepanel1.Page.Request.Params["__EVENTARGUMENT"] == "true"]
this.gridview.databind()
Hope this will resolve the issue.

UpdatePanel and UpdateProgress not working

If I use: OnSelectedIndexChanged like this:
<asp:DropDownList ID="ddl1" AutoPostBack="true" OnSelectedIndexChanged="Test_SelectedIndexChanged" runat="server"></asp:DropDownList>
UpdatePanel and UpdateProgress work correctly, meaning it shows my little gif etc.
However as soon as I change this to call javascript code, like this:
<asp:DropDownList ID="ddl1" AutoPostBack="true" onchange="selectValues()" runat="server"></asp:DropDownList>
It stops working. The progress doesn't show up. Now, before anyone asks why do I this, it's because I need to call some scripting into the managed code. It has to do with silverlight.
Does anyone have solution to this problem?
if your update panel doesn't refresh, the updateprogress control will not operate. if you try to update something without calling the update of the updatepanel (ie using your own JS) the updateprogress will not work.
I would guess it's because the progress is hooked up to show when the UpdatePanel is updated.
Does your second drop down trigger the update panel when you select from the list?
You may have to add a OnSelectedIndexChanged event to your drop down that does nothing to trigger the update panel.
You could add some javascript in yout SelectValues() function to show the progress panel, i believe it is simply a div with an image that you could change the css using javascript to visible.
Hope that helps!
I think your javascript is returning false value. So the server side event of dropdown selectedindex change event does not fire as it it not postback whole page.

ASP.NET CheckBox disabling postback with javascript

I'm trying to wire up a CheckBox to handle an event when check/unchecked. If the user has JavaScript enabled, use that, otherwise use a postback.
Here is my code:
<asp:CheckBox ID="ApplicationInProcessCheckBox" runat="server"
Text="Application In Process" AutoPostBack="true"
oncheckedchanged="ApplicationInProcessCheckBox_CheckedChanged"
onclick="return false;" />
The return false in the javascript onclick event is disabling the postback. However, it also won't let the box check or uncheck. (I have more code to add to the javascript event... I just want to get the concept working first).
What am I doing wrong?
I think we can't post back on clicking checkbox without Javascript enabled.

Resources