Access previous page controls value - c#

i have a problem i want to access controls of previous pages values. I know that i can use postbackurl but i must use OnClientClick property because i must call javascript method and according to the criterias my url change. Below method is called OnClientClick property. This page name Calculator.aspx
function redirectSellPage() {
var type = getParameterByName('type');
if (type == 'test') {
window.location.href = "Change.aspx?PageType=a";
} else if (a == null || a == "") {
window.location.href = "Change.aspx?PageType=b";
} else {
window.location.href = "Change.aspx?PageType=c";
}
}
I want to access Calculator.aspx controls values in Change.aspx.
How can i do that.

In source page, which in your case is Calculator.aspx, add this:
public String Value1
{
get
{
return YourTextBoxId.Text;
}
}
On the target page, which in your case is Change.aspx, add a # PreviousPageType page directive that points to the source page.
<%# PreviousPageType VirtualPath="~/Calculator.aspx" %>
Now in target page use PreviousPage property to access values.
String val = PreviousPage.Value1;

Related

How to I get ASP.NET Page Name

Easy one here.
I have a control inside a control inside a masterpage. The page name looks something like this in HTML
ctl00$MasterPageBody$MainControl$ChildControl
Any idea how I can get the above from code behind?
Thanks!
It's the clientID property of the control.
If we're talking about a top-level control on the page, or at least one that isn't in any sort of repeating type, you can just use the ClientID property.
<asp:Label runat="server" ID="testLabel" />
<script>
$('#<%= testLabel.ClientID %>').click(function() { ... });
</script>
If we're talking about something that isn't accessible directly like that, you'll have to do the same thing, but it'll have to be wrapped in a FindControl. The example of that isn't so clean, so I'll trust you can understand from my words. But basically, if you have a label that shows code-behind-given text on each row of a GridView, you'll have to call ((Label)e.Row.FindControl("testLabel")).ClientID. Still pretty straight-forward, but a bit more complicated than the first case.
You should use a function similar to this:
public static Control FindControlRecursive(Control ctl, string id) {
if (!ctl.HasControls())
return null;
Control res = null;
foreach(Control c in ctl.Controls) {
if (c.ID == id) {
res = c;
break;
} else {
res = FindControlRecursive(c, id);
if (res != null)
break;
}
}
return res;
}
in this way:
Control ChildControl = FindControlRecursive(this.Page, "ChildControl");
string ID = ChildControl.ClientID;

Field property is required. All field web controls require the field name to be set

I am assigning the field name of Sitecore image control dynamically from code behind file like below:
.ascx
<sc:Image runat="server" ID="scImgRelatedArticle"></sc:Image>
.ascx.cs
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Field = StartDateFieldName;
scDateArticleDate.Item = currentItem;
}
}
}
Sometimes current Item is null i don't want to assign any field value. I dont want to display the item. But i am ending up with an error message "Field property is required. All field web controls require the field name to be set."
Is there a way in sitecore to do this automatically if i didn't specify the scDateArticleDate.Item property.
You should always set the Field property
scDateArticleDate.Field = StartDateFieldName // where is a string right!
Then you control the visibility of the item depending on if you have or not the item.
Also notice you post a image in your ascx and a date field in the .cs
the complete code would be
scDateArticleDate.Field = StartDateFieldName; //always set the field
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Item = currentItem;
scDateArticleDate.Visible = true;
}
else
{
scDateArticleDate.Visible = false;
}
}
}
cheers
You are not assigning the Sitecore field to the sc:image web control,it should work as:
Sitecore.Data.Fields.Date scDateArticleDate=(Sitecore.Data.Fields.Date)e.Item.FindControl("scDateArticleDate");

Load Javascript function from update panel

