Access gridview as property from usercontrol - c#

I have gridview bind in user control and want to access that gridview from user control in page and use export to excel.
I created property in usercontrol to access but I am not able to access it.
How can I access gridview from usercontrol in my page?

Why don't you write ExportToExcel method into the user control or just make a property which is public and return the refference to the grid:
public GridView MyGrid
{
get{ return this.GridView1;}
}

I like using interface.
You can create an interface and implement it on the web page. And using a interface object in usercontrol, you can call the interface method.
eg,
Interface
public interface IExport
{
void Export();
}
Your web page
public partial class _Default : System.Web.UI.Page, IExport
{
protected void Page_Load(object sender, EventArgs e)
{
UC11.MyExport = this;
//UC11 will be whatever name of your usercontrol
}
public void Export()
{
//your export code
}
}
And your usercontorl
public partial class UC1 : System.Web.UI.UserControl
{
public IExport MyExport { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
MyExport.Export();
}
}
Hope this helps

if you use Master page for your aspx pages you can access your page GridView in your user control in this way
Page mypage= this.Page;
GridView mypageGridview = (GridView)mypage.Master.FindControl("ContentPlaceHolder1").FindControl("YourGridView");
if you have not master page
Page mypage= this.Page;
GridView mypageGridview = (GridView)mypage.FindControl("YourGridView");

Related

passing a string to a User Control

I'm approaching to Metro App world in this days, please be gentle.
Here's the problem:
a page receives a string from another page
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Title.Text = e.Parameter.ToString();
}
and I need to pass this string to an User Control of the receiving page.
How can I pass a parameter from a page to an UserControl of another page?
Like this:
Add a property to your user control:
public string MyText { get; set; }
Give your user control a name.
<src:TopBarControl x:Name="MyTopBarControl" />
Then use your NavigatedTo method:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var _TextParam = e.Parameter.ToString();
this.MyTopBarControl.MyText = _TextParam;
}
This will feed your User Control what it needs.
You could also bind to it by setting the parameter to some public property of the page. If you attempt this approach, please remember to make the User Control's property a Dependency property and not a CLR property. I wrote an article on binding if you want a better explaination http://blog.jerrynixon.com/2012/10/xaml-binding-basics-101.html
Best of luck!
Assuming usercontrol is part of navigated page, you have to do set Property of User Control on OnNavigatedTo override.
Example:
class MyUserControl : UserControl
{
public object Parameter {get;set;}
}
Suppose this user control is part of MyPage
class MyPage : Page
{
private MyUserControl myUserControl; // It is only for illustrations, Otherwise it goes to .designer.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Title.Text = e.Parameter.ToString();
myUserControl.Parameter = e.Parameter; // This is how to set the parameter in usercontrol.
}
}

How do I Update GridView on Masterpage from button on ContentPage?

I have a MasterPage with a GridView on a UpdatePanel. On one of my content pages I have a button which adds items to a session which I want to appear in the Gridview which is on the MasterPage. I'v got the items in the Gridview but have problem with refresh or postback or something like that. Does anyone have an answere to this?
If you have refresh issue in updatepanel, then it means that the post back button is aether not inside the update panel or the panel is not updated manually.
For this case I assume you cant put the button inside the panel as it is part of content page, so i suggest you set panel's UpdateMode to conditional and have some refresh method on your masterpage. In order to see this method in the content page make some Interface with this method and let the masterpage use this interface.
then in the content page take the masterpage referense and consume the refresh method.
e.g.
The interface
public interface IMaster
{
void RefreshPanel();
}
The masterpage
(note it uses the IMaster interface that we created before)
public partial class MasterPage : System.Web.UI.MasterPage, IMaster
{
protected void Page_Load(object sender, EventArgs e)
{
//Load items from session
}
public void RefreshPanel()
{
UpdatePanel1.Update();
}
}
The content page
public partial class ContentPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Add items to session
//....
//Now refresh the updatepanel on the masterpage
IMaster masterPage = Master as IMaster;
masterPage.RefreshPanel();
}
}
You will need to look into using Events and Delegates. Basically, you will create an Event on the Usercontrol which your MasterPage will react to. There are many other websites with examples, so just google ASP.NET Events and Delegates.

Passing User Control Class to Host Page

