Showing posts with label contains. Show all posts
Showing posts with label contains. Show all posts

Saturday, March 24, 2012

Invoke Javascript method after partial postback ?

Howdy All,

This might be an easy one but I don't seem to be able to figure it out.

Basically I have an update panel which contains a button that causes the partial postback. After the postback I'd like it to call a Javascript method.

I tried looking to set it on an atlas object and I even tried some hacks like putting a literal control in the Update Panel and setting the text to <script language=""Javascript"">testFunction();</script>" but still no cigar.

Any help would be greatly appreciated.

Try registering your script on the server-side, during the partial post. The script you register will be executed when the response is received from the partial postback.

Page.ClientScript.RegisterStartupScript

-Tony


in my callback function i havestring js = "alert('s');"; Page.ClientScript.RegisterStartupScript(GetType(), "key", js);It doesn't fire

casaubon:

in my callback function i have string js = "alert('s');"; Page.ClientScript.RegisterStartupScript(GetType(), "key", js); It doesn't fire

You need to wrap your js in <script type="text/javascript"> </script>, or use the overloaded RegisterStartupScript specifying "true" for the last parameter.

Hope this helps,

-Tony

Invoking ModalPopupExtender causes Submit button to not postback

I have a simple webform that contains a textbox and a required field validator. The page also has a modal popup extender tied to a button as well as two other buttons for saving the page's input and canceling the pages changes. The cancel button has causesvalidation="false".

I run the web application and on the page in question click the button that causes modal popup to show and then hide the modal popup. The textbox on the page does not have any text and so the validator shows the invalid message. When I click the 'Cancel' button the first time the page does nothing. If I click it again, the server side event handler is fired. Given that causesvalidation is set to false I expected the server event handler to fire on both occasions.

Here is the sample code that displays the said behavior -

<asp:ScriptManager ID="scriptManager1" runat="server" />
<div>
<asp:Label ID="label1" Text="Label1" runat="server" />
<asp:TextBox ID="textbox1" runat="server" />
<asp:RequiredFieldValidator ID="textRequiredFieldValidator" ControlToValidate="textbox1"
Text="*" runat="server" />
<br />
<asp:Button ID="popperUpperButton" Text="Show Popup" runat="server" />
<asp:UpdatePanel ID="pageUpdatePanel" UpdateMode="Always" runat="server">
<ContentTemplate>
<asp:Button ID="submitButton" Text="Send" OnClick="SubmitButton_Click" runat="server" />
<asp:Button ID="pageCancelButton" Text="Cancel" CausesValidation="false" OnClick="PageCancelButton_Click"
runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="statusLabel" runat="server" />
<act:ModalPopupExtender PopupControlID="modalPopupPanel" TargetControlID="popperUpperButton"
OkControlID="okButton" CancelControlID="leavePopupLinkButton" runat="server" />
</div>
<asp:Panel ID="modalPopupPanel" runat="server">
Just some text that is shown in the popup window.
<asp:Button ID="okButton" Text="OK" runat="server" />
<asp:LinkButton ID="leavePopupLinkButton" Text="Leave Popup" runat="server">
</asp:LinkButton>
</asp:Panel>

Server event handler code -

protected void Page_Load(object sender, EventArgs e)
{

}

protected void SubmitButton_Click(object sender, EventArgs e)
{
this.statusLabel.Text += "submit clicked...";
}

protected void PageCancelButton_Click(object sender, EventArgs e)
{
this.statusLabel.Text += "cancel clicked...";
}

Has anyone else seen this behavior and is there a known fix? I am seeing it consistently on the application I am building.

Thanks in advance,

Ruwan

Why are the submitButton and cancelButtons in an UpdatePanel but the textbox is not? Could you try to get the page working without the updatepanel first? I am not sure this issue has anything to do with the modalpopup being on the page.


Thanks for the response. I have tried the above with and without the updatepanel and see the same result. My reason for suspecting some coupling with the ModalPopupExtender is that the issue does not show up till the ModalPopupExtender is placed on the page.

