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.
Related
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;
Is it possible to rebind a repeater (inside master page) from content page?
My pages are base on master and content pages and I have some links (anchors) for download files!
After click on those links page_load of content page fires, and will show download window. But we will never get page_Load of master page and in master page I have a summary for showing download counts.
How can I rebind that summary (inside master page) from content page before showing download window?
Yes, it's pretty simple. You can 'find' the control from the content page.
Here's a sample where i'm binding to a GridView control.
Master:
<asp:GridView runat="server" ID="gridViewMaster" AutoGenerateColumns="true" />
Content Page:
var gridView = (GridView) Master.FindControl("gridViewMaster");
gridView.DataSource = dt;
gridView.DataBind();
Just replace the grid view object and control id with your repeater...and bind it to whatever object you want.
Edit - Here's the code to find a server side div:
var divMaster = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divMaster");
divMaster.InnerHtml = "<h2>Hello World</h2>";
Yes. You have access to the master page from your web form—using this.Master—which you'd then have to cast as the appropriate type. From there you can access any public methods or properties you've defined.
Simple add a ReBind method there that does what you want, and you should be good to go.
EDIT
It would be something like:
(this.Master as WhateverTypeYourRealMasterPageIs).ReBind();
You can expose the master page control to the children. Make the control public with an accessor in the master page... such as:
public Repeater MasterpageRptr {get;set;}
Then on your child page, add the MasterType definition:
<%# Page Language="C#" Title="Content Page" MasterPageFile="~/MyMaster.master"%>
<%# MasterType TypeName="MyMaster" %>
(where MyMaster is the master page class file)
Then you can call this in your child code-behide using the accessor.
.
.
Please vote if helpful.
The proper way to do so is for the user control to fire an event that the Master page subscribes to. Let me know if you need some sample code
Some sample code:
In your user control, add the following event:
public event EventHandler RefreshRequested;
The user control will throw this event whenever it wants a refresh by calling the following method:
private void OnRefreshRequested()
{
//make sure the event is being listened to. no point raising an event if no one cares!
if (this.RefreshRequested != null)
{
this.RefreshRequested(this, EventArgs.Empty);
}
}
Now, the master page will subscribe to the user control's event and acts accordingly. Subscribing to the event is just like subscribing to any other event (ie: Button_Click).
Let me know if this clears things up.
I have a a generic control called dropdownlist.ascx that populates it's ListItems based on a XML document and a property that I pass to this control.
This control is used multiple times on the same aspx page. I have no problem casting this control as a DropDownList control in the Page_Load event of the aspx page, however when I want to set the SelectedValues of this control on the Page_Load event of the aspx page it doesn't work as the Items.Count value is 0.
I assume there are some Page Lifecyle issues going on here.
Control on page.aspx
<triangle:DDLResponse ID="ddlHeight" runat="server" CssClass="dropdownlist ddlregister" responseId="height" mode="dropdownlist" />
Codebehind on page.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UserProfile profile = controls_session.profile;
DropDownList _ddlHeight = (DropDownList)ddlHeight.FindControl("dropdownlist");
_ddlHeight.SelectedValue = profile.Height;
}
}
The List Items of ddlHeight render without issue.
Anyone have any idea or solution to this?
Thanks,
Try to set the SelectedValue in Page_PreRender event. It will work.
How can I access a control on page a.aspx from a webusercontrol.ascx
I do not know the technical term used to describe this, if any,
the webusercontrol.ascx page contains a button.
onclick of the button, placeholder on main page must display the "required content".
if this were on the same page no problem.
but how to access across pages?
Expose an event on your ASCX control, subscribe an event handler method on the ASPX page to the event on that page's instance of that control, implement the method to make the required changes to the parent page.
What I'm trying to achieve/plan, is whereby a page loads with a set of inputs, e.g. TextBox, Radio List etc. Taking TextBox as an example, there is a button for the user to "Add" another textbox to the page (in the same group), e.g. Member1, Member2, Member3 etc etc.
Two questions:
I could add these with Javascript, however the resultant "save" on postback would not get these inputs? If so, how?
The form needs to work without Javascript as well, so postback to dad another control is fine, however if I click the "add" button again, it will only ever add one control.
protected void btnAdd_OnClick(object sender, EventArgs e)
{
holder.Controls.Add(new TextBox { ID = "txtControl1" });
}
You can access the dynamic controls in the postback by name, using the following syntax "Page.Request.Form[""].ToString();". This uses the "name" attribute, not the "id" attribute.
I guess you can have one of these scenarios :
1- to save any new control ( textbox ) data in a session variable .. for example save name and value in which when any post back you can draw then again from this variable
2- If the only post back done from Add button you can do its function without doing post back as in this example http://msdn.microsoft.com/en-us/library/ms178210.aspx