I need help with something, it might be a bit complicated. I call a WebMethod that does somethings from inside a js file. One of the things it does, is that it calls some functions that are located in a class within the app_code.
In that class in app_code I get a GridView Control that is on my page and I need to update it, so I do this:
ActiveGridView.DataSource = (ActiveDataTable).DefaultView;
ActiveGridView.DataBind();
The above works fine when I call the functions in app_code from a regular function, but doesn't update the grid when I call it within the WebMethod.
Any ideas how to solve this problem?
Please note that I CAN'T have the WebMethod return the data and then on client side to update the grid.
Edited
Actually I am using a updatePanel, my aspx code looks like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="MappedDataGrid" runat="server" CssClass="pipesTbl">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And again this works fine when I call the fill grid from an event on the page, but then when I call it after a webMethods calls the class in app_code that is spouse to fill the grid, it doesn't get refilled.
Edited again
I think the problem is with this line:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
</Triggers>
I tried changing it to this:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="MappedDataGrid" EventName="DataBinding" />
</Triggers>
But didn't work, I am probably missing something, and since this is all new to me, I didn't know what to do.
What should I be doing now in order to make it work as you said?
If you call a method which manipulates a ASP.NET page out of the lifecycle of that page, you won't view the result. If you want to bind data to a ASP.NET control, it needs to be in the lifecycle of the page, and if you call the method from javascript throught a webmethod, that method will be executed out of the lifecycle.
Try to fill the datagrid with AJAX embedding it in a UpdatePanel.
When you call a WebMethod you are not actually loading the page and running through the page lifecycle. Even though your method is part of your page codebehind class, when you call it in this way your controls on the page are not going to be available. It's just a shortcut for making a web service out of a method in your page class.
Based on what you say you want to do, you may want to look at using an UpdatePanel. With them, it IS actually doing a full postback to the page and you can change whatever is necessary with your page controls.
Update
The point with the UpdatePanel is that you won't use your WebMethod anymore. You will just click a normal button that is a trigger for the update panel, and you will update your grid like normal in the code behind (handle the click event of the button and call your code to bind the grid). Since only the grid is in the panel, only it will get refreshed on the screen.
Related
at my webforms.app on .net 4.0 after call _doPostBack via
aspx code
<%= GetPostBackReference() %>;
on code behind
protected string GetPostBackReference()
{
return Page.ClientScript.GetPostBackEventReference(ReloadThePanel,"null");
}
transformed to
__doPostBack('ctl00$cntMain$ReloadThePanel','null');
in form
<!-- update panel -->
<asp:UpdatePanel ID="updPanelSave" ClientIDMode="Static" runat="server">
<ContentTemplate></ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ReloadThePanel" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<!-- hidden button for update panel -->
<asp:LinkButton ID="ReloadThePanel" runat="server" style="display:none;" />
after first ajax postback is all ok, after another one ajax postback on same page it throw js exception
form.__EVENTTARGET.value undeffined
in (Scriptresource.axd ... dynamic code)
_doPostBack: function PageRequestManager$_doPostBack(eventTarget, eventArgument) ....
form.__EVENTTARGET.value = eventTarget;
form.__EVENTARGUMENT.value = eventArgument;
this._onFormSubmit();
what i do wrong ? Its maybee same isue with http://www.hanselman.com/blog/IE10AndIE11AndWindows81AndDoPostBack.aspx
problém is only on ie 11,8 usw.. Chrome and FF works well
Thanx
Easiest thing to do is upgrade to 4.5.1 - it may be that issue you mention and that'd solve it.
Seems strange that it happens AFTER and initial postback though (and doesn't tie in with the issue above).
Personally I don't see much point in creating the postback reference by using a hidden button (and suspect that's where it's getting itself confused). You have blank content template here, but there is probably something contained within in the real code, make sure you don't have any unmatched div tags as that would push the button into the panel and you'd lose reference to it.
I'm guessing that you must be calling the postback from javascript (otherwise why use a hidden button), so why not just use: . You might want to populate the id programatically (in aspx : ', '');"> )
I know it seems nasty including direct references to __dopostback - but the day M$ change that is the day that 90% of webforms websites will go down anyway, so it's pretty unlikely.
I'm using Ajax Update panel, now i'm trying to transfer from one aspx page to another apsx page by using server.transfer , but it is giving error.
I have tried
Server.Transfer("User.aspx");
Add this to your update panel if you don't have it.
<Triggers>
<asp:PostBackTrigger ControlID="btnYourButtonId"/>
</Triggers>
Remember to keep it asp:PostBackTrigger instead of asp:AsyncPostBackTrigger. Latter doesn't work with server.transfer.
i am tried like this, i take an button control which display is none:-
<div style="display:none">
<asp:Button ID="loginbtn" runat="server" OnClick="loginbtn_Click" />
</div>
Then I call the click event of the button through jquery:-
$("input[id$=loginbtn]").click();
in Server side I call button event:-
protected void loginbtn_Click(object sender, EventArgs e)
{
}
Its working fine,But,it reload the page, I need to Stop it , Please give me some solution how to call the server side method from client side in DNN
In this case (I'm assuming you are developing a module for DotNetNuke); The easiest way is to check "Supports Partial Rendering" in your module control. Then it will be wrapped by an Update Panel.
Host -> Extensions -> Edit your module -> Module Definitions -> Module Controls -> Edit your module control -> Check Supports Partial Rendering
However, if a control already contains JavaScript for dynamic behaviors, wrapping the control in an Update Panel has the potential to break the control's functionality.
There are a couple of ways to do this, say, using an UpdatePanel or changing your mechanism to use AJAX directly with WebMethod methods; let me introduce the easiest example for your case before you consider more. Wrap your content in an update panel, as such:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<!-- stuff to reload here -->
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="loginbtn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
So, then the other option is to use jQuery's ajax or equivalents (such as post) to submit your data programatically (rather than somewhat macroing the UI - which might not even work in some browsers such as versions of IE). The AJAX call can return data and you can populate your pieces in place as required.
I have a page that above it I want to have a panel that automatically refresh every 5 minutes, and if a user has a message in his inbox show to him. What is the best solution for this?
Should I usel AJAX, jQuery, or JavaScript? My preferred solution is server side solution.
Since you are working with ASP.Net, you can also achieve this behavior by using a combination of the following:
ScriptManager or ScriptManagerProxy (if you have nested pages):
Manages the ajax calls
UpdatePanel: Determines what gets updated. Controls nested within <ContentTemplate> are subject to partial
updates
Triggers: Controls when the content is updated.
For your purpose, a Timer control can be used as a trigger to ensure that partial postbacks are triggered every 5 seconds:
<asp:ScriptManager ID="scriptManagerMain" runat="server"/>
<asp:Timer ID="timer" Interval="5000" runat="server"/>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Panel ID="panelToBeUpdated" runat="server">
<asp:Label ID="lblContent" runat="server" ></asp:Label>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer" />
</Triggers>
</asp:UpdatePanel>
I would use Jquery to send an AJax request to the server to fetch the updated content. Upon receiving the content I would use JQuery again to update the markup.
You can set this up to trigger every 5 min using setInterval in Javascript
Hard to give a specific example without code. But you basically need to load the new content with an ajax request that is triggered by a timer.
setInterval(function(){
$.ajax({
url: "someUrlThatServesUpdatedContent.aspx",
cache: false
}).done(function( html ) {
$("#results").html(html);
});
}, 300000);
The above is just a simple example to point you in the right direction.
Edit: Here is an example of how to do the ajax call without JQuery
https://stackoverflow.com/a/2792721/1059001
It's possible to do that with AJAX using method described by previous answers, but if you wish to have a server side solution I would recommend loading that part of the page in an iframe, with meta refresh:
<meta http-equiv="refresh" content="300">
This method however would make it difficult to communicate any events or user actions back to main page.
how to fill textbox in asp.net while i am typing in another text
.. changes in one textbox will affect in another textbox auto.. and without refreshing my page.
Ok, try this. You will need the AJAX Control Toolkit. So read the article Installing AJAX Control Toolkit 4 in Visual Studio 2010 to see how to install it in Visual Studio.
Then you need to add a ScriptManager to your ASPX page. You will need to add the following code:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
What you then need to do, is add an UpdatePanel to your page. Inside this update panel, you need to place the textbox. This means that only the controls inside the update panel will refresh, and not the whole page. To do this, add the following code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<!--Add your Textbox Control to update here: Textbox1-->
<asp:TextBox ID="Textbox1" runat="server" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="Textbox2" runat="server" ReadOnly="True" ontextchanged="Textbox2_TextChanged"></asp:TextBox>
</ContentTemplate>
<Triggers>
<!--This is the textbox you will be typing text into: TextBox2-->
<asp:AsyncPostBackTrigger ControlID="Textbox2" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
The Trigger tells your page which control on the form needs to initiate the postback. Now in your .cs file, you need to add the event handler for the Textbox2 TextChanged event. Add the following code:
protected void Textbox2_TextChanged(object sender, EventArgs e)
{
// Set the text of textbox1 = textbox2
}
I hope that this helps.
No need for AJAX. JQuery is enough. The code will do it
$('#text1').bind('keyup', function(){
$('#text2').val($('#text1').val());
});
Assuming
text1 id of box writing into.
text2 textbox that text gets copied to
in .Net you will have to use client id to get the correct id so it may look like this
$('<%=text1.ClientID%>').bind('keyup', function(){
$('<%=text2.ClientID%>').val($('#text1').val());
});
Oh and wrap it up in $(document).ready as per standard. And of coures you need to include the JQuery library to your page.
No posting back or page refreshes at all. It's your lightest solution and easy to implement.
You will need to use Javascript to accomplish this. ASP.Net code runs on the server side, which means it cannot affect the page without a postback happening first. Read up on the OnTextChanged event and how to hook into it with javascript. There is a javascript library called jQuery which makes everything easier, though it isn't strictly necessary.
Use JQuery. You may need to make an AJAX call to the server if you are relying on a datasource such as a database to autofil this field.