I'm facing a weird problem with my asp.net buttons.
All was fine, I was coding a webpart with some ajax panels and buttons, and it was just so near to be finished when a wild error appeared. All my buttons were firing the same method despite I didn't told then so.
Well, I found it was due to a known issue : http://forums.asp.net/t/1567526.aspx/1
I tried to delete the called method to see if the other buttons would go back to their assigned methods, and since then, no more event is fired by buttons.
No more... Never...
I put back the method, created a new webpart with new code, restored a backup of my site, reset my server, restarted Visual Studio, And finally I'm trying to create a whole new VS solution with a whole new code. I've just a page with one lonely asp button, And nothing happens...
ascx file :
<%# Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%# Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%# Import Namespace="Microsoft.SharePoint" %>
<%# Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="PublicationUserControl.ascx.cs" Inherits="CripmeeWebParts.Publication.PublicationUserControl" %>
<asp:Button ID="Valider" runat="server" Text="Valider" />
ascx.cs file
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace CripmeeWebParts.Publication
{
public partial class PublicationUserControl : UserControl
{
protected override void OnLoad(EventArgs e)
{
EnsureChildControls();
base.OnLoad(e);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Valider.Click += new EventHandler(Valider_Click);
}
void Valider_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
}
I've tied all this :
attach event on the ascx code or on the CreateChildControls, use onCommand event, place button in a <form>, attach debugger to w3 process to see if it go through all my methods.
I'm just out of ideas...
Thank you for reading, any suggestion would be usefull.
here comes the solution I've found.
I still don't know why the event disappeared, but I notice that they are not fired only when I display the webpart from administration panel (site action>settings>webpart library> click on my custom webpart). If I add my webpart to a test page, all is working fine. If someone else have the same issue, I hope he will read this and avoid the week I've lost...
Thank you all for your time and answers :)
just put this
Valider.Click += new EventHandler(Valider_Click);
in Page_Load() or Page_Init() I can't see where you call CreateChildControls()
try
Valider= new Button();
Valider.Text = "Valider";
Valider.Click += new EventHandler(VAlider_Click);
Controls.add(Valider)
in your CreateChildControls
Your button needs an OnClick event declared
<asp:Button ID="Valider" runat="server" Text="Valider" OnClick="Valider_Click"/>
or Call your CreateChildControls in Page_Load
Related
I have following project structure where web application may contain user controls from the same web project or from a separate class library. The issue is in Default.aspx.cs, when I execute usercontrol2.SetValues(); I receive NullReferenceException since for some reason textbox2 is null. I've tried using EnsureChildControls(); but it does not help either. Should I register or invoke these usercontrols in some other way? Why does not it work out-of-box?
User control in web project
WebUserControl1.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox runat="server" ID="textbox1"></asp:TextBox>
WebUserControl1.ascx.cs
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public void SetValues()
{
textbox1.Text = "Hello";
}
}
}
User control in class library
WebUserControl2.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="ClassLibrary1.WebUserControl2" %>
<asp:TextBox runat="server" ID="textbox2"></asp:TextBox>
WebUserControl2.ascx.cs
namespace ClassLibrary1
{
public partial class WebUserControl2 : System.Web.UI.UserControl
{
public void SetValues()
{
textbox2.Text = "world";
}
}
}
Main page
Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register tagPrefix="web" tagName="WebUserControl1" src="WebUserControl1.ascx" %>
<%# Register TagPrefix="web" Namespace="ClassLibrary1" Assembly="ClassLibrary1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<web:WebUserControl1 runat="server" ID="usercontrol1"></web:WebUserControl1>
<web:WebUserControl2 runat="server" ID="usercontrol2"></web:WebUserControl2>
</asp:Content>
Default.aspx.cs
using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
usercontrol1.SetValues(); // OK
usercontrol2.SetValues(); // NullReferenceException
}
}
}
Using ascx usercontrols from a different assembly is not possible.
I think you should look at this problem this way. Your classlibrary is compiled into a dll-file. This is the only thing that is available for your webapplication. The ‘ascx’ doesn’t get compiled into the dll, and is not available.
If you really want to keep some controls separated from your web-project, I think there are a few options:
Convert your usercontrol to a customcontrol. Custom controls are a bit more difficult to create, but it is certainly not impossible. This way you really get a re-usable control.
See also this question; it addresses your problem and there’s also a link to an algorithm which ‘converts’ an ascx usercontrol to a custom control. (I didn’t test that)
Asp.NET user control referenced from another project/dll has NULL properties
Make sure your ascx files are available to the web-project, by publishing to a path in your output. Then use LoadControl to load them serverside. This could work, but I also think some of the reusability from your classlibrary is lost.
My web control doesn't appear in my web page in a different folder,
However it works in another web page in the same location.
My source code is like this :
..
<%Register src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1"
TagName="HumanUserControl"/>
...
<uc1:HumanUserControl runat="Server" id="HumanUserControl"/>
...
Error:
Request is not available in this context
The syntax on your Register tag is incorrect. And I'm not sure why you're using a ".wuc" extension instead of the default ".ascx".
You need matching <% %> tags, like so:
<%# Register Src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1" TagName="HumanUserControl" %>
If you delete the <%Register .../> and <uc1 .../> tags from your page completely, then in Visual Studio, drag-and-drop the user control from the Solution Explorer directly onto your page wherever you want the control to appear, it will automatically add the correct Register and user control tags for you.
If this doesn't clear things up, then provide more detailed error information, including the code where the exception is thrown.
The following works just fine:
~/UI/Human/HumanUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="HumanUserControl.ascx.cs" Inherits="UserControlTest.UI.Human.HumanUserControl" %>
<asp:Label ID="lblTest" runat="server" />
~/UI/Human/HumanUserControl.ascx.cs
namespace UserControlTest.UI.Human
{
public partial class HumanUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = Request.Browser.Browser;
}
}
}
~/Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UserControlTest._Default" %>
<%# Register Src="~/UI/Human/HumanUserControl.ascx" TagPrefix="uc1" TagName="HumanUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:HumanUserControl runat="server" id="HumanUserControl" />
</asp:Content>
My problem is, the aspx page is getting reloaded twice if the page having ReportViewer control.
This is the code am having in my .aspx page,
My problem is, the aspx page is getting reloaded twice if the page having ReportViewer control.
This is the code am having in my .aspx page,
<%# Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> <br><br>
and ..
<rsweb:ReportViewer ID="ReportViewer1" runat="server"
Font-Names="Verdana" Font-Size="7pt" Width="100%" Height="500px"
BackColor="#FAE28C" WaitControlDisplayAfter="10000" Visible="false" />
And this is in my code behind,
ReportDataSource rds =
new ReportDataSource
("dsSource", _PendingTicketAgeBC.SelectDailyTicketAgeInQueue(_SomeBE));
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.ReportPath = Server.MapPath(".") + "\\Somereport.rdlc";
ReportParameter paramHedaer = new ReportParameter();
paramHedaer.Name = "paramHeader";
paramHedaer.Values.Add(mHeaderparam);
ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { paramHedaer });
ReportViewer1.Visible = true;
Can anyone direct me the right way to resolve the issue. Thanks in advance.
I had this issue and was resolved by setting the AsyncRendering to false on the ReportViewer control. Note the the default value is true. Hope that helps
I'm trying to create a rendering template for my forms which need complex validations treatments.
the rendering template is working fine wwith :
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>ListForm</Display>
<Edit>ChaisTemplate</Edit>
<New>ChaisTemplate</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
and with my ascx :
<%# Control AutoEventWireup="true" CodeBehind="ChaisTemplate.ascx.cs"
Inherits="TransactionsFormsTemplates.ControlTemplates.ChaisTemplate, TransactionsFormsTemplates"
Language="C#" %>
<%# Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Namespace="Microsoft.SharePoint.WebControls" %>
<%# Register TagPrefix="wssuc" TagName="ToolBar" Src="~/_controltemplates/ToolBar.ascx" %>
<%PageLoad(this, null);%>
<SharePoint:RenderingTemplate ID="ChaisTemplate" runat="server">
.....
but I can't have any control mappings in my code behind (in the webapp bin with cas policies, and it's correctly deployed)
this :
<asp:TextBox runat="server" ID="test"></asp:TextBox>
is null when it comes to :
public partial class ChaisTemplate : System.Web.UI.UserControl
{
protected TextBox test;
so everytime I call test.Text on functions, I get a nullreferenceexception, because test is never mapped to my ascx textbox. why ?
plus, the PageLoad is never called like in classic asp.net pages, even with <%PageLoad(this, null);%> at the beginning.
however every event works :
<asp:Button runat="server" ID="button" OnClick="button_OnClick"/>
this will actually call button_OnClick in my code behind. But all my properties are null because not mapped.
did I miss something?
I'v been doing it like this. Take this as an example RenderingTemplate:
<SharePoint:RenderingTemplate ID="ParentItemsListView" runat="server">
<Template>
<table cellpadding=0 cellspacing=0>
<tr><td nowrap class="UserGenericHeader"><asp:Label ID="lblParentItems" runat="server" /></td></tr>
<tr><td><SharePoint:ListViewByQuery ID="listViewByQuery" runat="server" /><br /></td></tr>
</table>
</Template>
</SharePoint:RenderingTemplate>
Then in Render event i can reference these controls like this:
[SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
protected override void Render(HtmlTextWriter output)
{
if (this.Visible)
{
Label lblParentItems = this.TemplateContainer.FindControlRecursive<Label>("lblParentItems");
lblParentItems.Text = "Pievienotie " + this.ChildList.Title;
ListViewByQuery listView = TemplateContainer.FindControlRecursive<ListViewByQuery>("listViewByQuery");
listView.List = this.ChildList;
...
base.Render(output);
}
}
Ahh, yes, i use a handy extension function
public static T FindControlRecursive<T>(this Control parentControl, string id) where T : Control
{
T ctrl = default(T);
if ((parentControl is T) && (parentControl.ID == id))
return (T)parentControl;
foreach (Control c in parentControl.Controls)
{
ctrl = c.FindControlRecursive<T>(id);
if (ctrl != null)
break;
}
return ctrl;
}
However i had an issue where i inherited from SaveButton that i couldn't reference a control. In that case i changed so that i inherit from FormControl and i could find my control under this.Controls.
Also note that you cannot reference controls up until OnLoad event has executed (i.e if you're in OnLoad function, call base.OnLoad(e) before referencing controls.
Why is it so? I guess because it's not like you are having a template and then code behind (working with the same class/object), but it's more like you are rendering that ascx template in a different class/object file.
I can disable viewstate of each control, but not entire page.
Is there a way to disable viewstate in whole page?
Set Page.EnableViewState to false. This can be done either in the code-behind or in the page directive:
<%# Page EnableViewState="false" %>
You can also disable ViewState at the application level by setting the enableViewState attribute of the pages node to false in your web.config:
<pages enableViewState="false"/>
You can do it in the page declaration:
<%# Page EnableViewState='false' %>
private void Page_Init(object sender, System.EventArgs e)
{
this.EnableViewState = false;
}
You can set Page.EnableViewState="false"
But no matter what there will be some very small ViewState footprint on any .aspx page you create.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" EnableViewState="false" %>