I have created a .NET UserControl and exposed as a COM Control. It can be used in a html web page with object tag
Html Object tag looks like
<OBJECT id=control01 class=MyControl classid=clsid:C2A76664-69FA-45C8-B052-7322B14CB206></OBJECT>
And my .Net Control code look like below
[ClassInterface(ClassInterfaceType.AutoDual),
ComVisible(true),
Guid("C2A76664-69FA-45C8-B052-7322B14CB206"),
ProgId("MyControl")]
public partial class MyControl : UserControl
{
private void MyControl_Load(object sender, EventArgs e)
{
//Need to get object id here "id=control01"
}
}
When i load this control in a html page and called in browser. I should get the element id in my control class either in load event or constructor
Please anyone help me on how to get the html element id in .NET control class.
Related
My web form contains a web control and a asp:HiddenField. I am trying to access that hiddenfield in my asxc.cs file. I defined a public get,set block in my aspx.cs file. In web control, when I tried to call ReportPage.TestID, it does not recognize the Reportpage class. Is it the right way to access the HiddenField in the webcontrol? If so, how should I access the ReportPage class?
public partial class ReportPage : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int TestID
{
get
{
return Convert.ToInt32(TestIDHiddenField.Value);
}
set
{
TestIDHiddenField.Value = TestID.ToString();
}
}
}
You cannot directly access page controls from a user control because of Web Site compilation model. In short, ASP.NET compiles first App_code (its classes are visible from all the site) then user controls (.ascx) and when .aspx pages which use that controls.
A workaround looks like this:
In App_code create an abstract class (MyBasePage for example) which inherits from Page class and add abstract property (TestID in your example),
Create a page which inherits from MyBasePage and implement the property,
In .ascx.cs cast this.Page as MyBasePage and use the property you want.
I have a page called
EditProject.aspx?id=xxx
I would like to invoke it wherever I want in a modal dialog. The modal dialog is simple with bootstrap.
I would just like to know if there is a control to invoke the page somehow in a div or modal dialog.
I know about IFrame, but is there a nicer more modern way with asp .net?
Thanks
You'd be better off moving EditProject.aspx to a user control, EditPorject.ascx.
Userconrols work much the same as aspx pages, supporting the same events, but you can embed them within ASPX pages like so:
<div id="edit-project-popup">
<namespace:EditProject ID="editProject" runat="server" />
</div>
You can still access the query string parameters from the usercontrol. You can also pass values to the usercontrol by adding a property:
public partial class EditProject : UserControl
{
public int ID
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
// Your Code
}
}
You can then set this property in the ASPX pages markup:
<uc:EditProject ID="editProject" runat="server" ID="xxx" />
or in the ASPX pages code behind:
editProject.ID = "xxx";
Hope this helps.
For more information on user controls see this overview on MSDN:
http://msdn.microsoft.com/en-us/library/fb3w5b53(v=vs.100).aspx
Is it possible within my code-behind file to set the asynchronous mode of the page directive.
I have no way of directly modifying the <%#Page %> attribute and and struggling to find a way to implement this in my code-behind.
I have tried in my Page_Load method to add Page.AsyncMode = true, but it returns the following error:
is inaccessible due to its protection level
Is there any way to do this? Without being able to directly modify the master page?
No, you cannot change the asynchronous mode of a page in the code-behind. An asynchronous page implements the IHttpAsyncHandler interface, and there is no way to change the interfaces implemented by your page after the .aspx file has been compiled by ASP.NET and your code is running.
Setting the Page.AsyncMode property will not change the asynchronous mode. Its purpose is to let controls on the page know whether the page is running in asynchronous mode or not, so tampering with the property may cause controls to malfunction.
My guess - you're trying to access this property from your master page. But according to documentation, this property is protected bool AsyncMode { get; set; }. Which means it is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
This property is declared in System.Web.UI.Page and is accessible in it and any class derived from it. MasterPage doesn't derive from Page. That's why you cannot access it.
You can easily access it from your page:
public partial class YourPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.AsyncMode = true;
}
}
Because you're MasterPage does not Inherit from your Page, you cannot access the AsyncMode Property.
If you absolutely must edit the value from your MasterPage maybe consider adding a method to your page called "UpdateAsyncMode" and doing the following from your masterpage Page_Load
protected void Page_Load(object sender, EventArgs e)
{
MyPageClass p = this.Page as MyPageClass ;
p.UpdateAsyncMode(true);
}
Alternatively if this is something that needs to be more robust you could create a base class for Pages like the following and have all the web pages in your site extend that base class
public abstract class MyBasePage : System.Web.UI.Page
{
public void UpdateAsyncMode (bool b)
{
this.AsyncMode = b;
}
}
I have a Page with few web user controls in it
Page does the following , it extends Icode interface
public partial class TestPage: System.Web.UI.Page, ICode
// it implements Code implementation here
public string Code
{
get
{
return "sample code";
}
}
One of the control is as below
I am able to access the Code value from this sample control
sampleControl:BaseControl
// here I am able to access the page property's
if (Page is ICode)
{
string test= ((ICode))Page).Code;
}
Now i want to access the same value from BaseControl, as baseControl is getting executed before the page
i am unable to set the values in the base control.
BaseControl: System.Web.UI.UserControl
{
// how can i access those values here.
}
if i can set the values in the base control, all controls can acess that value
any help will be appreciated
It sounds like you have your dependency chain reversed. A control is supposed to be a reusable unit, and a page should contain controls. You shouldn't be trying to access page-level properties from within your control, you should be setting the control's properties from your page.
I'm trying to make a default modal box that must be accessible from any part of the application, and need to be called whenever I want from inside any page. (must be called from code-behind).
So I came up with the idea of a Panel + modalPopupExtender placed in the MasterPage, and calling it from child pages via code-behind.
How can I do that? Or perhaps you guys have a better idea to solve this.
Since the modal is to be called from the code behind, you can achieve it like this
Add a method to your Master Page
public class MyMaster : MasterPage
{
public void ShowModal(string someParameter)
{
// Do your logic here
// Show the modal
}
}
Then add a method to your page, or page base like this...
public void ShowModal(string someParameter)
{
MyMaster masterPage = this.Master as MyMaster;
masterPage.ShowModal(someParameter);
}
I recommend using a base class for your pages so that you don't have to replicate the above method.
Add a method to your Master page. For example:
public void ShowMpSignup4free()
{
mpSignup4free.Show();
}
Then call this method from the code behind page like this:
protected void lbSignin_Click(object sender, EventArgs e)
{
MasterPages_WebMasterPage wm = (MasterPages_WebMasterPage)(this.Master);
wm.ShowMpSignup4free();
}
Here mpSignup4free is ID of ModelPopupExtender and MasterPages_WebMasterPage is name of master page (WebMasterPage is name of master page placed in folder MasterPages. That is why the complete name of master page is MasterPages_WebMasterPage).
and lbSignin is Link button on the page whose master page is WebMasterPage to whose click event will show the model popup.
For avoiding post back place the lbSignin link button in UpdatePanel...