UpdatePanel only update from events - asp.net

hello every one my problem goes like this
i have two updatepannels
<asp:UpdatePanel ID="new_word_panel_UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate >
<asp:Panel ID='new_word_panel' runat="server">
<asp:Button ID='new_word_btn' runat="server" Text='give me a new word' Visible='false' OnClick='GenNewWord' />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate >
<asp:Button ID='Button8' runat="server" Text='hide' OnClick='hidegive' />
</ContentTemplate>
</asp:UpdatePanel>
the first updatepanel UpdateMode is set to Conditional so i can
update is content from the second updatepanel asp:Button ID='Button8' OnClick='hidegive'
event easily by using the Update() method.
this is the eventhandler:
protected void hidegive(object sender, EventArgs e)
{
if (new_word_btn.Visible == true)
new_word_btn.Visible = false;
else
new_word_btn.Visible = true;
**new_word_panel_UpdatePanel.Update();**
}
my problem is that i cant update the first UpdatePanel from reguler method on my page
although i am using the Update() method, i have try to update panel from this
method and nothing hapeens:
void PlayerWinEventHandler(object sender,Game.PlayerWinEventArgs e)
{
Session["score"] = int.Parse(Session["score"].ToString()) + 10;
UpdateScore();
if (new_word_btn.Visible == true)
new_word_btn.Visible = false;
else
new_word_btn.Visible = true;
new_word_btn.Text = "zibi";
**new_word_panel_UpdatePanel.Update();**
}
thanks for your help...

Why dont you use Triggers in your first update panel.
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button8" EventName="Click"/>
</Triggers>

Related

how to create 2 minute countdown timer using asp.net

