I have place an Ajax timer to databind a datalist inside an update panel. The anoying thing is that it keep doing postback everytime. I thought that having the datalist and the timer inside the update panel wouldnt cause an postback. Is there anyway to do a databind without a postback?
Thanks,
Erick
Can you provide your code for your Timer.Tick event?
Protected Sub Timer1_Tick(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Timer1.Tick MsgList.DataBind()End SubNothing fancy just the databind, still it creates a postback been inside the updatepanel
When you say it creates a postback, do you mean a full postback or a partial one? A Timer and UpdatePanelare intended to generate partial postbacks each tick, which often isn't very desirable.
gt1329a:
When you say it creates a postback, do you mean a full postback or a partial one? A Timer and UpdatePanelare intended to generate partial postbacks each tick, which often isn't very desirable.
It's a partial postback, but I have a textbox in the page outside of the update panel and if you are writing at the moment of the postback it would cut what you are writing and lose focus.
Any other way to do a databind without causing a postback?
A partial postback alone shouldn't affect focus of anything that's outside of an UpdatePanel.
Is there anything like a SetFocus() in the code that runs in the Tick event (or Page_Load, etc if they aren't wrapped in a !IsPostBack) that might be stealing focus from the TextBox when the partial postback completes?
Is the TextBox in another UpdatePanel itself, with UpdateMode not set to Conditional?
Private Sub Page_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles MyBase.Load,Me.LoadTry If Not Page.IsPostBackThen If UserInfo.DisplayName =""Then txtMsg.Enabled =False btnSend.Enabled =False Else Dim SQLActivateUserAs String ="UPDATE RoomUser SET is_active = 1 WHERE user_id = 6"Dim SQLConAs New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString)Dim SQLCmdAs New SqlCommand(SQLActivateUser, SQLCon) SQLCon.Open() SQLCmd.ExecuteNonQuery() SQLCon.Close()End If End If Catch excAs Exception ProcessModuleLoadException(Me, exc)End Try End Sub Protected Sub btnSend_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btnSend.ClickDim Username, Message, SQLInsertMsgAs String Username = UserInfo.DisplayName Message = txtMsg.Text SQLInsertMsg ="INSERT INTO Message (room_id,msg_ts,ip_addr,msg_from_userid,msg_to_userid,msg_color,msg_text,msg_type) VALUES (1,'" + Date.Now + "','192.168.1.1',6,-1,'#000000','" + Message + "',0)" Dim SQLCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SiteSqlServer").ConnectionString) Dim SQLCmd As New SqlCommand(SQLInsertMsg, SQLCon) SQLCon.Open() SQLCmd.ExecuteNonQuery() SQLCon.Close() txtMsg.Text = "" MsgList.DataBind()End Sub Protected Sub Timer1_Tick(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Timer1.Tick MsgList.DataBind()End Sub End ClassThis is my code, there is no use of setfocus()
Can you show your ASPX HTML code?
I test it and I think it can work well. The difference between your code and mine is that I use sitemap istead of using SQL server operation. My code is as following:
.cs code :
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private static int mNumber = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Timer1_Tick1(object sender, EventArgs e)
{
if ((mNumber % 2) == 0)
{
msgList.DataSourceID = "TestDataSiteMap";
}
else
{
msgList.DataSourceID = "TestDataSiteMap2";
}
msgList.DataBind();
mNumber++;
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = TextBox1.Text;
}
}
.aspx code:
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@. Import Namespace="System"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form ID="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" Width="342px"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
<asp:TextBox ID="TextBox2" runat="server" Width="339px"></asp:TextBox><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="6000" OnTick="Timer1_Tick1"></asp:Timer>
<asp:SiteMapDataSource ID="TestDataSiteMap" SiteMapProvider="TestDataSiteMap" runat="server" ShowStartingNode="false"/>
<asp:SiteMapDataSource ID="TestDataSiteMap2" SiteMapProvider="TestDataSiteMap2" runat="server" ShowStartingNode="false"/>
<asp:Panel ID="Panel1" runat="server" Height="62px" Width="207px">
<asp:ListBox ID="msgList" runat="server" Height="118px" Width="202px" DataSourceID="TestDataSiteMap"></asp:ListBox>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
<br />
</body>
</html>
And I also found there will be no effect when I am typing on a textbox outside the UpdatePanel.
No comments:
Post a Comment