Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

create a postback from clientside javascript

Posted on 2006-11-02
3
Medium Priority
?
962 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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