Solved

create a postback from clientside javascript

Posted on 2006-11-02
3
945 Views
Last Modified: 2008-02-01
Hello Expert,

I have been successfull in invoking a postback from clientside javascript by using  Page.GetPostBackEventReference

I have set this on the "onfocus" event of a textbox as follows

protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
              DtRespAlerted.Attributes.Add("onfocus", Page.GetPostBackEventReference(this));
        }
}

this script performs a PostBack on the onfocus() event of the textbox... However i'm unable to capture this event on postback...
how can i trap this event?? i mean i have the following declaration for the textbox control..

<input id="DtRespAlerted" class="DtTextBox" type="text" name="DtRespAlerted" runat="server"  onfocus="DtRespAlerted_onfocus" />

and i have created a custom event in the codebehind .cs file

    protected void DtRespAlerted_onfocus(object sender, EventArgs e)
    {
        // My internal code
    }

however when i put a breakpoint.. the code doesnt step thought this part of code??

how can i capture this event code behind..


Please help??
0
Comment
Question by:Collindsouza
  • 2
3 Comments
 
LVL 7

Expert Comment

by:nepali
ID: 17858754
Hey Collin,
Can you try to postback the form by raising the submit button event instead of onfocus event.
try this to do a postback in your client side onfocus event handler.

document.getElementById('SubmitButton').click()

where u have a button like one here
<asp:Button id="SubmitButton" runat="server" Text="Submit"></asp:Button>    

Then u can trap the click event of the button on the server side.

Regards
Mahendra
0
 
LVL 9

Expert Comment

by:Type25
ID: 17858890
Try checking the value of:  

Request.Form["__EVENTTARGET"].ToString()    

in page_load

if it doesn't contain a value then you can set it before you postback, like:
document.Form1.__EVENTTARGET.value='textBoxFocus'

For example:


0
 
LVL 7

Accepted Solution

by:
nepali earned 500 total points
ID: 17859014
here is the comlete code for ur solution.

aspx page
<!--
            function abc()
            {
                  document.getElementById('aaa').value = "abc";
                  document.getElementById('SubmitButton').click();
            }
//-->
            </script>
      </HEAD>
      <body>
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
                  <asp:Button id="SubmitButton" runat="server" Text="Button"></asp:Button>
                  <input type="hidden" id="aaa" runat="server">
            </form>
      </body>
</HTML>


Code behind.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If (Not IsPostBack) Then
            Me.TextBox1.Attributes.Add("onfocus", "javascript:abc();")

        End If
    End Sub

    Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
        If aaa.Value = "abc" Then
            Response.Write("Text Box Clicked")
        End If
    End Sub

Cheers
Mahendra
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question