How can I pass user control properties to the page AND make these properties available to all methods on the page (and not just to one method that is fired on a control action, e.g. onControlClick)
I have a set up of essentially 3 pages:
user control (ascx/cs)
class (cs) - that contains user control properties
host page (aspx/cs) - references the user control
The user control consists of 3 interrelated dropdowns. I'm having success passing these dropdown values through a class onto the page via an event that is fired when a user clicks on the dropdown menu. So this way the host page is continously aware of the values in the user control. However, I want the page to use the control's properties (stored in a class) on all of its methods - how do I make this user control class available to all?
Also I'm using ASP.NET and C# by the way.
Here's the Code (not sharing the full code here - just the snippets of a similar code block)
On the ASPX for Menu Host Page:
<linked:LinkMenu2 id="Menu1" runat="server" OnLinkClicked="LinkClicked" />
Host Page (cs):
protected void dropdownclicked(object sender, ddtestEventArgs e)
{
if (e.Url == "Menu2Host.aspx?product=Furniture")
{
lblClick.Text = "This link is not allowed.";
e.Cancel = true;
}
else
{
// Allow the redirect, and don't make any changes to the URL.
}
}
Host Page (aspx)
<asp:dropdowncustom ID="dddone" runat="server" OnddAppClicked="dropdownclicked" />
Control (cs)
public partial class usercontrol_tests_dropdown1 : System.Web.UI.UserControl
{
public event ddtestEventHandler ddAppClicked;
}
public void selectapp_SelectedIndexChanged(object sender, EventArgs e)
{
ddtestEventArgs args = new ddtestEventArgs(selectlink.SelectedValue);
ddAppClicked(this, args);
}
Class:
public class ddtestEventArgs : EventArgs
{
// Link
private string link;
public string Link
{
get { return link; }
set { link = value; }
}
public ddtestEventArgs(string link)
{
Link = link;
}
}
public delegate void ddtestEventHandler(object sender, ddtestEventArgs e);
Hopefully this is what you're after. The best way to do it is to expose your controls as public properties from your user control. So, in your user control, for each drop down list add a property:
public DropDownList DropDown1
{
get { return dropDownList1; }
}
public DropDownList DropDown2
{
get { return dropDownList2; }
}
You can do the same for any other properties you want to access on the host page:
public string DropDown1SelectedValue
{
get { return dropDownList1.SelectedValue; }
set { dropDownList1.SelectedValue = value; }
}
Then, from your host page you can access the properties through the user control:
string value = UserControl1.DropDown1SelectedValue;
or
string value = UserControl1.DropDownList1.SelectedValue;
Here's a couple of other answered questions that you might find useful as I think (if I've understood correctly) this is what you're doing:
Getting data from child controls loaded programmatically
How to change the value of a control in a MasterPage.

Databinding properties and get the values of them (ASP.Net)

Lets say i have this usercontrol
public class test : UserControl {
public int Count { get; set; }
public test() {
Count = 3;
}
public override DataBind() {
aRepeater.DataSource = dal.GetObjects(Count);
base.DataBind();
}
}
and i use it on my page like this
<my:test runat="server" Count="<# something %>" />
my problem now is that i am not able to get the value of Count in my usercontrol before after the call to base.DataBind(). I guess its something with databinding values to itself. The workaround sofar has therefor been
public override DataBind() {
base.DataBind(); // to bind values to self
aRepeater.DataSource = dal.GetObjects(Count);
base.DataBind(); // to bind new values that is dependent on the the first bind
}
It works, but it just doesn't seem right. My question is therefor whats the best practices is for this scenario.
Just override OnDataBinding method, not DataBind:
protected override void OnDataBinding(EventArgs e) {
base.OnDataBinding(e);
aRepeater.DataSource = dal.GetObjects(Count);
}
DataBind method essentially consists of two steps: 1) OnDataBinding(), 2) DataBind() for each child control.
It makes sense, because Count="<# something %>" occurs at DataBind(). I think you should handle all the data binding at DataBind() in this case (and not to use page binding methods). Of course this is only a matter of beautifying the code - nothing more.
Do you have to set the Count property of your user control in you .ascx file? How about not having a Count property at all or setting Count in the code behind of the page that includes the user control?
<my:test runat="server" id="test1" />
Code behind:
//User control code behind. GetCount() returns an int.
public override DataBind() {
aRepeater.DataSource = dal.GetObjects(GetCount());
base.DataBind();
}
or
//Page that has the user control.
protected void Page_Load(object sender, EventArgs e)
{
this.test1.Count = 5;
}
//User control code behind.
public override DataBind()
{
aRepeater.DataSource = dal.GetObjects(this.Count);
base.DataBind();
}

How do I reference an ASP.net MasterPage from App_Code

I'm working on a .net 3.5 site, standard website project.
I've written a custom page class in the sites App_Code folder (MyPage).
I also have a master page with a property.
public partial class MyMaster : System.Web.UI.MasterPage
{
...
private string pageID = "";
public string PageID
{
get { return pageID; }
set { pageID = value; }
}
}
I'm trying to reference this property from a property in MyPage.
public class MyPage : System.Web.UI.Page
{
...
public string PageID
{
set
{
((MyMaster)Master).PageID = value;
}
get
{
return ((MyMaster)Master).PageID;
}
}
}
I end up with "The type or namespace name 'MyMaster' could not be found. I've got it working by using FindControl() instead of a property on the MyMaster page, but IDs in the master page could change.
I've tended to do the following with Web Site projects:
In App_Code create the the following:
BaseMaster.cs
using System.Web.UI;
public class BaseMaster : MasterPage
{
public string MyString { get; set; }
}
BasePage.cs:
using System;
using System.Web.UI;
public class BasePage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (null != Master && Master is BaseMaster)
{
((BaseMaster)Master).MyString = "Some value";
}
}
}
My Master pages then inherit from BaseMaster:
using System;
public partial class Masters_MyMasterPage : BaseMaster
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(MyString))
{
// Do something.
}
}
}
And my pages inherit from BasePage:
public partial class _Default : BasePage
I found some background to this, it happens because of the way things are built and referenced.
Everything in App_Code compiles into an assembly.
The rest, aspx files, code behind, masterpages etc, compile into another assemlby that references the App_Code one.
Hence the one way street.
And also why Ben's solution works. Thanks Ben.
Tis all clear to me now.
I realise there are already accepted solutions for this, but I just stumbled across this thread.
The simplest solution is the one listed in the Microsoft website
(http://msdn.microsoft.com/en-us/library/c8y19k6h.ASPX )
Basically it says, your code will work as-is, if you include an extra directive in the child page aspx:
<%# MasterType VirtualPath="~/MyMaster.Master" %>
Then you can directly reference the property in the base MyPage by:
public string PageID
{
set
{
Master.PageID = value;
}
get
{
return Master.PageID;
}
}

Categories

Resources