Hi i am loading huge data by asp.net Ajax on button click in grid view showing loading message on update prgress ...in update progress time i have to disable my BtnLoadReport Button.
<td>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:UpdateProgress ID="updProgress"
AssociatedUpdatePanelID="UpdatePanel1"
runat="server" oninit="updProgress_Init" onprerender="updProgress_PreRender">
<ProgressTemplate>
<span style="color:green; font-style:italic;">Loading, please wait...</span>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<div id="DemoId">
<asp:Button ID="BtnLoadReport" runat="server" onclick="BtnLoadReport_Click"
Text="LoadReport" Width="176px" ClientIDMode="Static" OnClientClick="return clickOnLoadReport();" />
</div>
<asp:GridView ID="GridView1" runat="server" Height="170px"
onselectedindexchanged="GridView1_SelectedIndexChanged" Width="438px">
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnLoadReport" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
my script to disable button
<script type="text/javascript">
var updateProgress = null;
function clickOnLoadReport() {
// This will disable all the children of the div
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack()==false) {
var nodes = document.getElementById("DemoId").getElementsByTagName('*');
for (var i = 0; i < nodes.length; i++) {
nodes[i].disabled = true;
// nodes[i].off("click");
}
}
}
</script>
but it is not disabling my button for long it is disabling it for just seconds
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(this.BtnLoadReport);
}
protected void BtnLoadReport_Click(object sender, EventArgs e)
{
// System.Threading.Thread.Sleep(3000);
UpdatePanel1.Update();
DataSet dataSet = new DataSet();
dataSet.ReadXml(#"C\Data.Xml");
GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void updProgress_Init(object sender, EventArgs e)
{
BtnLoadReport.Enable = false;
}
protected void updProgress_PreRender(object sender, EventArgs e)
{
BtnLoadReport.Enable
= true;
}
this above thing i tried to disable my button BtnLoadReport on Update progress still not working
Thanks in Advance
function clickOnLoadReport() {
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(CancelPostbackForSubsequentSubmitClicks);
function CancelPostbackForSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == 'BtnLoadReport') {
args.set_cancel(true);
document.getElementById("BtnLoadReport").setAttribute("disabled", "disabled");
//alert('A previous request is still in progress that was issued on clicking ' + args.get_postBackElement().id);
}
}
}
i made changes in my javascript function it solve my problem
Related
There are several posts on here with similar titles, but none that I have found actually exhibit the same behavior I'm seeing. I'm using buttons with MultiView as my navigation to give the appearance of tabs. The page loads, no problem. I can switch tabs, no problem. The issue I'm having occurs only when I press a different navigation button while a gridview is actively loading. If I wait for the gridview to fully load, I get no errors.
The full error I'm receiving is: Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
If I add the following, it does resolve my issue. However, I'm trying to avoid this if at all possible.
<%# Page ... EnableEventValidation = "false" />
default.aspx
<form id="form1" runat="server">
<table width="100%" align="center">
<tr style="background-color:#E9E9E9;">
<td>
<asp:Button Text="Tab1" BorderStyle="None" ID="Tab1Button" CssClass="Initial" runat="server"
OnClick="Tab1Button_Click" />
<asp:Button Text="Tab2" BorderStyle="None" ID="ConflictButton" CssClass="Initial" runat="server"
OnClick="ConflictButton_Click" />
<asp:Button Text="Tab3" BorderStyle="None" ID="Tab3Button" CssClass="Initial" runat="server"
OnClick="Tab3Button_Click" />
<asp:Button ID="AffiliateAddButton" runat="server" Text="Add" />
<asp:MultiView ID="MainView" runat="server">
<asp:View ID="View1" runat="server">
<table class="TabContent"><tr><td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="SqlDataSource1">
</asp:GridView>
</td></tr></table>
</asp:View>
<asp:View ID="View2" runat="server">
<table class="TabContent">
<tr>
<td>
View 2
</td>
</tr>
</table>
</asp:View>
<asp:View ID="View3" runat="server">
<table class="TabContent">
<tr>
<td>
View 3
</td>
</tr>
</table>
</asp:View>
</asp:MultiView>
</td>
</tr>
</table>
</form>
default.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Tab1Button.CssClass = "Clicked";
MainView.ActiveViewIndex = 0;
LoadGrid();
}
}
protected override void Render(HtmlTextWriter writer)
{
// Register controls for event validation
foreach (Control c in this.Controls)
{
this.Page.ClientScript.RegisterForEventValidation(
c.UniqueID.ToString()
);
}
base.Render(writer);
}
private void LoadGrid()
{
SqlDataSource1.CancelSelectOnNullParameter = false;
GridView1.DataSourceID = null;
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
private void ButtonsControl(string tab)
{
if(tab == "Tab1")
{
AffiliateAddButton.Visible = true;
Tab1Button.CssClass = "Clicked";
ConflictButton.CssClass = "Initial";
Tab3Button.CssClass = "Initial";
LoadGrid();
}
if (tab == "Tab2")
{
AffiliateAddButton.Visible = false;
Tab1Button.CssClass = "Initial";
ConflictButton.CssClass = "Clicked";
Tab3Button.CssClass = "Initial";
GridView1.DataSourceID = null;
GridView1.DataBind();
}
if (tab == "Tab3")
{
AffiliateAddButton.Visible = false;
Tab1Button.CssClass = "Initial";
ConflictButton.CssClass = "Initial";
Tab3Button.CssClass = "Clicked";
GridView1.DataSourceID = null;
GridView1.DataBind();
}
}
protected void Tab1Button_Click(object sender, EventArgs e)
{
ButtonsControl("Tab1");
MainView.ActiveViewIndex = 0;
}
protected void ConflictButton_Click(object sender, EventArgs e)
{
ButtonsControl("Tab2");
MainView.ActiveViewIndex = 1;
}
protected void Tab3Button_Click(object sender, EventArgs e)
{
ButtonsControl("Tab3");
MainView.ActiveViewIndex = 2;
}
What I ended up doing was two things:
Added paging. Ideally I didn't want this in this specific scenario but limiting my page to 500 lines made it load fast enough to almost eliminate this.
Switched from multiview to frameset. Again, not an ideal option, but works in my given scenario.
i wanted to use a timer in a program i'm working on, but it always stops after 1 tick !! can you give me any tips to make it repeat unstoppably (or until i want it to) please ??
this is my code:
int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "(20)";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text = "(" + i + ")";
if (i == 0)
{
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
Edit: my HTML code :
<form id="form1" runat="server">
<div><center>
<span class="style1">message</span><br
class="style1" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" >
</asp:Timer>
<asp:Timer ID="Timer2" runat="server" Interval="1000">
</asp:Timer>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
PostBackUrl="~/Acceuil.aspx">Retour</asp:LinkButton>
<br />
</center>
</div>
</form>
Edit2: what i want to do is the normal redirecting code, count to 20 and refreshing every second (to show to user how much secs left) and at the end it redirects to a new page, but it wasn't working, so i was wondering why ??
and i think i got my answer from #Andrei Rînea, and thank you everyone for your help : )
Edit3: Solved and here is the code :
static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Label1.Text = "20";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text = i.ToString();
if (i == 0)
{
i = 20;
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
}
Thanks everyone again : )
Please use the following code :
static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
Label1.Text = "(20)";
}
protected void Timer1_Tick(object sender, EventArgs e)
{
i--;
Label1.Text =i.ToString();
if (i == 0)
{
Session["Profession"] = "Visiteur";
Response.Redirect("Acceuil.aspx");
}
}
your HTML code should be like :
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
I'm going to assume here that you are wishing to have a process that ticks away on your server in the background.
I'm also going to assume you've tried to add this to your Webform.
If so, the issue you have encountered, is that your Webform object only exists for the short time that it is processing your request, after which it is disposed of - including your timer.
If I'm correct, you'd probably like to take a look at Quartz.Net:
http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net
You are trying to run client side code on the server. You need to do this in JavaScript instead.
The C# code will be run just before rendering the page and not longer. You probably believe the code will run as long as the visitor is on the page which is not the case.
I'm trying to post data to the same page after postback with the help of ViewState. I fill out the contents of the page and post the form and display the input on the same page but the validator says that the firstname cannot be null while I just filled out the textbox before I submitted the form.
Default.aspx
<asp:Content ContentPlaceHolderID="Main" runat="server" EnableViewState="true">
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:Label ID="lbl1" Text="First Name" runat="server" />
</td>
<td>
<asp:TextBox ID="tFirstName" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ControlToValidate="tFirstName" ErrorMessage="This field cannot be empty."
runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbl2" Text="Last Name" runat="server" />
</td>
<td>
<asp:TextBox ID="tLastName" runat="server" />
</td>
</tr>
</table>
</div>
<asp:Button ID="id" Text="Submit" runat="server" OnClick="Submit" />
<div>
<p>
<asp:Label ID="lTest" runat="server" /></p>
</div>
<asp:Label ID="lSubmit" runat="server" />
</form>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
tFirstName.Text = (string)ViewState["tFirstName"];
tLastName.Text = (string)ViewState["tLastName"];
}
else
{
ViewState["tFirstName"] = tFirstName.Text;
ViewState["tLastName"] = tLastName.Text;
}
}
protected void Submit(object sender, System.EventArgs args)
{
try
{
lTest.Text = tFirstName.Text + " " tLastName.Text;
}
catch (Exception ex)
{
throw;
}
}
It seems my logic was wrong. The correct condition should be Page_Load() function:
if (!Page.IsPostBack)
{
tFirstName.Text = (string)ViewState["tFirstName"];
tLastName.Text = (string)ViewState["tLastName"];
}
It should be if (!Page.IsPostBack) instead of if (Page.IsPostBack)
:D
Have fun!
You are getting too confuse on this,
Below is the possible way to solve your issues. Before this, please learn about postback in page load
Seems you are passing the null values to the ViewState,
if (Page.IsPostBack)
{
tFirstName.Text = (string)ViewState["tFirstName"];
tLastName.Text = (string)ViewState["tLastName"];
}
else
{
ViewState["tFirstName"] = tFirstName.Text;
ViewState["tLastName"] = tLastName.Text;
}
Proceed as following
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostback==true)
{
if(ViewState["tFirstName"]!=null && ViewState["tLastName"]!=null)
{
tFirstName.Text = (string)ViewState["tFirstName"];
tLastName.Text = (string)ViewState["tLastName"];
}
}
}
protected void Submit(object sender, System.EventArgs args)
{
try
{
ViewState["tFirstName"] = tFirstName.Text;
ViewState["tLastName"] = tLastName.Text;
lTest.Text = tFirstName.Text + " " tLastName.Text;
}
catch (Exception ex)
{
throw;
}
}
I only want to point out, that you do NOT have to reassign values to the textboxes after a Postback. This is the normal asp.net web control behavior when EnableViewstate is turned on.
Remove your code from Page_Load and you should notice that textboxes remember values after a postback (unless you specifically remove it in behind code like tFirstName.Text = "")
I have a webs form page and I have a text box which once clicked passes a variable to the code behind and then back into another element of that page and I cannot get it to work.
This is the closest I have got.
<asp:Panel ID="Search" runat="server" Visible="true">
<tr>
<td>
<asp:Label ID="lblSearch" runat="server" Text="Search"></asp:Label>
</td>
<td>
<asp:TextBox ID="search" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="valSearch" runat="server"
ControlToValidate="movieSearch" Text="Please enter text" />
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Save"
OnClick="btnSubmit_Click" />
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlSearchResult" runat="server" Visible="false">
<script>
var search = '<%=Server.UrlDecode(Request.QueryString["Data"]) %>';
</script>
</asp:Panel>
And the code behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
pnlSearch.Visible = false;
pnlSearchResult.Visible = true;
Response.Redirect("search.aspx?Data=" + Server.UrlEncode(search.Text));
}
}
Also this does not change the visibility of the two panels for some reason.
I would appreciate any guidance I am very new to asp and c#.
The panel's visibility is not changing because you're forcing a new GET request to the page with this: -
Response.Redirect("search.aspx?Data=" + Server.UrlEncode(search.Text));
(I'm assuming your page is called 'search.aspx')
There's no need to do this. Remove this line.
Secondly, I see you want to force the textbox's Text value into a Javascript variable. Replace this
var search = '<%=Server.UrlDecode(Request.QueryString["Data"]) %>';
with this
var search = '<%= search.Text %>';
Write below code on page event.
Another more important point is you first panel Id is "Search" not "pnlSearch" on aspx page so please correct it
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Data"] != null)
{
Search.Visible = false;
pnlSearchResult.Visible = true;
}
}
I recommend solution without using Response.Redirect.
Code Behind Submit Button click:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (IsValid)
{
pnlSearch.Visible = false;
pnlSearchResult.Visible = true;
}
}
In Markup:
<asp:Panel ID="pnlSearchResult" runat="server" Visible="false">
<script>
var searchTxt = document.getElementById('search');
if(searchTxt.value != null && searchTxt.value != ''){
//do your stuff
}
</script>
I can get both the Javascript and the C# function to work just fine.
However, my Javascript function runs before the C#.
How do I get it to run after the C# function??
Here is my code:
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div id="div2" style="height:70px; width:auto; text-align:center;">
<p><b>This is A View!!!</b></p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="div1">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"
OnClientClick="javascript:Highlit()" />
</div>
</asp:View>
</asp:MultiView>
<script type="text/javascript">
function Highlit()
{
$("#div2").effect("highlight", {}, 10000);
}
</script>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
}
}
}
Here is the code reflecting changes from the answers:
Code behind
namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
ScriptManager.RegisterStartupScript(this, this.GetType(), "TEST", "Highlit();", true);
}
}
}
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div id="div2" style="height:70px; width:auto; text-align:center;">
<p><b>This is A View!!!</b></p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="div1">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
function Highlit() {
$("#div2").effect("highlight", { color: "#9499FC" }, 10000);
}
</script>
The only way to get the javascript to run after is to add a script reference in your Button1_Click event.
Example Code for standard postback:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
Page.ClientScript.RegisterStartupScript(this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}
As noted by others, be sure to remove your OnClientClick event. Also, consider moving your "Highlit" script outside of the update panel.
Additionally, since you are in an update panel, you will need to use the following example code for a Partial Postback:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Changed";
ScriptManager.RegisterStartupScript(this, this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}
You have to register a ClientScript at the end of Button1_Click event
and remove OnClientClick="javascript:Highlit()"
protected void Button1_Click(object sender, EventArgs e)
{
//Do stuff
ScriptManager.RegisterStartupScript(this, this.GetType(), "ANYNAME", "javascript:Highlit();", true);
}
Remove the OnClientClick attribute, and add the call as a startup script, so that it runs after the page has loaded:
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = "Changed";
Page.ClientScript.RegisterStartupScript(this.GetType(), "start", "Highlit();", true);
}
Side note: When you use the OnClientClick attribute, the code should not start with javascript:. That's only used when you put script in a href attribute in a link.