Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Postback - How to get javascript do button.click() & postback form dat

Status
Not open for further replies.

fletchsod

Programmer
Dec 16, 2002
181
0
0
I have spend all afternoon trying to make this work. When a customer click the login button, it does the postback to ASP.NET which check for existing user in database. If the user exist then it send the javascript “confirm()” prompt asking yes/no question. The end user then click the javascript yes/no button. I have not figure out how to do a postback with the given yes/no value back to the server as a javascript postback.

I wrote the javascript function below and found that the triggering the ASP.NET button seem to work. But the hidden label value “lblHiddenExistingUserPrompt “ is succesfully returned on the client-side but failed to be returned to the server-side as a postback. :-/ (See variable "sJunk" that contains empty data when a postback was made back to the server)

Code:
    string sJunk = Convert.ToString(this.lblHiddenExistingUserPrompt.Text).Trim();

    //08/03/2010 - Allow the user to log back in, to retain/discard the session...
    if (Convert.ToString(this.lblHiddenExistingUserPrompt.Text).Trim() == "X")
    {
        sJavaScriptErrorMsg = "";
        sJavaScriptErrorMsg += "function ftnExistingUsrAcctLoginPrompt() {\n";
        sJavaScriptErrorMsg += "    var oLblHiddenExistingUserPrompt = document.getElementById('lblHiddenExistingUserPrompt');\n";
        sJavaScriptErrorMsg += "    var oBtnLogin = document.getElementById(\"" + this.butLogin.ClientID + "\");\n";
        sJavaScriptErrorMsg += "    \n";
        sJavaScriptErrorMsg += "    if (confirm('You did not log out on your last visit. Would you like to resume your previous session or start a new one?')) {\n";
        sJavaScriptErrorMsg += "        oLblHiddenExistingUserPrompt.value = 'Y';\n";
        sJavaScriptErrorMsg += "    } else {\n";
        sJavaScriptErrorMsg += "        oLblHiddenExistingUserPrompt.value = 'N';\n";
        sJavaScriptErrorMsg += "    }\n";
        sJavaScriptErrorMsg += "    \n";
        sJavaScriptErrorMsg += "    oBtnLogin.click();\n";
        sJavaScriptErrorMsg += "}\n";
        sJavaScriptErrorMsg += "\n";
        sJavaScriptErrorMsg += "ftnExistingUsrAcctLoginPrompt();";
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), System.Guid.NewGuid().ToString(), sJavaScriptErrorMsg, true);
        return; //This exit the function...
    }
Code:
    <form id="form1" runat="server">
    <div>

        <!-- ASP.NET AJAX Script Only... -->
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>

                <asp:Button id="butLogin" runat="server" Text="Log In" EnableViewState="False" onclick="butLogin_Click"></asp:Button>
				    
                <asp:Label ID="lblHiddenExistingUserPrompt" runat="server" style="visibility:hidden;"></asp:Label>
           
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="butLogin" />
            </Triggers>
        </asp:UpdatePanel>
    
    </div>
    </form>

Is there a way to make this work? (or other way?) There gotta be a way to make this work. (Don't want to make a 2nd webpage and have the customer click the button again.)

Thanks...
 
Try using something like this.

In js where you want the postback:
Code:
  var eventTarget = 'PostBackFromJS';
  var eventArgument = 'UserId:Joe|Confirm:Yes';

  // Rest of JS function ...

  __doPostBack(eventTarget, EventArgument);

Then on the server side, in the page load:
Code:
  Dim eventArgument As String = Request.Form("__EVENTARGUMENT")
  Dim eventTarget As String = Request.Form("__EVENTTARGET")


So now on the server side those two values have been passed from JS and you can do whatever you want







* Sine scientia ars nihil est
* Respondeat superior
 
I'm trying... Doesn't seen to work. :-/
 
As I was reading your post again, I was thinking is the ViewState enabled for the page or control? If not that could be why you cannot get the value on the server side.

* Sine scientia ars nihil est
* Respondeat superior
 
This is a simple example where on the click of a label I cause a postback. The variable 'setId' is a global declared elsewhere.
Code:
    function lblSortReports_Click() {
        __doPostBack('SortSet', setId); 
    }

Then in the code behind page:
Code:
    Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load

        Dim _eventArgument As String = Request.Form("__EVENTARGUMENT")
        Dim _eventTarget As String = Request.Form("__EVENTTARGET")

        If _eventTarget = "SetNodeDblClick" AndAlso _eventArgument <> "" Then
            '* ** Update the Report Set
            ShowReportSetSchedule(_eventArgument)

        ElseIf _eventTarget = "RptNodeDblClick" AndAlso _eventArgument <> "" Then
            '* ** Goto SelectionCart
            NavigateToSelectionCart(_eventArgument)

        ElseIf _eventTarget = "SortSet" AndAlso _eventArgument <> "" Then
            NavigateToSortPage(_eventArgument)
        End If

    End Sub


* Sine scientia ars nihil est
* Respondeat superior
 
Nice... I'll give this a try... One question. Say you click this button to go to next page then an end-user click the web browser's back button. I hope the javascript popup won't occurred...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top