Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 967
  • Last Modified:

create a postback from clientside javascript

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
Collindsouza
Asked:
Collindsouza
  • 2
1 Solution
 
nepaliCommented:
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
 
Type25Commented:
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
 
nepaliCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now