Showing posts with label postback. Show all posts
Showing posts with label postback. Show all posts

Wednesday, March 28, 2012

Calling javascript from ASP.NET AJAX

I am trying to read a cookie with some javascript, without postback, using a timer control. I all need to do is return the value of the .js file and use it in the timer event.

So far here's what I have done:

I have a ScriptManager and an updatepanel with a timer control inside.

I set the ScriptReference to the file that reads the cookie:

<Scripts>
<asp:ScriptReference Name="ReadCookie" Path="javascripts/ReadCookie.js" />
</Scripts>

The timer calls this event:

protected void Timer1_Tick(object sender, EventArgs e)
{

}

My Question is how do I call that cookie and access the value in my code behind?

Thanks, Justin.

I guess I'll should restate my question:

How do I call a javascript function that has been added to the scripts collection of the scriptmanager (as shown above) from my c# codebehind?

Thanks, Justin.


You can register that JavaScript function in Timer's Tick Event.


Yes, I have tried that with no success but maybe I am doing it wrong:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
<Scripts>
<asp:ScriptReference Path="ReadCookie.js"/>
</Scripts>
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="readCookie">
</asp:Timer>

....

readCookie.js:

function readCookie() {
var nameEQ = "PhoneNumber" + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}

if (c.indexOf(nameEQ) == 0){
return c.substring(nameEQ.length,c.length);
}
else {
return null;
}
}
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

I Get this Error:

Compiler Error Message:CS0117: 'ASP.agents_customerdetailsform_aspx' does not contain a definition for 'readCookie'


Hi, Justin