i want to create a 2 minute timer under text box using Asp.net, i wanted the timer to be in a label under the text box, so i wrote this code in the control page after double click in the timer icon:
int seconds = int.Parse(Label1.Text);
if (seconds > 0)
Label1.Text = (seconds - 1).ToString("mm:ss");
else
Timer1.Enabled = false;
And this code in the aspx file:
<span "CodeAsk">ألم تتلقى الرمز ؟</span><asp:Label ID="Label1" runat="server" text="2"></asp:Label>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="120000"></asp:Timer>
but it's not working, what is the problem with my code?
but it's not working, what is the problem with my code?
Timer needs to be used in conjunction with the UpdatePanel control. You can trigger the ontick event of the timer through the AsyncPostBackTrigger method of the updatepanel.
And for converting the text of the Label into the form of a timer, you need to use TimeSpan time = TimeSpan.FromSeconds(seconds); to achieve, you can refer to this.
<form runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span id="CodeAsk">ألم تتلقى الرمز ؟</span><br />
<asp:Label ID="Label1" runat="server" Text="2"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</form>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TimeSpan time = TimeSpan.FromSeconds(Convert.ToInt32(Label1.Text) * 60);
string str = time.ToString(#"hh\:mm\:ss");
Label1.Text = str;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
TimeSpan result = TimeSpan.FromSeconds(TimeSpan.Parse(Label1.Text).TotalSeconds - 1);
string fromTimeString = result.ToString(#"hh\:mm\:ss");
Label1.Text = fromTimeString;
}
Here is the test result:

how to do not postback button click

I want to prevent this phenomenon ...
Postback when button is clicked
I want to perform the result when the button is clicked and prevent the screen refresh.
<asp:Button ID="DomainSeachButton" runat="server" Text="search"
OnClick="btnDomainSearch_Click" OnClientClick="onMySearch();" Width="69px" />
function onMySearch() {
// __doPostBack("DomainSeachButton", "client");
var r = confirm("Press a button!")
if (r==true)
{
alert("You pressed OK!")
return true;
}
protected void btnDomainSearch_Click(object sender, EventArgs e)
{
if (_cDBConnect.IsValidDBInfo() == true)
{
string sql = string.Format("select * from tb_licensekey_storages where cert_domain_name like '%{0}%';", txtSearchDomain.Text.Trim());
var da = new SQLiteDataAdapter(sql, _cDBConnect.GetConnectionString());
DataTable dt = new DataTable();
da.Fill(dt);
gridViewDBInfo.DataSource = dt;
gridViewDBInfo.DataBind();
}
}
You can easily achieve this with ASP Control UpdatePanel it will not actually refresh the whole page but the the selected content of the page, for example:
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="updtpnl" runat="server">
<ContentTemplate>
<asp:Button
ID="DomainSeachButton"
runat="server"
Text="search"
OnClick="btnDomainSearch_Click"
OnClientClick="onMySearch();"
Width="69px"
AutoPostBack="true"
/>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DomainSeachButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Try changing your btnDomainSearch_Click to
$("#btnDomainSearch_Click").click( function(e){
e.preventDefault();
//Whatever
})
You can use AutoPostBack="false" in the particular button.
if AutoPostBack set false it will not send request to server otherwise it will send request to server.
<asp:Button ID="DomainSeachButton" runat="server" Text="search" AutoPostBack="false" OnClick="btnDomainSearch_Click" OnClientClick="onMySearch();" Width="69px" />
Please read this article for more information More Details

Timer1_tick is not working until postback event

As per topic in discussed. I've assigned one Timer1_Tick (AJAx Control) into my web form user control but it is not working right until i've perform "__doPostBack" in client side and it is not be able to perform automatically once the form is loaded.
Code in markup:
<asp:UpdatePanel ID="TimeSheetUpdate" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TimeSheetTimer" EventName="Tick"/>
</Triggers>
<ContentTemplate>
<asp:Timer ID="TimeSheetTimer" Interval="1000" runat="server" OnTick="TimeSheetTimer_Tick" ClientIDMode="Static"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
protected void TimeSheetTimer_Tick(object sender, EventArgs e)
{
File.WriteAllText(#"C:\checkTimesheetcontrol.txt", "");
File.AppendAllText(#"C:\checkTimesheetcontrol.txt", "Check :" + DateTime.Now.Second);
return;
}
I need it to be perform ealier once the control in loaded in page.How Can I do for it?

How to make UpdatePanel to do Update after custom Event?

I'm setting UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional; to make manual updates but it doesn't work for some custom events, when I have some event alike here:
protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
UpdatePanel1.Update();
}
My project is failing with:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Internals.dll
Additional information: The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.
even if I set ChildrenAsTriggers or not. Error message is not clear for me and I can't understand what should I do to process update right after I process my event?
addition:
aspx:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:ListView ID="ListView1" runat="server">
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
I suppose you should change your markup like this
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
....
You should set UpdateMode="Conditional" in your markup itself
protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
// Try to call the update method after calling the custom even but in the click event of the button. Ensure you update the Trigger accordingly in the update panel
**UpdatePanel1.Update();**
}
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
}
Try to add AsyncPostBackTrigger to the update panel with update mode as conditional
Although you are doing same thing explicitly.
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
Just to check if there is any other issue, you can set the updateMode property of the update panel to "Always"

UpdatePanel not refreshing

I'm a newbie with ASP.NET, so probably my question is easy... but I'm wasting a lot of time with no success.
Part of my page is
<asp:UpdatePanel ID="pnlFileUpload" runat="server">
<ContentTemplate>
<ajaxToolkit:AsyncFileUpload ID="upload" runat="server" OnUploadedComplete="upload_UploadedComplete"
OnUploadedFileError="upload_UploadedFileError" UploaderStyle="Modern" UploadingBackColor="Yellow"
Width="400px" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="pnlFileError" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblFileError" runat="server" Text="errFile" Visible="false"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="upload" EventName="UploadedComplete" />
</Triggers>
</asp:UpdatePanel>
When user uploads (started with ajaxToolkit:AsyncFileUpload) ends, upload_UploadedComplete is called.
A sample code is:
protected void upload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
int size = upload.PostedFile.ContentLength;
if (size > maxsize)
{
lblFileError.Text = 'File too big...';
lblFileError.ForeColor = System.Drawing.Color.Red;
lblFileError.Visible = true;
pnlFileError.Update();
}
}
But lblFileError is never shown... why?!?
AsyncFileupload doesn't need updatePanel ( none of AJAx toolkit controls need ) . as they have it as a built in . remove fileupload UpdatePanel and test again
:
it seems you can't do that in this way . based on AjaxControlToolkitSampleSite to show user a message about uploading file you should use "ScriptManager.RegisterClientScriptBlock" as below :
protected void upload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
int size = upload.PostedFile.ContentLength;
if (size > maxsize)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "error", "top.$get(\"" + lblFileError.ClientID + "\").innerHTML = 'File too big...';", true);
}
}

Resources