The updated ASPX page reads -

<asp:ScriptManager ID="scriptManager1" runat="server" />
<div>
<asp:Label ID="label1" Text="Label1" runat="server" />
<asp:TextBox ID="textbox1" runat="server" />
<asp:RequiredFieldValidator ID="textRequiredFieldValidator" ControlToValidate="textbox1"
Text="*" runat="server" />
<br />
<asp:Button ID="popperUpperButton" Text="Show Popup" runat="server" />
<asp:Button ID="submitButton" Text="Send" OnClick="SubmitButton_Click" runat="server" />
<asp:Button ID="pageCancelButton" Text="Cancel" CausesValidation="false" OnClick="PageCancelButton_Click"
runat="server" />
<br />
<asp:Label ID="statusLabel" runat="server" />
</div>
<asp:Panel ID="modalPopupPanel" runat="server">
Just some text that is shown in the popup window.
<asp:Button ID="okButton" Text="OK" runat="server" />
<asp:LinkButton ID="leavePopupLinkButton" Text="Leave Popup" runat="server">
</asp:LinkButton>
</asp:Panel

Thanks,


have you tried adding a validationgroup attribute to the validator and the submit button. adding a attribute like validatorgroup="vg1" to both should cause all other control to ignore the validation except what is part of the group. This may help.


also, I will agree with kirtid, the use of the update panel may be part of the problem. Your update panel only wraps the 2 buttons, so clicking 1 will fire but you will not be able to see the results. if the label is NOT in the update panel. it will not be updated. Try moving the label into the update panel and see if it gets written to. also move the textbox and validator into the UP as well so they can be fired and accessed. I just ran that code with all of them inside the update panel, and it worked for me.

-Alan


Alan,

The ValidationGroup suggestion has provided a work-around for the test application, which now works fine. I will merge the solution in the product that first brought up the issue.

Thanks for the help!

Invoking postback for an update panel in a parent document

The default page for my website contains a div with links to user data that is only visible when a user is logged into my site. The login control lives in an IFrame within the default page (see sample code below).

Now, how can I get the udpate panel on the default page to recognize an event or a button click inside of the login control? I'm not looking for a tutorial on getting the login control to work, but rather any link button inside of the IFrame's update panel forcing the default page's update panel to post back. Both pages have a script manager.

chopped down version of existing code:

dafault page:

1<asp:update panel id="upUserPrefs" runat="server" updatemode="conditional">2 <contenttemplate>3 <div id="userPrefs" runat="server"><a href="prefs.aspx">preferences</a></div>4 </contenttemplate>5</asp:update panel>

IFrame source page:

1 <asp:UpdatePanel ID="updateSignon" runat="server" UpdateMode="conditional">2 <ContentTemplate>3 <asp:Table ID="tblLoginCtrl" runat="server" visible="false" Width="100%" HorizontalAlign="Left" CellPadding="0" CellSpacing="0">4 <asp:TableRow>5 <asp:TableCell ID="tcLoginCtrl">6 <uc1:LoginControl ID="LoginControl" Enable runat=server LoginStyle=Login_Home />7 </asp:TableCell>8 </asp:TableRow>9 </asp:Table>10 </ContentTemplate>11 </asp:UpdatePanel>
Any help would be greatly appreciated.

</p><p> (this.Parent.Parent as UpdatePanel).Update();</p><p>

is what I use.....however i have other problems...LOL. but that should fix yours.


Hi,

I think you can add a invisible button in the default page, set it as a trigger for the update panel.

Then add a javascript function in the contained page, in which invoke the click event of the button in the container.

Please feel free to let me know if there is any problem.


I tried that before, but when I have 2 instances of the same control on my page, it fires events for both of them. Or did I do something wrong?? Should this be happening? Have you ever heard of this??


In my opinion, it's normal that two control of the same type would behavior the same.