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.
Related
MasterPage:
public string strP;
public void Page_Load()
{
strP = #"SELECT * FROM ...";
}
Content Page:
public void Page_Load() {
if (!Page.IsPostBack) {
string strO = Master.strP; //strO = null
}
}
I know I am supposed to use the Page_Init but can someone assist me in ensuring I am able to get the set value of strP from the content page.
This is down to the page lifecycle...
The Content Page's load method will fire BEFORE the Masterpage's Load method. Slap a breakpoint in both Load event handlers and you'll see what I mean...
The Page_Load() is meant for you to do some things related to the page. You postpone some things you can't do in the constructor for the relevant objects missing (like the Request, Response, etc).
Looking at your code, setting a static SQL statement, which doesn't require any interaction with the request, etc. can be done in the constructor already.
If it is related to the request, you could put that in the Init event of the master page.
Also see my related answer on Variables from Master Page always empty.
In case others have the same questions or would like to know how to achieve what I requested, here is the detailed steps:
First assign the public variables in the MasterPage:
public string strO;
protected void Page_Init(object sender, EventArgs e)
{
strO = #""; //whatever the variable supposed to be
}
Call in Content Page:
public string strOT;
protected void Page_Init(object sender, EventArgs e)
{
strOT = Master.strO;
}
I am using devexpress controls in my page, but that is not the problem.
Here is what happens, I have created a property in the page with a get only, this property will retrieve query string value from a ViewState. I store the value in the ViewState on page load event which is enclosed in !IsPostBack. After I store
note that i have put an update panel on my master page.
I searched the net and found that ViewState values are never stored in callbacks, I don't know if that is the reason. Here is my code:
public partial class _Default : BasePage
{
private Int64 RequestId
{
get
{
return (Int64.Parse(ViewState["RequestId"].ToString()));
}
}
protected override void Page_Load(object sender, EventArgs e)
{
//Check for security
base.Page_Load(sender, e);
if (!IsPostBack)
{
GetQueryString();
gridBind();
}
}
private void GetQueryString()
{
string requestId = this.Request.QueryString["RID"];
if(!String.IsNullOrEmpty(requestId))
ViewState["RequestId"] = Int64.Parse(this.Server.UrlDecode(requestId))
else
ViewState["RequestId"] = 0;
}
}
I edited the question, the first problem i had was due to the IE7 stupidity, but nevertheless ViewState after each postback is null. I tried to use the EnableViewState but it's always null. It's the same in any page I use in my solution. We can't use ViewStates at all. There is definitely something wrong.
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");
Hi guys can anyone tell me how to do this... I have a Silverlight application with two XAML pages. On the first one I have a button. When the button is clicked I would like to redirect the user to the second XAML page.
How can I accomplish this?
This is what i have so far:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.HelloMessage.Text = "Hello Universe";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
private void HelloMessage_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("This is the load event for this page");
}
}
If you really have a need to navigate from one XAML page to another, this will set you in the right direction: Silverlight Navigation Overview. I would say, however, that in a Silverlight app, if you're simply trying to display different content on the page, you don't generally follow the standard web navigation paradigm.
Application.Current.RootVisual = new MySecondPage();
where MySecondPage is the xaml page to which you want to navigate to.
Alright, I am trying to accomplish this: When a user clicks a button that is on a ascx web user control with text boses, it first displays a DIV that is hidden, this div contains a ascx web user control. Basically I want that web user control to grab what they typed in the boxes on the first web user control, and then apply to a SQL search from what the users type in the text boxes on the first page. Is this possible or do I need to rethink my strategy on this? I am programming in c# for the SQL statements.
It is possible.
You can define properties of the control which accepts the text input, and expose the values using direct field access, variables, or session variables; you can then use FindControl from within the newly displayed control, and, if found, utilise the now exposed properties to gather the values required.
For instance, your input control code-behind might look something like this:
partial class MyControl : UserControl
{
public string MyFieldValue
{
get { return MyFieldTextBox.Text; }
}
}
And in the next control, to use it, a little like this:
partial class MyControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var myControl = Page.FindControl("MyControlInstanceName") as MyControl;
if (myControl != null)
{
var myFieldValue = myControl.MyFieldValue;
}
}
}
Is the 2nd user control embedded in the 1st or not?
If not, you can make anything available upwards between user controls by simply adding public properties to your user controls. This means they can then be accessed from the page level or the containing user control. For example, if I have UCA, UCB, UCC
UCA contains UCB and UCC is hidden.
UCB has the following property
public string UserEnteredName
{
get { return NameTextBox.Text; }
}
UCC has the following property and method
public string UserEnteredName { get; set; }
public BindResults()
{
UserEnteredLiteral.Text = UserEnteredName;
}
Then tie it together with UCA:
protected MyButton_Click(object sender, EventArgs e)
{
UCC.UserEnteredName = UCB.UserEnteredName;
... some logic herre.
UCC.BindResults();
}
You can also raise an event from UCB that can be responded to in UCA if your button or submit action exists in UCB.