Check this link for answer and more help:

  • CS0117 :: 'Type' does not contain a definition for 'Identifier' :http://dotnetslackers.com/_NET/re-56753_CS0117_Type_does_not_contain_a_definition_for_Identifier.aspx

  • Monday, March 26, 2012

    Calling __doPostBack causing full postback :-(

    Ok, if I capture an event on the client, for example, a click event on a textbox that is inside an update panel, and in that event I call __doPostBack I get a full postback.

    However, if I add a button and call it's click event, I get the partial page update I want. So I guess the question is how to invoke a partial page update via script?

    Thanks ... Ed

    Make sure the appropriate trigger has been set up for the UpdatePanel. You can also set the TextBox to have AutoPostBack="true" which will add a __doPostBack to the onchange event handler.

    Of course if you absolutely need to trigger a button click from JavaScript you can do something similar to this:

    <

    htmlxmlns="http://www.w3.org/1999/xhtml">
    <headrunat="server">
    <title>Untitled Page</title>
    <scriptlanguage=javascript>
    function SemiHacky(){
    __doPostBack('Button1','');
    }
    </script>
    </head>
    <body>
    <formid="form1"runat="server">
    <asp:ScriptManagerID="ScriptManager1"runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanelID="UpdatePanel1"runat="server">
    <Triggers><asp:AsyncPostBackTriggerControlID=Button1EventName="Click"/></Triggers>
    <ContentTemplate>
    <asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:ButtonID="Button1"runat="server"Text="Button"/>
    <br/><br/>
    <ahref="javascript:SemiHacky();">Click here</a> to execute some JavaScript and submit the Button1 button via JavaScript.
    </form>
    </body>
    </html>

    The code behind is just setting the Label text to the current time so you can see the PostBack occur:

    Label1.Text =DateTime.Now.ToString();

    Cheers,
    Al


    Hi,

    I did exactly as you suggested, but to no avail.

    I am trying to launch a modal dialog and I have tried the following variations, some of which seem to work correctly, except that once the modal dialog is display and I try to dismiss it via a server

    side mydlg.close() I get an error stating "The control 'dlgDivTasks' already has a data item registered."

    These are the individual calls that I have tried. I should point out that if I just to a plain __doPostBack("","") I get a full postback and no error when I try to dismiss the dialog.

    _doPostBack('Button1','');
    __doPostBack("","");
    Sys.WebForms.PageRequestManager.getInstance()._doPostBack("","");
    document.forms[0].ctl00_ContentPlaceHolderContent_Button1.click();
    $find("DlgTasksBehavior").show();

    Thanks ... Ed


    Hi Ed,

    Have you had any luck using the OnOkScript property of the ModalPupupExtender? One way that I use the modal popup is I have an Ok and Cancel button but they may be named differently based on user actions (ie. Cancel may be labelled as Close). I then use the OnOkScript to execute some client JavaScript which then uses a web service to pass the data back to the server for some processing.

    OkControlID="btnOk"OnOkScript="modalOkScript()"CancelControlID="btnCancel"

    function

    modalOkScript(){
    //TODO: Add some web service call to take care of my data processing requirements
    }

    When the Web Service completes, the onSuccess JavaScript function for the webservice in my OnOkScript function changes the 'Cancel' button text to 'Close.

    function

    onSuccessModalOkScript(result){
    $get('btnOk').disabled =true;
    $get('btnCancel').value ='Close';
    }

    Then it's finished and the Modal closes via client side when the user is ready.

    Al


    Looks like my copy / paste made the JavaScript functions look odd.

    Hi, thanks for the reply.

    Wouldn't what you suggest be kinda limiting? I wouldn't have access to the various data and controls on the page unless I passed a mountain of parameters the the service call. Seems to me I should just be able to do an async update via the update panel so I can use my server side code to get the job done.

    The thing that is a mystery to me is why it is doing a full post back in the first place.I have everything working perfectly it's just the damned blink!

    Thanks ... Ed


    I have just had a similar situation myself whereby a full postback was occuring whenever I called __doPostBack. Trick is that you have to specifiy the actual client ID in the EventTarget parameter

    i.e. _doPostBack('', ''); will NOT work where as _doPostBack('ctl00_textBox1', ''); WILL work. Obviously thats just an example so replace ctl00_textBox1 with your actual clientID value

    So my code became:

    textJobRef.Attributes.Add("onkeyup","javascript:setTimeout('__doPostBack(\'" + textJobRef.ClientID +"\', \'\')', 0)")

    I dont think the timeout is required, but .NET does it so it must be for a reason! :)

    call postback in javascript ?

    Is that anywhere i can call a partial postback in javascript ? what function i need to call ?

    hello.

    normally, this is done by using a dummy element maintained on the page.for instance, you add a button and then, after getting a reference to that html control, you call its click method.

    Wednesday, March 21, 2012

    CalendarExtender....

    Hello, Everyone

    Please can you help me out with this in (VB)...?

    I have a form with a calendarExtendar. When I select a date, and a postback happens I lose the selected date value.

    Also when I try and insert I get the todays value and not the selected one..

    You have any suggestions?

    After deleting everything and starting again it works ….very strange


    The problem was not solved by deleting everything…. But it help me find out where the problem actually is! The problem lies with the image pop up for the calendar control …. Delete that and you will stop getting the current date after postback… If you know the solution is let me know…


    Post some code...though the post back problem sounds more like you have forgotten to put the load methods inside an "If(!Page.IsPostBack)" block.

    Ignore


    As requested.... Please can you explain further or suggest some reading about "load methods inside an "If(!Page.IsPostBack)"

    ProtectedSub SqlDataSource1_Inserting(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
    Dim Textbox22As TextBox = FormView1.FindControl("TextBox22")

    Dim Textbox1As TextBox = FormView1.FindControl("TextBox1")

    Dim sDateTimeAsString =CStr(Textbox22.Text) +" " +CStr(Textbox1.Text)Dim OpenDateTimeAsDate = DateTime.Parse(sDateTime)

    e.Command.Parameters("@.OpenDateTime").Value = OpenDateTime

    EndSub

    <tr>

    <tdnowrap="nowrap"style="width: 235px">

    System Target Date of Resolution</td>

    <tdstyle="width: 390px">

    <asp:TextBoxID="TextBox22"runat="server"Style="position: static"</asp:TextBox>

    <asp:Imagerunat="Server"ID="Image2"ImageUrl="~/images/Calendar_scheduleHS.png"/>

    <asp:RequiredFieldValidatorID="RequiredFieldValidator8"runat="server"ControlToValidate="TextBox22"

    ErrorMessage="Select Target date"Style="position: static"InitialValue="Select Date">*</asp:RequiredFieldValidator>

    <cc1:CalendarExtenderID="CalendarExtender2"runat="server"

    TargetControlID="textbox22"

    PopupButtonID="Image2"Format="dd MMM yyyy"CssClass="Calendar">

    </cc1:CalendarExtender>

    </td>

    </tr>

    <tr>

    <tdnowrap="nowrap"style="width: 235px; height: 21px">

    System Target Time of Resolution</td>

    <tdstyle="width: 390px; height: 21px">

    <asp:TextBoxID="TextBox1"runat="server"Style="position: static" </asp:TextBox>

    <asp:RequiredFieldValidatorID="RequiredFieldValidator9"runat="server"ControlToValidate="TextBox1"

    ErrorMessage="Select Target Date"Style="position: static"InitialValue="Enter Time HH:MM">*</asp:RequiredFieldValidator></td>

    <cc1:MaskedEditExtenderID="MaskedEditExtender3"

    runat="server"

    TargetcontrolID="TextBox1"

    Mask="99:99"

    Masktype=Time

    MessageValidatortip=True

    AcceptAMPM=False

    />

    </tr>