admin管理员组文章数量:1313347
I have 2 buttons in my asp File
<asp:Button ID="BTN_Send_LA" runat="server" Text="Save" OnClientClick="ConfirmSendData()"></asp:Button>
//The button the client will click
<asp:Button ID="UploadButton" runat="server" Text="" OnClick="BTN_Send_LA_Click"/>
//Dummy Button for the JS .click()
And here is my Js part:
function ConfirmSendData() {
var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
if (r == true) {
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
//$('UploadButton').trigger('click'); TEST 1
//__doPostBack not working aswell
}
}
So here what i expect to be done:
- The client click the first button (Trigger the JS) => Works
- R is true => Works
- The JS part trigger the Onclick of UploadButton => Don't Work
I don't understand why this method doesn't work as it seems to be the general approach most other answers take on StackOverflow?
UPDATE:
Ok, I've tried every solutions proposed below and now i have weird problems:
When I click on the client button, 1 of the 3 following things happens randomly (route followed with the debugger)
1: The button click do a blank postback (IsPostBack == true) event OnClick="BTN_Send_LA_Click" not fired
2: The button click do a blank postback (IsPostBack == false) event OnClick="BTN_Send_LA_Click" not fired
3: The button fire the event OnClick="BTN_Send_LA_Click" of the dummy button properly.
I don't understand why. When i click directly on the dummy button, everything works fine
Everytime I do a CTRL+F5, the first time I click the client button will work 100% (event fired)
something else: in my event BTN_Send_LA_Click(), I change the background color of multiple controls (lightgreen)
1: If I click on the dummy button => the background color of the controls are changed
2: If I click on the client button and even if the BTN_Send_LA_Click() is fired, the background color doesn't change.
Why ? I'm totally lost on this one
Updated code:
function ConfirmSendData()
{
/*
var dd = document.getElementById("<%=DDL_LaveurLA.ClientID%>");
var txt = dd.options[dd.selectedIndex].text;
var r = confirm("Êtes vous bien: " + txt + " sinon veuillez changer dans le champ spécifié 'Laveur'"); */
var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
if (r == true) {
//$("#<%=UploadButton.ClientID%>").click();
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
}
return false;
}
I have 2 buttons in my asp File
<asp:Button ID="BTN_Send_LA" runat="server" Text="Save" OnClientClick="ConfirmSendData()"></asp:Button>
//The button the client will click
<asp:Button ID="UploadButton" runat="server" Text="" OnClick="BTN_Send_LA_Click"/>
//Dummy Button for the JS .click()
And here is my Js part:
function ConfirmSendData() {
var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
if (r == true) {
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
//$('UploadButton').trigger('click'); TEST 1
//__doPostBack not working aswell
}
}
So here what i expect to be done:
- The client click the first button (Trigger the JS) => Works
- R is true => Works
- The JS part trigger the Onclick of UploadButton => Don't Work
I don't understand why this method doesn't work as it seems to be the general approach most other answers take on StackOverflow?
UPDATE:
Ok, I've tried every solutions proposed below and now i have weird problems:
When I click on the client button, 1 of the 3 following things happens randomly (route followed with the debugger)
1: The button click do a blank postback (IsPostBack == true) event OnClick="BTN_Send_LA_Click" not fired
2: The button click do a blank postback (IsPostBack == false) event OnClick="BTN_Send_LA_Click" not fired
3: The button fire the event OnClick="BTN_Send_LA_Click" of the dummy button properly.
I don't understand why. When i click directly on the dummy button, everything works fine
Everytime I do a CTRL+F5, the first time I click the client button will work 100% (event fired)
something else: in my event BTN_Send_LA_Click(), I change the background color of multiple controls (lightgreen)
1: If I click on the dummy button => the background color of the controls are changed
2: If I click on the client button and even if the BTN_Send_LA_Click() is fired, the background color doesn't change.
Why ? I'm totally lost on this one
Updated code:
function ConfirmSendData()
{
/*
var dd = document.getElementById("<%=DDL_LaveurLA.ClientID%>");
var txt = dd.options[dd.selectedIndex].text;
var r = confirm("Êtes vous bien: " + txt + " sinon veuillez changer dans le champ spécifié 'Laveur'"); */
var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
if (r == true) {
//$("#<%=UploadButton.ClientID%>").click();
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
}
return false;
}
Share
Improve this question
edited May 20, 2015 at 9:01
charles godin
asked May 19, 2015 at 13:54
charles godincharles godin
632 silver badges6 bronze badges
4
-
1
Have you tried
$("#<%=UploadButton.ClientID%>").click();
? – freefaller Commented May 19, 2015 at 13:58 -
Wele to StackOverflow. Please note that generally questions are kept at a fairly professional level so you wouldn't expect to see words like
guyz
in a question. – Ian Commented May 19, 2015 at 13:58 - 1 Which part doesn't work, getting the element or firing the click event? What does clickEvent contain after its assignment? – aw04 Commented May 19, 2015 at 14:01
- @aw04 the firing event occur randomly (check updated part) – charles godin Commented May 20, 2015 at 8:30
4 Answers
Reset to default 4You've got it all right except:
- You need a closing
}
on yourif
statement. ConfirmSendData()
needs toreturn false
to prevent the first button from submitting.
i.e.
function ConfirmSendData() {
var r = confirm("Êtes vous bien...");
if (r == true) {
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
}
return false;
}
You have the following mented out, which I presume means you've tried it and it didn't work...
//$('UploadButton').trigger('click'); TEST 1
jQuery needs the #
character before the ID of the element to find it, so try this instead...
$("#<%=UploadButton.ClientID%>").click();
I would also update the first part of your function slightly to a more readable (and efficient) version by only finding the dropdown element once...
var dd = document.getElementById("<%=DDL_LaveurLA.ClientID%>");
var txt = dd.options[dd.selectedIndex].text;
var r = confirm("Êtes vous bien: " + txt + " sinon veuillez changer dans le champ spécifié 'Laveur'");
You should use $("#<%=UploadButton.ClientID%>")
instead of $('UploadButton')
because asp:Button
elements generates not in element with just id UploadButton
function ConfirmSendData() {
var r = confirm("Êtes vous bien: " + document.getElementById("<%=DDL_LaveurLA.ClientID%>").options[document.getElementById("<%=DDL_LaveurLA.ClientID%>").selectedIndex].text + " sinon veuillez changer dans le champ spécifié 'Laveur'");
if (r == true) {
var clickButton = document.getElementById("<%= UploadButton.ClientID %>");
clickButton.click();
// alternative variant for jquery
// $("#<%=UploadButton.ClientID%>").click();
}
}
also another thing that function ConfirmSendData
needs to return false
to prevent the submitting data by first button
Try this:
__doPostBack('<%= UploadButton.UniqueID %>', '');
本文标签: Calling C code behind method using javascript without ajaxStack Overflow
版权声明:本文标题:Calling C# code behind method using javascript without ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741943652a2406289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论