Solved

create a postback from clientside javascript

Posted on 2006-11-02
3
934 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now