JavaScript in Asp.net Form

netpicker9
11-30-2006, 08:41 AM
First.aspx:


<Form id="aspForm" runat="server" action="Second.aspx">
'Lot of Other stuff here..

'Buttons Bar
<asp:Button ID="cmdGo" runat="server" Text="Continue >>"/>
<asp:Button ID="cmdPrint" runat="server" Text="Print Me" OnClientClick="javascript:PF1();"/>
<input name="cmdCalc" type="Submit" value="Calculate" onclick="return MeCalc();">
<input type="hidden" name="intMax" value="">
<input type="hidden" name="intMin" value="">
</Form>


When I click cmdGo, it will take me to Second.aspx

Problems I am facing:

1.
When I click cmdCalc (for this I am using normal HTML Submit button), I want to call Third.aspx.
In this button I am calling a JavaScript Function MeCalc(), MeCalc has lot of things to do and it will return some values to Hidden Fields,
These hidden field values I want to take to Third.aspx

User has to select one of Radio buttons (are created dynamically) in the form before submitting to Third.aspx, I wrote validation for radio button and displaying an alert, but after pressing OK in alertbox,
the form is submitting onto it self.. i.e to First.aspx.

and If I select radio button and then click Submit.. the for still submitting to the same page, but not going to Third.aspx


function RadioB(){
var theMain = document.forms['aspForm'];
if (!theMain) {
theMain = document.aspForm;
}
var numarr = "";
var option = -1
var i
var liCount = <% Response.write(mtn) %>

for (i=0; i< theMain.txtSelect.length; i++) {
if (theMain.txtSelect[i].checked) {
option = i;
}
}
if (option == -1) {
alert ("Please select radio button before Calculate");
return false;
}

theModel.intMax.value = eval("theMain.txtMax" + row + ".value")
theModel.intMin.value = eval("theMain.txtMin" + row + ".value")

return true;
}

function MeCalc(){
if(RadioB()){
document.location="Third.aspx?NUM=<% response.write(sID)%>";
}else{

}
}



Where I am doing Mistake here?? Please guide me.

--------------------------------------------------------------------------

2.
When I click cmdPrint, I am calling JavaScript function PF1(), its opening an another page in
new window, But still the parent form (First.aspx) is submitting onto it self, How to Avoid that?


function PF1(){
var lsURL = document.location.href;

if (lsURL.indexOf('?') < 0){
lsURL = lsURL + "?pf=1&NUM=<% response.write(sID)%>";
}
lsURL = lsURL.replace(/\.aspx/i, "P.aspx")
window.open(lsURL, "_blank", "location=no,top=0,left=0,menubar=yes,toolbar=no,scrollbars=yes,height= 500,width=820");
}



Thanks

wayneph
11-30-2006, 10:09 AM
1. don't use document.location. You have to actually submit the form.
theMain.action = 'third.aspx';
theMain.submit;

2. Make it a client side button as well. no need to post to the server.
or try returning false from your function. That should suppress the click event. But I haven't tested it.

netpicker9
12-06-2006, 03:20 PM
1. don't use document.location. You have to actually submit the form.
theMain.action = 'third.aspx';
theMain.submit;

2. Make it a client side button as well. no need to post to the server.
or try returning false from your function. That should suppress the click event. But I haven't tested it.

Hi,

I got one more problem.. When i use
theMain.action = 'third.aspx';
theMain.submit;

asp.net page is giving error, once I click button.. Where as the same code in other page is working fine..

My page is not showing error details, but.. something like "Invalid Viewstate"

Suggest me.

Thanks

wayneph
12-07-2006, 07:23 AM
Well, the problem is that ASP.NET forms are designed to PostBack to themselves. The ViewState is used to recreate the controls that existed on the Page when it was built and sent to the browser. By changing the action, you can't use any of the default ASP.NET form support anymore. You have to code everything on you're own.

Can you combine your pages into a singe form? Just make <asp:panel> controls for the different things that should be displayed. In the OnClick handlers for your buttons, you can set the Show and Hide the correct panels by changing the visible property. That way you don't have to resort to using the Request object, and the ViewState will be maintained properly.

Out of curiousity, do you have a Classic ASP or PHP background? If so you need to unlearn a lot of the things you used to do, in order to become more efficient with ASP.NET.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum