Reputation: 5992
I am trying to PostBack
the Page
manually, BUT somehow its not working. I am not sure what i am doing wrong here. I am using Jquery dialog boxes and putting the confirmation box before posting back the page. here is my code.
<form id="form1" runat="server">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<asp:CheckBox ID="cbIsCollected" runat="server" AutoPostBack="false" Checked='<%# MWClickAndCollectHelper.CheckOrderCollectedStatus(AlwaysConvert.ToInt(Eval("OrderId"))) %>'
OnCheckedChanged="cbIsCollected_CheckedChanged" CssClass="isCollectedCheckBox" />
</form>
var isCollectedCheckBox = $('.isCollectedCheckBox input[type=checkbox]');
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
$(isCollectedCheckBox).on("change", function () {
var checked = this.checked;
var checkbox = this;
if (checked) {
checkbox.checked = true;
__doPostBack(checkbox, 'JavaScript');
}
});
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.GetPostBackEventReference(this, string.Empty);
string targetCtrl = Page.Request.Params.Get("__EVENTTARGET");
postbackId.Text = targetCtrl ?? "First Time Loaded";
if (!Page.IsPostBack)
{
BindGridView(gvOrders, CNCOrderCollection);
}
}
So when I Check
the CheckBox
it should postback to server and the ControlID name should appear. BUT in my case its always shows 'First time Loaded'. that means page does not recognize it as a postback. I want to raise server side CheckBox_changed
event.
Please help.
Upvotes: 2
Views: 4585
Reputation: 3615
Why are you trying to post back "manually"? The checkbox control has a postback handler. Change AutoPostBack to true and set OnCheckedChanged to the name of a event method on the server side. If you want to get the control that sent the click, that's what the sender is for. Example:
<asp:CheckBox ID="cbIsCollected" runat="server" AutoPostBack="Trye"
OnCheckedChanged="cbIsCollected_CheckedChanged" CssClass="isCollectedCheckBox" />
Protected Sub cbIsCollected_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
dim cbSender as checkbox
cbSender = ctype(sender, checkbox)
end Sub
If this code isn't accomplishing what you are looking for, let me know.
EDIT:
If you want to do the postback from JS, you have two options. First, you can call the _doPostback like this:
function someFunction()
{
__doPostBack('btnName','');
}
Where btnName is the name of a button on your page.
Second, you can simply have a button on your page that is hidden but has an event handler associated with it. Your client side button click calls calls as JS function that does the click of the server button for you: like this:
function clickButton() {
var getBtn = document.getElementById('<%= btnName.ClientId %>')
getBtn.click()
}
Of course, this seems redundant as why wouldnt the Yes button just have its own event handler and the No button be a client side button that returns false?
Upvotes: 1