How to manually submit a button in asp.net using javascript? - c#

Using javascript, I want to submit a asp.net button, how can I do this?
I know the onclick looks like: javascript:WebForm_DoPostBackWithOptions(new .....);
I am also weary because the ID of the control can change.

If you have a control similar to this:
<asp:Button ID="Foo" ... />
You can something simple like fire the 'click' event in JS while accessing the updated client ID (jQuery syntax here):
$('#<%=Foo.ClientID%>').click()
Or you could get the proper JS to run like this:
<script type="text/javascript">
function clickFoo() {
<%=Page.ClientScript. GetPostBackEventReference(Foo)%>;
}
</script>

var button = document.getElementById('btnID');
if (button)
{
button.click();
}
If you can put the javascript right in your .aspx markup, then you can get around the changing ID's as well by doing this:
var button = document.getElementById('<%= myServerButton.ClientID %>');
if (button)
{
button.click();
}
When your .aspx is processed, the ID of the button as it appears on the page will be substituted into your javascript function.

Using jquery put something like this into your aspx page.
$('#<%= myctrl.ClientID %>').click();
myctrl is the button. The property ClientID gives the id of the html button. Jquery offers the click function.

That is easy you can use __doPostBack function passing the controll ID that you want the click(command etc) event get fired.
To avoid problems with ID, do something like it:
__doPostBack("<%= yourConrol.UniqueID%>");
EDIT:
There is an existing .Net Framework method Page.GetPostBackEventReference that emits client-side script that initiates postback and also provides a reference to the control that initiated the postback event.

You can either, click a button or submit a form.
So, if you want to click the button,
var button = document.getElementById('<%= btnButtonID.ClientID %>');
if (button) { button.click(); }
or to Post the form
document.forms[0].submit();

Related

How to load repeater controls after the page load in asp.net?

I want to load two repeater controls after the asp.net page is loaded completely. These two controls are populated by a button click event. Instead of the button being clicked by the user, I want that to be clicked by an ajax call - or by any other means - after the page is loaded.
I have followed the technique described here: http://forums.asp.net/t/1452988.aspx?how+to+load+gridview+after+the+page+is+completely+loaded.
Essentially it uses Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded); Once the pageLoaded event handle is obtained, it uses javascript to call the button click event. While it sort of works, the issue is that the page loading and the button click events are executed repeatedly. Because of this, the page flickers constantly. Please let me know how to resolve this issue.
Note: The site has a master page which has the scriptmanager.
<asp:UpdatePanel>
...More code here...
</asp:UpdatePanel>
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
function pageLoaded(sender, args) {
var hdnLoadedPage = $get('<%= hdnLoaded.ClientID %>');
var btnApplyFilter = $get('<%= btnFilter.ClientID %>');
if (hdnLoadedPage != 'Yes') {
btnApplyFilter.click();
}
}
</script>
</asp:Content>
Thanks

Response.Redirect() not working after javascript confirm got false value

I had a simple page like this:
A.aspx -
has an asp TabControl make user change to B.aspx or back, and a button will use javascript confirm user to do something.
B.aspx -
has the same TabControl like A.aspx , just show some message here.
button code in A.aspx like this:
<button id="do" onclick="if (confirm('you sure?')==false) { return false; };"></button>
and my Response.Redirect code in A.aspx.cs TabControl_TabChanged() like this:
Response.Redirect("b.aspx");
It work fine before I click the button, if I click it and select 'ok' it still fine,
but when I select 'cancel', the Response.Redirect() will run but the page didn't change.
please help me find the problem.
Just Replace 'onclick' for javascript with 'OnClientClick'.
I would try to set a jQuery event on my Button
<form id="submitform">
<button id="do" >change</button>
</form>
$("#do").click(function(e) {
var r=confirm("you sure?");
if (r===false)
{
e.preventDefault();
}
});
perhaps this helps

Trigger an asp.net LinkButton with javascript

I have a page configured to hide a FileUpload control, so when you click a button it shows the file upload windows and after you choose the file it automatically submits it to the server.
Ir order for this to work i created 2 linkbuttons and a file upload but only one of the buttons is visible. When you click the button it triggers the fileupload window and when the FileUpload control has a change it triggers the submit (invisible) button. The proble I'm having is that only the OnClientClick is being triggered and the server side code is not running
<script type="text/javascript">
document.getElementById('<%= button.ClientID %>').onclick = function () {
document.getElementById('<%= fileupload.ClientID %>').click();
return false;
}
document.getElementById('<%= fileupload.ClientID %>').onchange = function () {
document.getElementById('<%= save.ClientID %>').click();
}
</script>
The save button has a onclientclick to show a modal window and should send to server
OnClick="lnkChange_OnClick" OnClientClick="ShowModalDialog(0);"
Any way to fix this?
I think __doPostBack('','') will help you.
Call _doPostBack function from your client side function. It will postback the page.

get checkbox click inside updatepanel by jquery

I have an update panel with a runat server div inside,
this div isn't shown in the first load of the page. I used to show it after user input the search key then reload the update panel which contain the div and fill div controls then show it.
I have a CheckBox inside this div tag and I need to get the click event of this check box with jquery
I try to use direct .click or .live but all doesn't work !!
Any help will be appreciated.
Use this code
$(document).ready(function()
{
//This will add one function to be called on every request of the update panel
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});
function EndRequestHandler()
{
$('#checkboxID').change(function(){
//Your functionality
});
}
You need to introduce javascript into the page to simulate the click event again. Page.RegisterClientScriptBlock or Page.RegisterStartUpScript should do it.
or
place this inside the updatepanel
<script type="text/javascript">
Sys.Application.add_load(your jquery function);
</script>

hyper link to set drop downlist to visible

Wondering if there is a way upon clicking on a hyper link to set drop downlist to visible in code behind or asp?
<asp:HyperLink ID="HyperLink2" runat="server">View More Workout Programs ยป</asp:HyperLink>
If you have to do it in code-behind, then use a LinkButton instead of a HyperLink. Then it will have a click event just like any button and in that click event you can set the other element to .Visible=true.
However, does this need to be done in code-behind? Keep in mind the difference in "visibility" between server-side and client-side code:
If set to .Visible=false on the server-side, the content is not delivered to the client at all.
If set to display:none on the client-side, the content is present and can be viewed in the page source, it's just not displayed by the browser.
In some cases, the former is needed for security purposes. But if it's just a matter of user experience then I would recommend showing/hiding the content entirely on the client-side so as to avoid post-backs that do nothing more than change element display properties.
For example (assuming jQuery):
<a id="toggler" href="#">Show the content</a>
<div id="hidden" style="display:none;">Content</div>
<script>
$(document).ready(function(){
$("#toggler").click(function(){
$("#hidden").show();
});
});
</script>
Use an asp:LinkButton instead of a hyperlink and handle the OnClick event. In the OnClick event, toggle myDropDownList.Visible depending on whether you want to show it or not.
You should implement that kind of feature in the client (javascript code) to improve user experience.
Anyway, you can use a Panel with Visibility=false and put Visibility=true in code behind when link is clicked. You would need to adjust the position of that panel with css to look like a dropdown.
You can try with JQuery : http://www.jquery.com
It will be something like
<script type="text/javascript">
$(document).ready(function(){
$("#<% =HyperLink2.ClientID %>").click(function() {
$("#<% =DropDownList1.ClientID %>").toggle();
});
});
</script>
If you need to send form back to the server, use asp:LinkButton instead and handle OnClick event on the server side. If you need to show drop down list on the client side, use javascript function with onclick client event to show or hide any section you want.

Categories