How to generate fake postback with javascript? - asp.net

I have Dropdownlist on my page and its selectedindexchanged method created in code behind file (.cs).
I wanted to create fake postback with A tag (onmouseover event).
First i viewed source of html.
<select name="ctl00$cpholder_ana$ddlFaturaNolar" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)" id="ctl00_cpholder_ana_ddlFaturaNolar">
<option selected="selected" value="CHOOSE"></option>
<option value="001926">[ 30.04.2009 - 156.492,00 TL ] 001926</option>
</select>
Then, I copied
onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)"
And, I created A tag with mouseover event(to make Postback but as it fired by Dropdownlist)
<a onmouseover="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)">asdasdasdasdad</a>
But it didn't drop to SelectedIndexChanged method.
First, WHY?
Second, How can i do this?
Thanks from now.

You can use this code snippet -
__doPostBack('<%= dropdownlist.UniqueID %>', '');
You can't use hard-coded unique ids because they may change due to many reasons. For e.g. the id will change if the parent control's id changes, etc. You will have to get the UniqueID rendered from the server side using the code like the one given above.
EDIT: Forgot to mention one important thing. The page will postback only when the selectedIndex of the dropdown changes :) So, if you want to fire that event, change the index of the dropdown list using this and then call the __doPostBack code -
document.getElementById("<%= dropdownlist.UniqueID %>").selectedIndex = 1;
__doPostBack('<%= dropdownlist.UniqueID %>', '');
EDIT2: Adding upon what Bob said, you can make use of hidden server controls. I suggest you use a asp:Hidden control and hook up its OnValueChanged event. So, whenever you want to post your page back to the server, you just have to change the value of your hidden variable. This way you won't have to use a hidden button.
document.getElementById("<%= hiddenField.UniqueID %>").value = (new Date()).getTime();

I am sorry to say guys
__doPostBack is not working for DropDownList but it is working for Button. So do one thing
Add a button which should be followed like this
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="Published" Value="1"></asp:ListItem>
<asp:ListItem Text="Pending" Value="2"></asp:ListItem>
<asp:ListItem Text="Rejected" Value="3"></asp:ListItem></asp:DropDownList>
now call your __doPostBack method
__doPostBack('<%= btnRefresh.UniqueID %>','');

Is it not <%= ddlFaturaNolar.ClientID %>?

Another option you can try, which I find easier and is like likely to break, is to create a hidden (style="display:none" don't use the visible property) asp:Button on your page. When you want to post back you can just simulate the click on that button
document.getElementById("<%= Button1.ClientID %>").click();
If you want to stick to posting back on the drop down, make sure the AutoPostBack property of the dropdown is still set to true. Just keep in mind this behavior is a little strange, you are firing a selected changed event on a mouse over. Not only is this a little confusing, you also are increasing the risk of accidental postbacks, as it is very easy to mouse over something and expect nothing to happen.

Related

Values Entered in a textbox or radio button list changes after firing an event clear when the event posts back?

In ASP.net I have a simple textbox and radio button list with yes or no options. On the VB server-side I implement OnClick of the radio button and it does things.
<asp:RadioButtonList ID="rbltest" runat="server" AutoPostBack="true" OnSelectedIndexChange="rbltest_SelectedIndexChanged" <asp:ListItem Text="Yes" Value="0"></asp:ListItem>
<asp:ListItem Selected="True" Text="No" Value="1"></asp:ListItem>
</asp:RadioButtonList>
The only problem is simply that if I click the radio button, then enter data into the TextBox (immediately - in less than 2 seconds), when the radio button returns, it deletes the data. This is annoying. Is there anyway around it?
The only way to fix this is to remove AutoPostBack=true on the RadioButtonList control. The auto-postback option immediately initiates an HTTP post back to the server, so anything typed after then is going to be lost.
Other options:
Implement a client-side onclick handler, and disable the text while the post-back processes (ie, make it clear to the user that they can't type anything, aka the principle of least astonishment).
Implement the change logic completely on the client side using Javascript
Use the ASP.Net AJAX UpdatePanel to do the postback asynchronously (this would not require a full postback to the server, so would allow you to keep typing).
You must delete EnableViewState="false" on your control who lose data, and let default value of ViewState (true) on control.

ASP.NET - reload a dropdown?

I have a dropdown which shows filesnames and when the index is changed, the slected file is offered for download. I also have a button which creates new files ... now after a new file was created, the new filename should also be shown in the dropdown. It works fine, when I refresh the page, but this is not what I want.
I tried putting the dropdown in an updatepanel and giving it the file create button id, it failed ... is this the correct apporach or is there an easier way?
Thanks!
I just cant get it to work, this is my code:
<asp:UpdatePanel ID="UP_ExportInvoices" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList ID="DDL_ExportFileDownLoad" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="DDL_ExportFileDownLoad_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
I was thinking that if the UpdateMode is set to Always, that the content is always updated? I also have that button (asp:ImageButton) which resides outisde this UpdatePanel. I tried adding a Trigger fpr that button, but it did not work. What am I making wrong. So far, im only thrwoing exceptions or the dropdown is not updated.
Thanks :)
If you are creating the file in the same page, then just append the file name to the dropdown. Can you do this trick in your application?
Does you button posts back the page? If yes, then you need to rebind the drop-down again after you create the file in button click handler.
If button makes partial post-back (say it is placed within UpdatePanel) to the server then above will be still applicable but dropdown should also be in UpdatePanel.
You need to ensure that the Button is a trigger for the Update Panel, or is a child within it.
Here is a full explanation:
http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers
You need to place the button within the UpdatePanel. This will cause a partial postback and the dropdown should re-bind, showing the new item. Alternatively you can include JavaScript in your page which adds the new item to the dropdown list on the client-side, however this can sometimes cause problems with ASP's automatic event validation.

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.

Ajax dropdown limit to list

pretty much what the title says.
I am using an Ajax Drop Down as illustrated here:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/DropDown/DropDown.aspx
using linkbuttons ... is there a way to limit to list?
Thank you.
Edit: I think it was VB 6 maybe that you could select "LimitToList" in a drop down. Meaning the user can only select the values in the drop down and not enter his own data.
Since you're extending a textbox, I think the best option would be to attach an event listener that voids keypresses, you could do this in the ASPX:
<asp:Textbox id="txtFoo" onkeypress="return false;" runat="server"/>
Or, in the code behind:
txtFoo.Attributes.Add("OnKeyPress","return false;");
This will prevent a user from typing in the textbox, essentially creating the effect you want.
A bonus side effect is that a user is allowed to free type an entry if javascript is disabled and the dropdown extender doesn't work.

Resources