i've been at this over a day now and have read almost all posts on this topic and still can not get this working. I'm trying to capture a value from a textbox (ideally a checkbox but starting with a textbox for testing) on a javascript pop-up in asp.net using js / ajax as follows:
in Site.Master:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
HTML:
<script type="text/javascript">
function openprojectCopyModal() {
$("#projectCopyModal").modal('show');
}
function btnCopyConfirmClick() {
$.ajax({
url: "Member/ModalDetails",
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
txtTest: $("#txtTest").val()
}),
success: function (response) {
//
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//
}
});
};
</script>
<!--Modal Save confirmation-->
<div class="modal" id="projectCopyModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Copy Project Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Please select optional items to include when copying this project:</p>
<div style="float: left;">
<asp:TextBox ID="txtTest" CssClass="form-control" Width="250px" runat="server" />
</div>
</div>
<div class="modal-footer">
<button id="btnCopyConfirm" type="button" class="btn btn-primary" onclick="btnCopyConfirmClick">Save</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
CodeBehind:
// Make a copy of project
protected void btnCopy_Click(object sender, EventArgs e)
{
// Get ID of project to copy
ListViewItem item = (sender as ImageButton).NamingContainer as ListViewItem;
int iID_orig = Convert.ToInt16(ListView_Project.DataKeys[item.DataItemIndex].Values["ProjectID"]);
this.Session["copyID"] = iID_orig;
// Prompt to verify
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openprojectCopyModal();", true);
}
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static string ModalDetails(string txtTest)
{
return txtTest;
}
It will pop up the dialog but that's it, it will not call the WebMethod 'ModalDetails' which is in my project root folder Members (have tried 'url' "~/Members/Member/ModalDetails" and many iterations of that as well. Thank you
Edit: I updated function to still to no avail:
$(function () {
$('[id*=btnCopyConfirm]').click(function () {
$.ajax({
url: "Member/ModalDetails",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
txtTest: $("#txtTest").val()
}),
success: function (response) {
//
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//
}
});
});
});
UPDATE I could not get this working for whatever reason so I switched to an asp button server side in combination with jquery as follows for those who it might help:
jquery:
function storevalue() {
var str = $("#txtTest").val();
alert(str);
if (str=="") {
return false;
}
else {
document.getElementById("hfCopyOptions").value = str;
return true;
}
}
html:
<asp:Button ID="btnCopyConfirm" runat="server" CssClass="btn btn-primary" Text="Copy" OnClick="btnCopyConfirm_Click" OnClientClick="return storevalue();" />