Accessing master page control from WebMthod - c#

I have been struggling with accessing a control in master page from a web method (or any static method for that matter).
I have a bootstrap-styled drop down in master page that list user's favorites. Items in this drop down are populated using a repeater that reads list of favorites for current user from DB and populates the drop down.
On the main page that uses this master page there are a number of reports with an "Add to favorites" button. To do this client side, I added an onclick event to button, it calls a javascript function that calls a web method to store this info (User ID, URL) in database. This part is fine. The problem is that then I need to update the dropdown (rebind the repeater).
Here is what I have and what I have tried:
In master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="EA.SiteMaster" %>
....
<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />
....
<ul class="dropdown-menu" role="menu">
<asp:Repeater runat="server" ID="rptFavorites" OnItemDataBound="rptFavorites_ItemDataBound">
<ItemTemplate>
<li>
<asp:LinkButton runat="server" ID="lbFavLink" /><span class="glyphicon glyphicon-heart"></span>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
in Default.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EA._Default" %>
<%# MasterType virtualpath="Site.master" %>
....
function addFavorite(sURL, sFriendlyName) {
// I added this to get the routing working properly
var path = PageMethods.get_path() + '.aspx';
PageMethods.set_path(path);
PageMethods.AddUserFavorite(sURL, sFriendlyName, onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
alert(result);
}
function onFailed(result, userContext, methodName) {
alert(result);
}
....
<button type="button" class="btn btn-primary btn-xs" id="btnMPP" onclick="addFavorite('http://blahblah.com/IV/SomeReport.html','Some Report')" ><i class="fa fa-heart-o" aria-hidden="true"></i></button>
in Default.aspx.cs:
[System.Web.Services.WebMethod]
public static string AddUserFavorite(string sURL, string sFriendlyName)
{
string sMsg = string.Empty;
string sUserID = HttpContext.Current.Session["UserID"].ToString();
Favorites oFavorite = new Favorites();
oFavorite.FavoritesURL = sURL;
oFavorite.FavoritesFriendlyName = sFriendlyName;
oFavorite.UserID = sUserID;
int iRet = Favorites.AddFavrites(oFavorite);
if (-1 == iRet)
sMsg = "Failed to add to favorites list";
else
{
sMsg = "\"" + sFriendlyName + "\" (" + sURL + ") was added to your favorites list";
UpdateMaster();
}
return sMsg;
}
private static void UpdateMaster()
{
Page page = (Page)HttpContext.Current.Handler;
MasterPage mp = page.Master; <--- Always null
Repeater rptFavorites = mp.FindControl("rptFavorites") as Repeater;
if (rptFavorites != null)
{
// rebind repeater here
}
}
Any ideas on how I can achieve this is greatly appreciated. Of course, I can change the "Add to Favorites" to an ASP LinkButton and no issues, but just need to know how this approach can be done; if it can.

I don't think you need this code to access your repeater.
Page page = (Page)HttpContext.Current.Handler;
MasterPage mp = page.Master; <--- Always null
You just need to tell it to look for it in the MasterPage.
Try it this way.
(Repeater) rpt = (Repeater) Master.FindControl("rptFavorites");

Related

How to FindControl on content page from dynamic User Control

I am getting a null reference exception when trying to FindControl on a button. I have a shopping cart setup where I have a content page (.aspx) based on a master page. On the content page, there is a placeholder control in which I am dynamically adding user controls (one control per product, each with an 'add to cart' button inside it).
When the user clicks button to add item to cart, I can add it to the cart successfully, then I am counting the number of items in the cart and if more than 1, trying to show the 'checkout' button, or if none in the cart hide it.
I am using FindControl, but getting a null reference error. Why can't it find the checkout button? My current code is below:
MASTER PAGE (template.Master):
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="template.master.cs" Inherits="OUWP.template" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head runat="server"></head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder ID="m_cph_body" runat="server"></asp:ContentPlaceHolder>
</form>
</body>
</html>
CONTENT PAGE (shop.aspx)
<%# Page Title="" Language="C#" MasterPageFile="~/template.Master" AutoEventWireup="true" CodeBehind="shop.aspx.cs" Inherits="OUWP.shop" %>
<%# MasterType VirtualPath="~/template.Master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="m_cph_body" runat="server">
<asp:PlaceHolder ID="Catalogue" runat="server">
<!-- this is where the user controls are dynamically generated-->
</asp:PlaceHolder>
<asp:Panel ID="pnl_Basket" runat="server">
<div>
<asp:LinkButton ID="lbtnCheckout" runat="server">Continue to Checkout</asp:LinkButton>
</div>
</asp:Panel>
</asp:Content>
USER CONTROL PAGE (PurchasableProduct.ascx)
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="PurchasableProduct.ascx.cs" Inherits="OUWP.CustomControls.PurchasableProduct" %>
<asp:UpdatePanel ID="udpBody" runat="server">
<ContentTemplate>
<asp:Panel ID="pnlPurchasableProduct" runat="server">
<asp:LinkButton ID="lbtnAddLine" runat="server" OnClick="lbtnAddLine_Click"></asp:LinkButton>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lbtnAddLine" />
</Triggers>
</asp:UpdatePanel>
USER CONTROL CODE BEHIND (PurchasableProduct.ascx.cs)
protected void lbtnAddLine_Click(object sender, EventArgs e)
{
// try to find checkout button on parent page of control
System.Web.UI.Page page = (System.Web.UI.Page)this.Page;
LinkButton Target1 = (LinkButton)page.FindControl("lbtnCheckout");
// count the items in the cart (saved in session variable)
DataTable dt = (DataTable)Session["varDataTableCart"];
Int32 ItemCount = 0;
Int Line Quantity = 0;
foreach (DataRow dr in dt.Rows)
{
Int32 LineQuantity = Convert.ToInt32(dt.Rows[dt.Rows.IndexOf(dr)]["Quantity"].ToString());
ItemCount = ItemCount + LineQuantity;
}
// if 1 or more items in cart, try to make button visible
if (ItemCount > 0)
{
Target1.Visible = true;
}
// otherwise no items in cart, so try to hide checkout button
else
{
Target1.Visible = false;
}
}
I have also tried to get at it through the master page using the code below but that didn't work either:
MasterPage mp1 = (MasterPage)page.Master;
LinkButton Target1 = (LinkButton)mp1.Page.FindControl("lbtnCheckout");
Ok so by pure luck I tried this code and it worked and found my button. Using 'parent' of the current control.
LinkButton Target1 = (LinkButton)this.Parent.FindControl("lbtnCheckout");

ASP.net User control not recognized

I am trying to add a user control dynamically to an asp.net web page.
user control code:
<%# Control ClassName="CustomParameterView" %>
<script runat="server">
public string Value { get; set; }
public string Description { get; set; }
public string Name { get; set; }
private void Input_OnTextChanged(object sender, EventArgs e)
{
Value = Input.Text;
}
</script>
<div class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" id="DisplayName"></label>
<div class="col-sm-3">
<asp:TextBox ID="Input" runat="server" CssClass="form-control" OnTextChanged="Input_OnTextChanged" />
</div>
</div>
</div>
I have added the register line in the .aspx file:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PerCHW.Valkyrie.Server.WebApplication._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<%# Reference Control="CustomParameterView.ascx" %>
...
The problem is that this:
var control = (CustomParameterView) LoadControl("CustomParameterView.ascx");
does not compile.
I also saw people trying ti add the ASP. before the UC name but that does not work as well...
What am I doing wrong?
It looks like you are not adding the control to a PlaceHolder.
In the .aspx page add a PlaceHolder:
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
And then add the Control to the PlaceHolder in code behind:
var control = (CustomParameterView)LoadControl("~/CustomParameterView.ascx");
PlaceHolder1.Controls.Add(control);
Make sure this code is called every time the page is loaded, so don't put it inside a !IsPostBack check or the Control will be gone after PostBack.

tooltip computed links for asp button after button is clicked

<ItemTemplate>
<tr>
<asp:LinkButton ID="btnID" runat="server"
ToolTip='The calculated IDs are: ' OnCommand="showIds"
CommandArgument='<%# Convert.ToInt32(Eval("Year")) + "," +
Convert.ToInt32(Eval("Month")) %>'>
<%# Convert.ToInt32(Eval("Count")) - Convert.ToInt32(Eval("LittleCount"))%>
</asp:LinkButton>
</tr>
</ItemTemplate>
As you can notice the tooltip text is static. In code behind, I do calculate and get some integers ( IDs ) every time the above button is clicked ( protected void showIds(object sender, CommandEventArgs e) { .... }) contained as a List<ExpressionListDictionary>. ( the asp:LinkButton is contained inside an asp:ListView )
What I want to do, is to change the tooltip into a dynamic one, containing all the already obtained IDs as links. ( Something like this: http://jsfiddle.net/IrvinDominin/jLkcs/5/ - but in my case I do need firstly to click the button for calculating the IDs, and after this I would need to change the tooltip text from code as it needs to show the respective IDs, as links if it is possible)
How can I achieve this?
If you have a class (or id or something) to identify the buttons you can make an jQuery document ready function to change the tooltip with ids to a link containing the ids.
I modifyed your fiddle: http://jsfiddle.net/jLkcs/545/
$(document).ready(function () {
$(".myLinkButton").each(function() {
createlink(this);
});
});
function createlink(obj){
var ids= $(obj).attr('title');
var linkHtml="<a href='javascript:alert(" + ids + ")'>link</a>"
$(obj).attr('title',linkHtml);
}
Why not simply adjust the ToolTip in the codebehind during postback?
protected void showIds(object sender, CommandEventArgs e)
{
((LinkButton)sender).ToolTip = "blahblah";
}
You can set your sender attributes if the CommandEventArgs CommandName is equal with your defined one
public void LinkButton_Command(Object sender, CommandEventArgs e)
{
if (e.CommandName.Equals("showIds"))
{
//
}
}
Here is an working example, this will work, not counting in what user control LinkButton is used:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
public string btnNoTooltip = "No IDs are calculated";
public string btnTooltip = "The calculated IDs are:";
protected void Page_Load(object sender, EventArgs e)
{
}
public void LinkButton_Command(Object sender, CommandEventArgs e)
{
if (e.CommandName.Equals("LinkButtonOrder"))
{
LinkButton lkTrigger = (LinkButton)sender;
if (lkTrigger.ToolTip.Equals(btnNoTooltip))
{
lkTrigger.ToolTip = btnTooltip + " " + e.CommandArgument;
}
else
{
lkTrigger.ToolTip += " " + e.CommandArgument;
}
Random random = new Random();
lkTrigger.CommandArgument = random.Next(0, 100).ToString();
Label1.Text = "Triggered: " + e.CommandName + " with Argument " + e.CommandArgument;
}
}
}
Markup:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h3>LinkButton Command Event Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Order Item Here"
CommandName="LinkButtonOrder"
ToolTip='No IDs are calculated'
CommandArgument="01"
OnCommand="LinkButton_Command"
runat="server"/>
<br />
<asp:LinkButton id="LinkButton2"
Text="Or Order Item Here"
CommandName="LinkButtonOrder"
CommandArgument="02"
ToolTip='No IDs are calculated'
OnCommand="LinkButton_Command"
Runat="server"/>
<br />
<br />
<asp:Label id="Label1" runat="server"/>
<asp:PlaceHolder id="plhInjectId" runat="server" Visible="false"></asp:PlaceHolder>
</asp:Content>
You can use jquery to generate Tool Tip on Page itself.
Add a hidden field for your all the already obtained IDs (comma sepearted) to asp:ListView
Populate this hidden in ItemCreated event on server
add a class to your link button, say 'ShowHyperlinkOnHover'
Bind mouseenter event to class ShowHyperlinkOnHover document.ready function of jquery, this will dynamically generate tool tip. and then on Mouse Over tool tip will be displayed.
$(document).ready(function () {
$(document).on("mouseenter", ".ShowHyperlinkOnHover", function(this){
// 2 is index of hidden field having comma seperated Ids
var dynaToolTip;
$(this).parent("td:nth-child(2)").split(',').each(
function(oneId) dynaToolTip=dynaToolTip+ anyFomationLogic(oneId);
);
$(this).attr('title',dynaToolTip);
});
});

Cookie not displaying on second page load

What I am trying to accomplish is to create a cookie so that after you click a save button, leave the page and then come back to the page, the value of the cookie should be displayed in a label at the top of the page welcoming the user back to the page.
Here is the code I am using.
<%# page language="C#" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.OleDb" %>
<script language="C#" runat="server">
String welcomeBackName;
void Page_Load(Object sender, System.EventArgs e)
{
if (Page.IsPostBack==true)
{
HttpCookie RUcookie = new HttpCookie("RUcookie");
lblMessage.Text = txtfirstname.Text.Substring(0,1).ToLower() + txtlastname.Text.ToLower() + "#radford.edu";
RUcookie.Value = "Welcome " +txtfirstname.Text +" "+ txtlastname.Text;
RUcookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(RUcookie);
if(RUcookie != null)
{
welcomeBackName = Request.Cookies["RUcookie"].Value;
welcomeBack.Text = welcomeBackName;
}
}
}
</script>
<html>
<form id=form1 runat="server">
<br>
<ASP:Label id="welcomeBack" Text="" size="60" runat="server"/>
<br>
<br>
<br>
First Name: <asp:TextBox id="txtfirstname" size="20" runat="server"/><br>
<br>
Last Name : <asp:TextBox id="txtlastname" size="20" runat="server"/><br>
<br>
<ASP:Button id="butSave" Text="Save" Autopostback=true runat="server"/>
<br>
<br>
Email: <asp:Label id="lblMessage" size="80" forecolor=Blue runat="server"/><br>
</form>
</html>
Few things wrong.
You said you want this to happen when the user leaves the page and comes back. By definition, you won't be in a postback. That'd be a fresh load of the page. Instead of checking if it's a postback, you should simply check to see if the cookie exists.
The other thing is that IsPostBack is by definition, a boolean. You don't need to check if it's equivalent to true. It's either true or false.

How to create a runat server DIV in the behind code ? ASP.NET & C#

I'm creating multiple DIVs according to database records at my C# behind code. How could I make them runat server ?
Thanks in advance ..
Create and add ASP.NET Panels.
The code
<asp:Panel id="abc" runat="server">
is exactly the same as if you do:
<div id="abc" runat="server">
They render the same, but it's the functionality with other WebControls that the Panel is most used, and the Panel web control gives you more control under code-behind as it exposes more properties.
If you want to access a DIV on serverside, you could also add runat="server". It will be created as HtmlGenericControl.
That's not necessary, just create a HtmlGenericControl and add it to the controls collection:
HtmlGenericControl div = HtmlGenericControl("div")
div.Id = "myid";
this.Controls.Add(div);
Use a custom control that pulls the data and renders it how you would like. Kind of like this:
public class MyDivControl : System.Web.UI.Control
{
private System.Data.DataTable tblMyResults;
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
// Get your Data (or do it on Page_Load if you'll need it more than once
if (tblMyResults != null && tblMyResults.Rows.Count > 0)
{
int iIndex = 0;
foreach (System.Data.DataRow rItem in tblMyResults.Rows)
{
writer.WriteLine("<div id=\"{0}_{1}\">", this.ClientID, iIndex++);
//Whatever content you want here using your rows.
writer.WriteLine("</div>");
}
}
}
}
Then just drop the control on the page where you want it to render.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Solution.Web.Presentation.pub._default" MasterPageFile="~/ui/master/main.master" %>
<%# Register TagPrefix="custom" Namespace="MyNameSpace" Assembly="MyProjectAssembly" %>
<asp:Content runat="server" ContentPlaceHolderID="cntMain">
<custom:MyDivControl runat="server" />
</asp:Content>
You can use Repeater Control
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div id="box<%# Eval("ID")%>" runat="server"></div>
</ItemTemplate>
</asp:Repeater>
and bind data from codebehind

Categories