automatically generating a list of links - asp.net

Ok so I am trying to make a list of links in a page that is generated using a foreach and loop as long as there are objects in the list. Here is the code that I use to generate the links:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["mamlist"] != null)
{
mamlist = (List<mammifere>)Session["mamlist"];
int i = 0;
foreach (mammifere l in mamlist)
{
mamol.InnerHtml += ("<li><a onClick='select("+i+");' >" + l.Nom + "</a></li>");
i++;
}
}
}
}
For some reason, the links are unclickable. I get this:
How can I make links that do not lead to another page but instead launch a method in the C# code of the page?

You can create LinkButton controls that call subroutines/methods in your ASPX code:
Sample code:
<%# Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Example</title>
<script language="C#" runat="server">
void LinkButton_Click(Object sender, EventArgs e)
{
Label1.Text="You clicked the link button";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
In your specific case, add a ContentPlaceHolder in your master page:
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
In the page where you want the links to appear you add a Content control like so:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
Then foreach link you want, do this:
foreach (mammifere l in mamlist)
{
LinkButton linkButton = new LinkButton();
linkButton.Text = l.Nom;
linkButton.OnClick= "LinkButton_Click";
linkButton.ID = l.Nom;
Content1.Controls.Add(linkButton);
}

Related

Value from previous page not transferring

I have the following code, in which I am trying to transfer some text in a previous page's textbox control (located in the master page) to the same master page textbox control. However it is not working. Please let me know if you can spot the error.
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox placeholder =
(TextBox)PreviousPage.Master.FindControl("TextBox1");
if (placeholder != null)
{
TextBox searchBox = (TextBox)Master.FindControl("TextBox1");
string search = placeholder.Text.ToString();
searchBox.Text = search;
}
}
}
this is the .aspx of the master page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="AlexBookShop.Site1" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<style type="text/css">
.auto-style1 {
margin-bottom: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">Children</asp:ListItem>
<asp:ListItem Value="1">Finance</asp:ListItem>
<asp:ListItem Value="3">Non-Fiction</asp:ListItem>
<asp:ListItem Value="4">Technical</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" CssClass="auto-style1"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Log Out" OnClick="Button2_Click" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
For transferring data from one page to other page via MasterPAge you need to do cross page posting.
you need to assign the PostBackUrl property of a button control on the first page to point to the second one. like
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Secondpage.aspx"/>
set the PreviousPage directive on the second page:
<%# PreviousPageType VirtualPath="~/firstpage.aspx" %>
Then whenever second page receives a postback, get the data you need from the first page:
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
TextBox placeholder =
(TextBox)PreviousPage.Master.FindControl("TextBox1");
if (placeholder != null)
{
TextBox searchBox = (TextBox)Master.FindControl("TextBox1");
string search = placeholder.Text.ToString();
searchBox.Text = search;
}
}
}

Error on String.Format

I get an "Object reference not set to an instance of an object." error on the line "welcome.Text = ...."
Home:
protected void OKButton_Click(object sender, EventArgs e)
{
if (UserNameTextBox.Text != String.Empty)
{
Session["UserName"] = UserNameTextBox.Text;
Label welcome = (Label)Master.FindControl("GreetingLabel");
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
}
}
<%# Page Title="" Language="C#" MasterPageFile="~/Professional.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"%>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<br /><br />
<asp:TextBox ID="UserNameTextBox" runat="server"></asp:TextBox>
<br /><br />
<asp:DropDownList ID="SitePrefDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem Text="Professional" Value="Professional"></asp:ListItem>
<asp:ListItem Text="Colourful" Value="Colourful"></asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:Button ID="OKButton" runat="server" Text="OK" onclick="OKButton_Click" />
</asp:Content>
I got the code from MCTS Exam 70-515 Web Dev book.
I looked at the Errata page, no luck. http://oreilly.com/catalog/errataunconfirmed.csp?isbn=9780735627406
Master Page:
public partial class Professional : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null)
{
GreetingLabel.Text = String.Format("Welcome, {0}!", Session["UserName"]);
}
}
}
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Professional.master.cs" Inherits="Professional" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="HeadContent" runat="server">
</asp:ContentPlaceHolder>
<link href="~/Styles/Site.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="Contoso.gif" /><asp:Label ID="Label1" runat="server" Text="Welcome to Contoso!"
Font-Size="X-Large"></asp:Label>
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
<Items>
<asp:MenuItem Text="Products" Value="Products"></asp:MenuItem>
<asp:MenuItem Text="Services" Value="Services"></asp:MenuItem>
<asp:MenuItem Text="Downloads" Value="Downloads"></asp:MenuItem>
<asp:MenuItem Text="About Us" Value="About Us"></asp:MenuItem>
</Items>
</asp:Menu>
<asp:ContentPlaceHolder id="MainContent" runat="server">
<asp:Label ID="GreetingLabel" runat="server" Text="Label"></asp:Label>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Regards
LF
Where is the GreetingLabel control located in the master? If it's in a ContentPlaceHolder the NamingContainer is that not the master.
You: "it is in a Master, under":
<asp:ContentPlaceHolder id="MainContent" runat="server">
That's it, you first have to find the ContentPlaceHolder, then use FindControl on it:
var cPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
Label welcome = (Label)cPlaceHolder.FindControl("GreetingLabel");
Now you don't get a NullReferenceException on welcome.Text anymore:
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);
Edit Since you have commented that it still doesn't work for whatever reason. I will suggest a different - better - approach. Provide a public property in your Master, for example Greeetings. Then you can get/set the Label.Text via this property. That is much more readable and maintainable. It will also work even if you change the label to a different control like TextBox or Div.
For example (in the MasterPage of type Professional):
public string Greetings
{
get { return GreetingLabel.Text; }
set { GreetingLabel.Text = value; }
}
Now you can cast the Master property in your content-page to Professional to access it:
Professional professional = (Professional) this.Master;
professional.Greetings = String.Format("Welcome, {0}!", Session["UserName"]);
Try this:
see this
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"]);// replace Session["UserName"] with Session["UserName"].ToString()
now your new line is
welcome.Text = String.Format("Welcome, {0}!", Session["UserName"].ToString());

