I'm using a drop down list for selecting customer, from that i need to remove the setTimeout which i shown in the image, i don't know from where to remove it..can any one suggest me from where it loads..my browser alone show the <select .. >, i need to know from where it loads..
<td><asp:DropDownList Width="180px" CssClass="select_quo_one" ID="ReceiverDropDown"
runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ReceiverDropDown_SelectedIndexChanged">
</asp:DropDownList>
</td>
My Browser Code:
Seems to me you have a few choices -
1) Remove the AutoPostBack - this will stop the SelectedIndexChanged event firing but as you only seem to be popping up an alert you could probably recreate this client side.
2) Put the DropDownList in an UpdatePanel as explained to you in your other question, you may need to do a bit of debugging to get this working but should do what you want.
3) Accept the page reload - accept this is the trade-off when using Web Forms and leave as is.
4) Move to something like MVC which does not have the Postback model.
Sounds like you probably need to do some reading into Web Forms and look at the Pros and Cons and make an informed decision as at the moment you are trying to fight the framework rather than work with it.
The setTimeout is automatically generated by the DropDownList control because AutoPostBack is set to true.
From http://msdn.microsoft.com/en-us/library/system.web.ui.postbackoptions.autopostback(v=vs.110).aspx
The JavaScript setTimeout method is used with the _doPostBack method to ensure that the user action completes before the postback occurs.
Edit:
"flickering occurs in my page so i have to remove the setTimeout value.. – user3595072 17 mins ago"
The setTimeout itself is not (directly) causing the page flickering. The flickering is because the DropDownList is doing a postback every time it is changed bc AutoPostBack is set to true and this causes the page to refresh. If you are not doing anything server side immediately after a customer is selected (like loading other customer info on the page), then you can just set AutoPostBack to false. Otherwise you can look into using an UpdatePanel on your page, or use something like AJAX calls to do your processing without a full postback.
Edit 2:
Since you are only adding javascript within your AutoPostBack event, you can add a jQuery reference (if you do not have one already) by adding the following to your ASPX page:
<script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
Then set AutoPostBack="false" on your DropDownList and add this javascript to accomplish the same thing that your AutoPostBack was doing:
<script type="text/javascript">
$(function () {
$('#<%=ReceiverDropDown.ClientID %>').on('change', function (e) {
var benId = $(this).val();
if (benId != '' && benId != '0') {
AddDetails(benId);
}
});
});
</script>
Related
While creating my login-form, I need a checkbox option, allowing user to show <asp:TextBox TextMode="Password"> input, or hide it back again, but it seems my <asp:CheckBox ID="chkShowPass"> does not process chkShowPass_CheckedChange event. I found an article on this topic (ASP.NET Checkbox.Checked is not working), where chkShowPass.AutoPostBack property set to true settles the problem.
When I have plain ASP.NET example to put <asp:CheckBox ID="chkShowPass"> in, I'm able to do that with no problem, but the key thing, I have now added <asp:UpdatePanel> element to prevent my website pages from auto-scrolling on button-clicks, and to make my good-basket UI (which lies in
Site.Master) update properly from those clicks.
What shall I do, to make my <asp:CheckBox ID="chkShowPass"> react to chkShowPass_CheckedChange event, but, still, keeping my Site.Master's content in <asp:UpdatePanel>? I will also appreciate, if you know any way to prevent page from upscrolling on click, without <asp:UpdatePanel>.
You can do it using jQuery. give id to Password Textbox and a checkbox
<script type='text/javascript'>
$(document).ready(function(){
$('#checkbox').click(function(){
if($(this).is(':checked'))
{
$('#password_id').attr('type', 'text');
}
else
{
$('#password_id').attr('type', 'password');
}
});
});
</script>
Remember to add jquery script in a page.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
I am displaying button on jquery page load event but it is not working. After the page is rendered, it is not visible.
It works fine if i set the visibility in code behind Page Load event.
Jquery
function pageLoad() {
$('#btnSwitchDistributor').css({ 'visibility': 'visible', 'display': 'inline-block' });
}
Html
<asp:Button ID="btnSwitchDistributor" runat="server" Text="Switch Distributor" Visible="false" />
According to MSDN, when you set Visible="false" in your server-side code then the element is not rendered to the client at all. Which means your button isn't "invisble" on the resulting page, it doesn't exist on the resulting page. No JavaScript code can interact with an element that isn't present.
It sounds like instead you want to apply a style to the control:
<asp:Button ID="btnSwitchDistributor" runat="server" Text="Switch Distributor" style="display:none;" />
Aside from that, there are two two potential issues here which I can't necessarily confirm from the content of the question, but which you'll want to check...
The ID value you're looking for is the server-side object's ID. This may not necessarily be the client-side id of the resulting HTML element. Examine the page source to see the client-side id. If it's different then you may need to set it explicitly using the ClientID property.
Do you ever call pageLoad() in your JavaScript code? If not then, well, you'd need to do that. I assume in the document.ready event handler? For jQuery that may be as simple as: $(pageLoad)
Just remove the visible html attribute and the JQuery css.
I have a checkbox in my formview which I want to access in JS to do some enable/disable for textboxes.
This is what I have:
<script type="text/javascript">
$(document).ready(function () {
$('#<%=FormView1.FindControl("chkMap").ClientID%>').change(function () {
if ($(this).is(":checked")) {
}
$('#textbox1').val($(this).is(':checked'));
});
});
With this code, nothing happens and no error is shown in firebug console. Am I doing something wrong? Thanks, Laziale
I have a few guesses. The first one is probably wrong but I had to point it out. The code you posted does not have an end script (</script>) tag. You should check that first.
Second guess is, for some reason <%=FormView1.FindControl("chkMap").ClientID%> this might not be outputting the ID of that control. Have you tried to 'view source' of that page and made sure that the ID is as it should be? Maybe you've changed the ID of that checkbox or something.
Third guess is the way you reference the textbox (#textbox). This is an ASP.NET WebForms page. Your checkbox is a server control (<asp:CheckBox ... />). Are you sure you haven't created the textbox like <asp:TextBox runat="server" ID="textbox1" />? If you have, you would need to write the code like:
$('#<%=FormView1.FindControl("textbox1").ClientID%>').val($(this).is(':checked'));
to reference that textbox control via JavaScript.
I'm suggesting all these because the JS code works fine. I've created a JSFiddle and tested it.
Here's the fiddle. link
I have a form in asp.net webpage which contains 2 drop down lists and a hyperlink + a button.
I want that if user changes an option is dropdowns to change navigate url.
I have done like so:
<asp:DropDownList ID="ddlPackages" AutoPostBack ="true" runat="server"
onselectedindexchanged="ddlPackages_SelectedIndexChanged"></asp:DropDownList>
and then I have the method defined.
The point is that I don't want to make a submit when I change the drop down. Can I do it using only aspx or I have to use javascript?
If I understand you correctly you want to change the href of the hyperlink based on the selected value of the dropdown. You can do this with javascript. Make sure you have AutoPostBack="false" and remove the OnSelectedIndexChanged attribute as well.
To do it using jQuery, use something like this:
<script type="text/javascript>
$(function(){
var $dropdown = $('#<%= ddlPackages.ClientId %>');
$dropdown.change(function(){
//Put logic to decide the link here based on the select value
var newPage = 'page' + $dropdown.val() + '.aspx';
$('#linkId').attr('href', newPage);
});
});
</script>
Edit:
In case you absolutely must have the logic for getting the new url based on the drop down value on the server side, you can turn the method into a Page Method and call it using ajax from the jQuery script above. But you can probably get away with creating the javascript dynamically in the aspx page instead.
I see two options:
1) Wrap the controls in an Update Panel (.NET 3+). Put AutoPostBack=true on the dropdownlist, and define a SelectedIndexChange event for it that changes the hyperlink control's Navigate URL property. When the user changes the dropdown, you're method will fire without the APPEARANCE of a form submission. Downside: there's a slight delay between the dropdown changing and the link changing. If your server is really, really slow, this delay could potentially cause problems (but this is probably unlikely).
2) Use custom JavaScript and do away with your .NET controls completely. This is what I would probably do, assuming you don't need to do anything else with these controls programatically. Your JavaScript function would monitor the dropdown for a SelectedINdexChange and adjust the href attribute of the anchor tag accordingly. Look into jQuery to speed up development if you aren't too familiar with plain JavaScript.
If the control is an ASP:DropDownList, you can use the AutoPostBack="True|False" property to prevent a postback
If you don't want to use the AutoPostBack you have to post the form using jQuery or Javascript
You can add an event on your drop down list onchange and add the code you need to post the form usin jQuery:
$('#formId').submit();
http://api.jquery.com/submit/
If you to want navigate to another Url add the below code at your DropDownList control (make sure AutoPostBack=false)
onchange="window.location.href='yourUrl'"
it would be better put that Javascript on a separate file anyway
I have one GridView which has one CheckBox and three TextBoxes in its template column.
The logic is, when I check the CheckBox, the corresponding TextBoxes should get enabled. If I uncheck the CheckBox, then the corresponding TextBoxes should get disabled. I have written JavaScript for this functionality.
Everything is working fine, but in that page I have one DropDownList too. When I change the DropDownList value, the page gets PostBack and at that time I lost the JavaScript functionality, i.e. the enabled TextBoxes in the GridView gets disabled. How to solve this problem?
After every post back in the update panel you need to reinitialize your javascript, because as you understand the struct of the html has change and javascript runs on the previous one that not exist after the update panel updates. Microsoft gives a functionality to do that as follow.
This is javascript lines.
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function InitializeRequest(sender, args) {
}
function EndRequest(sender, args) {
// here initialize again your javascript
}
</script>
Use the RowCreated event for the ASP Grid to add the client function on the check box, this way the client function will stay on each postback
You must register your javascript at each postback for it to run, by using the ScriptManager.RegisterStartupScript method: here's an example (edited for article in english, sorry :p).
Use auto post back with check box to enable disable text box from server side code. This will update the view state and so it will be maintained on select change. Use these inside UpdatePanel for ajax. Alternatively, you can add drop down into updatepanel as well, so that select change does not affect other page elements.
JavaScript is working on DOM. When page gets PostBack values from ViewState are written to DOM and overwrites your JavaScript changes. You should store state of checkboxes in persistent way ie. jQuery data() or hiddenfield and after PostBack write changes back to DOM.