I have a page with a private variable: AccountData.
This handles all of the account data and I save it in the viewstate between postbacks.
I have a User Control named AccountFavorites that renders all of the account Favorite topics.
When a user decides he wants to cancel one favorite topic he clicks on an asp:LinkButton and it posts back to the server to change the value in the Database.
However, the Control doesn't know what AccountData is, only the page does.
How can I use the AccountData in a postback of the Control?
Can I make the postback go to the page instead?
<myprefix:AccountFavorites runat="server" id="TheAccountFavorites" />
In your page set the info...
protected void Page_Load(object sender, EventArgs e)
{
TheAccountFavorites.TheAccountData=MyAccountData;//assuming you'll load up MyAccountData
}
In your control, expose AccountData as a public property. This will allow you to access it from the page.
public AccountData TheAccountData {get;set;}
This is assuming AccountData was the type name and not something else, like a string. But you can change AccountData to string and it will work.
Since it is page that owns/parents the user control, it is preferable to for the page to know about the specifics of the control, not the other way around. Add AccountData property to the control and in the page's Page_Load do:
uc.AccountData = AccountData;
Related
I have following user control my .ascx Page. (Test.ascx)
<uc:Addresses runat="server" itemId='<%# StringId %>'></uc:SNETAddresses>
In the code behind of Test.ascx I have
protected string StringId = "{2A06199B-ED96-42F0-AB9A-602139E58BFB}";
In the code behind of user control Addresses.cs I have:
public string itemId { get; set; }
So basically I want to pass a string to the variable itemId. But Somehow its not getting the value of variable "StringId". This simple thing is taking my so much time. I checked this post asp.net passing string variable to a user control but I am so sorry I could not get the answer. The reply is:
You may need to call DataBind on your Page in CreateChildControls or some other method
I am new to Asp.Net and I didn't get what the user mean here.
You need to call Page.DataBind(), e.g. in your Page_Load() event handler.
Data-binding expressions (<%# ... %>) are only processed when you call the DataBind() method.
Alternatively (instead of using a data-binding expression), you can also simply assign the value to the user control's property from your page's code-behind:
// add an ID to the user control (in the markup)
<uc:Addresses runat="server" id="myControl"></uc:SNETAddresses>
Then in your Page_Load() method simply assign the value to the user control's property:
myControl.itemId = StringId;
Is it possible to acces a control thats located in a content page (withing a content place holder, a multiview control to be more exactp) from the master page?
The situation is, i have a menu with buttons thats located in the master page.
Now in my content page i have 1 content place holder.
In which a multiview with several views is located.
If i press a button in the menu (MasterPage) then it should open the proper view (with its controls) displayed in the content place holder area.
I have set the ActieveViewIndex=0 but i am getting all sorts of wierd behavour.
I have to do something with the ActiveViewIndex++ somewhere but nothing seems to work.
edit::
string a = Request.Querystring["one"]
string b = Request.QueryString["two"]
if ( a == "addOne") // where addone is a redirection to the content page from the master page button
{
mvMultieView.SetActiveView(vView1);
}
else
if ( b == "addTwo")
{
mvMultieView.SetActiveView(vView2);
}
Any suggestions?
Kind Regards.
You can easily do that using find control
View myView = (View)this.Master.FindControl("PlaceHolderFullMain").FindControl("PlaceHolderMain").FindControl("Mymultiview")
The way my team and I accomplished this task (and I don't know that it's the best method, but it was effective) was to use query strings (as you had in your previous question it looks like). We established a standard QS variable called iView that would determine the name of the view in question (not necessarily the control name itself, but some keyword that the content control would respond to). Since all of our pages/controls have a page base class they inherit from, we put a method in the base class (in our case it was at the page level, but in yours a control level might work) that was responsible for getting the requested view. In the control we would have a mechanism (switch perhaps) that would set the activeView. In some cases we just used the actual ID of the control (since it was a mystery to be obfuscated) and avoided the switch altogether.
http://www.mydomain.com/mypage.com?iView=mySecondView
partial class MyControl : System.Web.UI.WebControl
{
// blah blah control stuff
public string getRequestedView()
{
return (Request.QueryString["iView"]) ? Request.QueryString["iView"] : String.Empty;
}
}
...
protected void Page_Load(object sender, EventArgs e)
{
View myView = myMultiView.FindControl(this.getRequestedView());
if(myView != null)
this.MyView.SetActiveView(myView);
}
have you done this change in the Master page or content page?
because i also got the same problem.
I have link buttons in my master page which i need to activate view controls in content page.
the content page is not the currently loaded page.
will this method help for my solution?
On my master page , I have "Search textbox" and "Search Button".
On My content page , I have a "User Control" which has a "GridView".It shows some data about Vendors.
Also, on this User Control's Page Load, i have code written to display all vendors in GridView.
Now, when user enters Vendor Number in "Search textbox" , and hits "Search Button" , i want to handle this event inside my User Control.
How to do this ?
Please help me. Thanks in advance.
Note : i know how to handle the event in content page but not sure how to handle it inside user control placed on content page.
You just need to add logic that passes in the Search Parameters to the User Control.
On the User Control, make a public method to Bind the grid that takes in the search text
public void BindGrid{string searchText)
{
//get datasource with the searchText used as a Where, or whatever suits your current situation
//bind grid
}
Then, on the MasterPage, you should have something like
protected void btnSearch_Click(object sender, EventArgs e)
{
UserControl1.BindGrid(tbSearchText.Text);
}
You just need to make sure that your UserControl doesn't bind data on the PageLoad event if IsPostBack is true. Otherwise, you'll be binding data twice.
If you know how to handle the event in the content page, you can apply that same approach to the control. It will still be the content page that wires the control's handler to the master page's event, since the content page is the entity that knows and can access both the master page and control.
I am using a user control that I created (just a .cs file not an .ascx file), that does some magic and depending on a value generated by the control, I need it to do something on the parent page that is 'hosting' the control. It needs to call a method under certain circumstances (method is on the parent control).
the control is placed on the parent page like so:
<customtag:MyControl ID="something" runat="server" />
I'm dynamically creating buttons etc on the control itself but when a button is clicked, let's say for example that there's a text box on the control and if the value of the textbox is "bob" it needs to call a method on the page that's housing the control...how can I accomplish this?
You could do what casperOne suggested, but I wouldn't advise it. This is tightly coupling your user control to your parent page, which kind of defeats the purpose of a user control.
Personally, I'd add an event to the user control (say, ButtonClicked) that the parent page can handle. In the event handler in your parent, deal with the event however you see fit. This way you can plug the user control into a different page at a later date and not have to worry about logic in the user control that requires a specific kind of parent page.
You should be able to get the Page hosting the control through the Parent property. However, that's going to be returned to you as a Control. You have to cast it to the type of your page in order to access any methods on the page which you have defined.
I think that casperOne is on the right track, but you need to go a step further. I'm giong to make the assumption that this user control will be used on more then on page. (I normally write VB.Net, sorry if my C# is malformed)
Make a base page class (you can store it in your App_Code directory or in a project):
public class PageToInheritFrom : System.Web.UI.Page {
public void SpecialFunction() {
}
}
Now make sure that all of your pages inherit from this page in your code behind file:
public partial class _Default : PageToInheritFrom {
}
Now in your user control you know what the page type is and can call
((PageToInheritFrom)this.Page).SpecialFunction();
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...