jquery drop down for selecting multiple values

i want to develop dropdown inside a check box . this url shows me how to do it.
http://dropdown-check-list.googlecode.com/svn/trunk/demo.html
but the code isnt given how to implement it in project.
i need to bind my datatable to this dropdow.
and once user selects a value and clicks on the button in .cs file i should be able to get all these values
if any one knows how to slove this issue let me know
thank you
This is a jquery plugin that does the transformation client side. You haven't tagged your question to indicate what server side technology you are using, but from your description I infer it is ASP.NET.
Here's a sample page I've setup illustrating how to achieve the functionality you are looking for:
<%# Page Language="C#" %>
<%# Import Namespace="System.Linq" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
listBox.DataSource = new[] { "Aries", "Taurus", "Gemini" };
listBox.DataBind();
}
}
protected void BtnClick(object sender, EventArgs e)
{
var selectedItems = (from item in listBox.Items.Cast<ListItem>()
where item.Selected
select item.Text).ToArray();
result.Text = "You selected: ";
result.Text += string.Join(",", selectedItems);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/ui.core.js"></script>
<script type="text/javascript" src="scripts/ui.dropdownchecklist.js"></script>
<script type="text/javascript">
$(function() {
$('.listBox').dropdownchecklist();
});
</script>
<link rel="stylesheet" href="css/ui.dropdownchecklist.css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="listBox" runat="server" SelectionMode="Multiple" CssClass="listBox" />
<asp:LinkButton ID="btn" runat="server" Text="Click me" OnClick="BtnClick" />
<asp:Label ID="result" runat="server" />
</div>
</form>
</body>
</html>

LinkButton's server-Side event not firing when disabled=true, and inside an UpdatePanel, in IE8

It is hitting the Page_Load event, but not the LinkButton's click :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:LinkButton ID="btnRenewAll" runat="server" onclick="LinkButton1_Click" OnClientClick="javascript:return ClientMe()">LinkButton</asp:LinkButton>
<br />
<asp:Label ID="lblMe" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
<script>
function ClientMe() {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
return true;
}
</script>
</html>
CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lblMe.Text = "Checked";
UpdatePanel1.Update();
}
}
If you replace your JS with:
function ClientMe() {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(startRequest);
return true;
}
function startRequest(sender, e) {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
}
it should work. citing:
link text
Change this line:
btnRenewall.disabled = 'disabled';
Here is a list of the minimized attributes in HTML and how they should be written in XHTML :
HTML XHTML
compact compact="compact"
checked checked="checked"
declare declare="declare"
readonly readonly="readonly"
disabled disabled="disabled"
selected selected="selected"
defer defer="defer"
ismap ismap="ismap"
nohref nohref="nohref"
noshade noshade="noshade"
nowrap nowrap="nowrap"
multiple multiple="multiple"
noresize noresize="noresize"

cannot change GridView Caption

It seems to be impossible to change GridView .Caption after it has been set once.
Once I set caption and then change it within postbacks, in the code all seem to be ok, on page PreRender, GridView PreRender and wherever
I have no idea what to do - on page (and GridView also) PreRender event while debugging the .Caption is proper, but it renders with old caption anyway
Page seems to be render with set-once caption although I changed it.
I even tried to place it to updatePanel and update it, but it didn't help.
Can anybody suggest the reason?
thanks in advance.
Seems to be working here in this example, can you post your code?
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Collections.Generic.List<int> Values = new System.Collections.Generic.List<int> { 1, 2, 3, 4, 5, 6, 7 };
grdTest.DataSource = Values;
grdTest.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
grdTest.Caption = "test grid " + DateTime.Now.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdTest" Caption="test grid" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
hello
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
</div>
</form>
</body>
</html>

Resources