I have the following code -
protected string Term
{
get { return this.ViewState["Term"] != null ? (string)this.ViewState["Term"] : ""; }
set { this.ViewState["Term"] = value; }
}
Then set Term -
public void populateTerm()
{
Term = "Test";
ScriptManager.RegisterStartupScript(this, GetType(), "term","useTerm()", true);
}
Then in Javascript can use Term -
function useTerm() {
var Term = <%= Term %>;
// use Term
}
This works fine when the page is getting reloaded on the button click that was firing populateTerm(). However I have since moved the button into an updatePanel, so because the entire page is not getting reloaded on button click (only the update panel), this means that useTerm() is not called and <%= Term %> is null.
If I remove the updatePanel code it works fine. How can I get this to work with the updatePanel?
Assuming that the function useTerm is in you aspx page, you can move that script block within the UpdatePanel ContentTemplate.
An alternative would be to change the JavaScript function so term is passed in as a parameter:
function useTerm(term) {
// use term
}
and then to call it with the latest value of Term:
public void populateTerm()
{
Term = "Test";
string script = string.Format("useTerm('{0}');", Term);
ScriptManager.RegisterStartupScript(this, GetType(), "term", script, true);
}

How can i store the selected tab on a RadTabStrip that can be returned to on back button click

I currently use a hidden input field that is assigned the value of the tab that has just been selected, via javascript, like so:
function onTabSelecting(sender, args) {
var tab = args.get_tab(); //get selected tab
document.getElementById("MainContent_hdnPreviousTab").value = tab.get_text(); //assign value to hidden field
if (tab.get_pageViewID()) { //ignore
tab.set_postBack(false);
}
}
I then use this assigned value when the page is returned to, on client-side (ajax) PageLoad() event:
<script type="text/javascript" language="javascript">
var runOnce = false;
function pageLoad() {
if (!runOnce) {
var lastTab = document.getElementById("<%= hdnPreviousTab.ClientID %>");
if (lastTab.value) {
if (tabStrip) {
var tab = tabStrip.findTabByText(lastTab.value);
if (tab) {
tab.click();
}
}
}
runOnce = true;
}
}
</script>
Currently in IE this works fine (I know right?), the value that was previously set in javascript is still there and i am able to lcoate the tab that the user left the page on. However in FF, Chrome, etc. i have no such luck. The hidden field is returned to it's empty state (value = "") regardless of utilising viewstate or not.
Very curious as to whether anyone has an alternative method that would be appropriate in this situation. Please let me know if this is unclear.
Many thanks.
You could use localstorage.
localStorage.setItem('tab', value);

Get Textbox Value from Masterpage using c#

I have a search textbox situated on a masterpage like so:
<asp:TextBox ID="frmSearch" runat="server" CssClass="searchbox"></asp:TextBox>
<asp:LinkButton ID="searchGo" PostBackUrl="search.aspx" runat="server">GO</asp:LinkButton>
The code behind for the search page has the following to pick up the textbox value (snippet):
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
Page previousPage = PreviousPage;
TextBox tbSearch = (TextBox)PreviousPage.Master.FindControl("frmSearch");
searchValue.Text = tbSearch.Text;
//more code here...
}
All works great. BUT not if you enter a value whilst actually on search.aspx, which obviously isn't a previous page. How can I get round this dead end I've put myself in?
If you use the #MasterType in the page directive, then you will have a strongly-typed master page, meaning you can access exposed properties, controls, et cetera, without the need the do lookups:
<%# MasterType VirtualPath="MasterSourceType.master" %>
searchValue.Text = PreviousPage.Master.frmSearch.Text;
EDIT: In order to help stretch your imagination a little, consider an extremely simple property exposed by the master page:
public string SearchQuery
{
get { return frmSearch.Text; }
set { frmSearch.Text = value; }
}
Then, through no stroke of ingenuity whatsoever, it can be seen that we can access it like so:
searchValue.Text = PreviousPage.Master.SearchQuery;
Or,
PreviousPage.Master.SearchQuery = "a query";
Here is a solution (but I guess its old now):
{
if (PreviousPage == null)
{
TextBox tbSearch = (TextBox)Master.FindControl("txtSearch");
searchValue.Value = tbSearch.Text;
}
else
{
TextBox tbSearch = (TextBox)PreviousPage.Master.FindControl("txtSearch");
searchValue.Value = tbSearch.Text;
}
}

Categories