开发者

ASP.NET how to use __doPostBack from Custom JavaScript with Master/Content Pages

In a simple aspx page I can have a custom JavaScript 开发者_StackOverflow社区function postback as described in http://www.xefteri.com/articles/show.cfm?id=18 The simple page uses a linkbutton:

<asp:LinkButton id="CreateFile" runat="server" onclick="CreateFile_Click" />

with code behind that has the VB subroutine:

Sub CreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

and a Javascript custom function that does:

 __doPostBack('CreateFile','');

When I move the code to a content page with a corresponding master page the simple example no longer works.

I'm aware that server control IDs are changed in the generated HTML when a master page is involved, and I'm using the correct IDs in the Javascript. As an example, for the LinkButton ID I use '<%=CreateFile.ClientID%>' for the Javascript in the generated HTML. Never the less, I still can't figure out how to get postbacks from the Javascript when master pages are involved. More accurately, with a master page in the mix the code won't even compile but results in:

'CreateFile_Click' is not a member of 'ASP.testmaster_aspx'

If I remove onclick="CreateFile_Click" from the LinkButton markup it compiles but does not work. Similary if onclick is removed from the simpler version without a master page it does not work either, as expected.

Any input on how to get postback from a custom Javascript function when using master pages would be much appreciated.


are you hard coding the call to __doPostBack?

try using Page.GetPostBackEventReference

to obtain a valid handle in your javascript.

once you write out a valid post back event reference, you could wrap it inside a local function, which you can than call from master page.

if (window.myPostBackfunction) // check if it exists
  myPostBackFunction();


Do this.

Keep the JavaScript that calls the __dopostback function in your master page. On every page that requires the calling of do post back to refresh things automaically after an event put a button and hide it using the "style=display: none" (in this example I am naming this button as 'btndp') and on the click event of this asp button write the code you want to run in the __dopostback. Now JavaScript requires ID of the element while ASP require name of the element so in your JavaScript at master page do this

var btnName = $get("content_btndp").name;
__doPostBack(btnName, "");

You see here we first got the name of the element using the ID and then passed the name to the __dopostback function. So on every content page you can put an invisible button with same name and your one script on master page will serve all.

I hope this solves your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