I have created a Crystal Report. It is working fine. Then I tried to use it in Asp.Net using
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
DisplayGroupTree="False" />
The first time, it works fine but when I click on the print button, the report disappears and gives an error. When I move my BindReport method out of if(!IsPostBack) then it starts working fine.
Below gives error when print button is clicked:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindReport();
}
}
But this works fine
protected void Page_Load(object sender, EventArgs e)
{
BindReport();
}
Can someone help me understand what is the reason?
Insofar as only the mouse click events of the CrystalReportViewer control can be serialized into ViewState, binding to a report class that can be serialized generates an insoluble problem when reloading pages:
1 If the report binding code is placed in a Not IsPostBack conditional block the mouse click events from ViewState are retained, but the binding of the report does not take place, and an exception is thrown.
2 If the report binding code is placed outside the conditional block, the report is bound correctly, but the contents of ViewState is crushed in the process, and mouse click events are lost.
Nota : This situation occurs most often when clicks are made in a report to several pages at the CrystalReportViewer control. The report continues then mysteriously back on page 1.
Solution
Put the binding code CrystalReportViewer control in the Init event
Link : http://msdn.microsoft.com/fr-fr/library/ms225455%28v=vs.90%29.aspx
Related
I have a bunch of GridView on one page, and each have their own coordinating SqlDataSource.
They all load their coordinating data when initializing the page (on the page load). I want to Load the data for specific SqlDataSource(s) on the page when the user clicks a button, so that it is not trying to load ALL the data for all the different grids all at the same time when the page initially loads.
Note: I need to also have Paging working, and anything I have tried so far has broken the paging functionality.
Looking to tell the SqlDataSource what data to bind to it after the user clicks a button, and then have the specific GridView Bind that data to it and have the Paging functionality work with the data it was told to load.
Environment: C#, WebForms, ASP.NET, .NET 3.5, Controls SqlDataSource / GridView
From what I can tell it correctly binds the data, but the 'gridview' control keeps disappearing when I click one of the pages at the bottom for the paging.
Note: If I click the button again, it will show the correct page and the correct data for the page.
protected void btn_LoadData2_Click(object sender, EventArgs e) { Gridview2.DataSourceID = "DataSource2"; Gridview2.SelectCommand = "SELECT * FROM dbo.DataNeeded"; Gridview2.DataBind(); Gridview2.DataBind(); }
Add a Selecting event to each of your data sources. Then in the code behind check if IsPostBack and if IsPostBack is false then cancel the operation.
protected void MyDataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
e.Cancel = true;
}
I have a textbox inside a gridview.
It was initially set to Disable state.
I am enabling and disabling it in javascript.
But the problem arises during the postbacks.The grid view was able to persist the textbox data but not the Enable/Disable values.During every postback it comes back disabled even when it is enabled before the postback.
I have explored around the use of hidden field to maintain the state in these scenarious but I wish there is something less tiresome...
Can some one enlighten me on this issue...
Seeing the code could really help but generally take off the Enable="False" from the HTML then in the Page_Load event add something like:
Protected void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
tetxBox.enabled = false;
}
}
I currently have a master page that has a drop down menu which displays a different User control depending on the menu choice.
Inside of the User control I'm trying to add a submit button that processes the inputted forms inside the user controls using the "_Click()" event handler. My user control aspx looks like the following:
<asp:Button ID="configBttn" runat="server" OnClick="configBttn_Click" AutoEventWireUp="true" />
and then I have the following in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void configBttn_Click(object sender, EventArgs e)
{
Response.Write("Hello world");
}
I have even tried to add an event handler:
configBttn.Click += new EventHandler(configBttn_Click)
However this is still not allowing for the button to be triggered. Any ideas?
I add the user control inside the codebehind inside a div container:
divContainer.Controls.Add(pPage.LoadControl("~/Controls/Control.ascx"));
Where are you creating the UserControl in the Page Lifecycle?
I suspect that it needs to be created in the Init method of its container page.
In addition, they should not be wrapped in a !IsPostback block, or else when the page posts back, the control event handler is not mapped, so nothing is there to catch the click event.
Without seeing the code that adds the user control, I'm guessing that it is not saved as part of the page's view state. Which means when you click the button inside the user control the whole page gets refreshed and the user control is either not created or a different instance of it is created.
Within a tabcontainer I wish to show a variety of tabs which will contain different user controls.
I need to assign the user controls to the tabs through code, and not assign the user controls as its usually done within the tags, for instance:
<ajaxToolkit:TabPanel runat="server" HeaderText="NOMBRE" ID="TabPanel1" Enabled ="true" >
<ContentTemplate>
</ContentTemplate>
</ajaxToolkit:TabPanel>
For what I need this does not work.
So here is my code to assign the tabs to the user controls, the code is as the fallowing:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Control ctrlNombre = LoadControl("~/UserCtrl/Nombre.ascx"); //user control
TabPanel1.Controls.Add(ctrlNombre); //add user control to tabpanel
}
}
And the event “onactivetabchanged” I create a menu depending on which tab is active, which will load the control, here is the code:
protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e)
{
switch(TabContainer1.ActiveTabIndex)
{
case 0:
Control ctrl1 = LoadControl("~/UserCtrl/userControl1.ascx");
TabPanel1.Controls.Add(ctrlNombre);
break;
case 1:
Control ctrl2 = LoadControl("~/UserCtrl/ userControl2.ascx");
TabPanel1.Controls.Add(ctrlApPaterno);
}
}
However, this actually does work, the problem occurs when I clicked a certain button from some user control, this makes a full post back to the server as it would normally will do, but this post back causes the user control previously loaded disappear. What can I do to solve this? I really hope someone will help me out on this one, I will really appreciate it.
Here is an image of what happens when I clicked a button:
Thank you so much guys, I hope someone can help me solve this.
You may need to recreate the tab panels when the postback occurs in the Page_init because the tabpanels do not exist on post backs until they have been made...
So in Page_init you should call a function that makes the default tab collection and then later on the page you can add a new tab panel to the collection.
protected void Page_Init(object sender, EventArgs e)
{
Control ctrlNombre = LoadControl("~/UserCtrl/Nombre.ascx"); //user control
TabPanel1.Controls.Add(ctrlNombre); //add user control to tabpanel
}
Refer:
http://dorababu-meka.blogspot.com/2012/12/create-dynamical-ajax-tab-and-filling.html
http://www.c-sharpcorner.com/uploadfile/Ravish001/create-dynamic-tabs-using-ajax-tab-container-add-controls-read-them-dynamically/
Hope this makes sense.
Here is what I am trying to do in two simple steps:
1) New Row (trNewPost) which has table inside and controls in it to add new post or to update existing post.
Default Visible=false;
2) Add Button to make above row visible = true;
3) trMyPosts has Gridview in it and displays all the posts.
Default visible = true.
When user click on editing any row of the gridview (RowCommand event) I just want to hide this grid (trMyPosts) and show trNewPost.
That's all. events firing, but nothing happening.
I think you've got viewstate problem.
One of the things you can do is this :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// do things here
}
}
Because whenever anything happens, the page posts back. By encapsulating your Page_Load with ! Page.IsPostBack, you prevent those things from happening over and over again.
Now, if your variable is a global variable, you will have this same problem. Consider instead using a Session variable.
Also, I just wanted to show you this piece of code just in case :
protected void HideShowClicked(object sender, EventArgs e)
{
// toggle the visibility of the control
// (that is, if visible then hide, if hidden then show)
myControl.Visible = ! myControl.Visible;
}