using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadListView();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
ListViewDataItem objListItem = (ListViewDataItem)((Button)sender).NamingContainer;
string EmployeeCode = ((HtmlGenericControl)objListItem.FindControl("IdDiv")).InnerText.ToString().Trim();
string EmployeeName = ((HtmlGenericControl)objListItem.FindControl("NameDiv")).InnerText.ToString().Trim();
Response.Write("<Script>alert('Employee Name: " + EmployeeName + " Employee Code: " + EmployeeCode + "');</script>");
}
protected void LoadListView()
{
List<UserDetails> objEmployee = new List<UserDetails>()
{
new UserDetails(){EmployeeCode="E0001",EmployeeName="John the Ripper"},
new UserDetails(){EmployeeCode="E0002",EmployeeName="Jeff Carter"},
new UserDetails(){EmployeeCode="E0003",EmployeeName="John Rambo"},
new UserDetails(){EmployeeCode="E0004",EmployeeName="Rocky Balboa"},
};
DataList1.DataSource = objEmployee;
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
int i = e.Item.ItemIndex;
if (i != -1)
{
string name = ((HtmlGenericControl)e.Item.FindControl("NameDiv")).InnerText.ToString().Trim();
HyperLink lnkButton = (HyperLink)e.Item.FindControl("lnkDatalist");
lnkButton.NavigateUrl = name + ".aspx";
lnkButton.Text = name;
}
}
}
class UserDetails
{
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
}
Ashutosh Pathak
Blog: http://catchcode.blogspot.com Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
Use the debugger and see what happens. I suspect e.Item.ItemType contains more than just Item or AlternatingItem. You may have to mask ItemType to make the comparison work.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
minhpg
Member
19 Points
98 Posts
Datalist ItemDataBound?
Mar 29, 2012 08:01 AM|LINK
Hi all, i set NavigateUrl hyperlink in gridview , it run but when i use in datalist, it is not run. my code are below:
gridview:
protected void gvwArticles_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Article article = (e.Row.DataItem as Article); HyperLink hlink = (HyperLink)e.Row.FindControl("lnkTitle"); hlink.NavigateUrl = "~/xxx/" + article.Title.ToString().Trim(); }Datalist:
protected void dlstServiceList_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem) { Article article = (e.Item.DataItem as Article); HyperLink hlink = (HyperLink)e.Item.FindControl("lnkTitle"); hlink.NavigateUrl = "~/xxxx/" + article.Title.ToString().Trim(); } }How can i fix it when use DataList?
thanks,
Ashutosh Pat...
Contributor
5737 Points
1105 Posts
Re: Datalist ItemDataBound?
Mar 29, 2012 08:16 AM|LINK
please have a look at code below, its working fine on my system using datalist:
ASPX Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %> <!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 id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" onitemdatabound="DataList1_ItemDataBound"> <ItemTemplate> <br /> Employee ID <div id="IdDiv" runat="server"> <%#Eval("EmployeeCode") %> </div> Employee Name<br /> <div id="NameDiv" runat="server"> <%#Eval("EmployeeName") %> </div> <asp:HyperLink ID="lnkDatalist" runat="server"></asp:HyperLink> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> </ItemTemplate> </asp:DataList> <br /> </div> <br /> <asp:Button ID="btnRetString" runat="server" OnClientClick="return CallMe();" Text="Add Comment" OnClick="btnSubmit_Click" /> <br /> <div id="AddComment"> </div> </form> </body> </html>CodeBehind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; public partial class DataList : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { LoadListView(); } } protected void btnSubmit_Click(object sender, EventArgs e) { ListViewDataItem objListItem = (ListViewDataItem)((Button)sender).NamingContainer; string EmployeeCode = ((HtmlGenericControl)objListItem.FindControl("IdDiv")).InnerText.ToString().Trim(); string EmployeeName = ((HtmlGenericControl)objListItem.FindControl("NameDiv")).InnerText.ToString().Trim(); Response.Write("<Script>alert('Employee Name: " + EmployeeName + " Employee Code: " + EmployeeCode + "');</script>"); } protected void LoadListView() { List<UserDetails> objEmployee = new List<UserDetails>() { new UserDetails(){EmployeeCode="E0001",EmployeeName="John the Ripper"}, new UserDetails(){EmployeeCode="E0002",EmployeeName="Jeff Carter"}, new UserDetails(){EmployeeCode="E0003",EmployeeName="John Rambo"}, new UserDetails(){EmployeeCode="E0004",EmployeeName="Rocky Balboa"}, }; DataList1.DataSource = objEmployee; DataList1.DataBind(); } protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { int i = e.Item.ItemIndex; if (i != -1) { string name = ((HtmlGenericControl)e.Item.FindControl("NameDiv")).InnerText.ToString().Trim(); HyperLink lnkButton = (HyperLink)e.Item.FindControl("lnkDatalist"); lnkButton.NavigateUrl = name + ".aspx"; lnkButton.Text = name; } } } class UserDetails { public string EmployeeCode { get; set; } public string EmployeeName { get; set; } }Blog: http://catchcode.blogspot.com
Please mark it as answer if it helps, as clicking on the button can save time of others :)
MCP,MCAD,MCSD,MCTS
superguppie
All-Star
48225 Points
8679 Posts
Re: Datalist ItemDataBound?
Mar 29, 2012 11:28 AM|LINK
Use the debugger and see what happens. I suspect e.Item.ItemType contains more than just Item or AlternatingItem. You may have to mask ItemType to make the comparison work.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
minhpg
Member
19 Points
98 Posts
Re: Datalist ItemDataBound?
Apr 04, 2012 03:12 PM|LINK
ok, it work fine now.
thank you all for your reply!