Solved

create a postback from clientside javascript

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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