I have a DataList control in asp.net webform, inside this DataLIst I have 2 labels that are bound to database, and one button.
One of the label represent Id and other one Stock.
I want to capture the button click event of specific product, and then add that product to the user cart.
Here is my datalist:
<asp:DataList ID="dListProduct" runat="server" RepeatColumns="4" OnItemCommand="dListProduct_ItemCommand">
<ItemTemplate>
<div>
<table class="table-responsive" border="1">
<tr>
<td>
<asp:Label runat="server" Text="Available Stock: " ></asp:Label><asp:Label ID="Label1" runat="server" Text='<%# Eval("Stock")%>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text="Id" ></asp:Label><asp:Label ID="lblPId" runat="server" Text='<%# Eval("Product_Id")%>' Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button id="btnAddtoCart" runat="server" Text="Add to Cart"/>
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
Here is the code I am using in OnItemCommand Event of DataList:
protected void dListProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
Label lbl = (Label)e.Item.FindControl("lblPId");
Response.Write(lbl.Text);
}
but this code never get executed.
Can anyone help me out?
Copy and paste the code below exactly the way I have it.
Code behind:
public partial class DataListExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
MyStock stock1 = new MyStock { Product_Id = 1, Stock = "Stock 1" };
MyStock stock2 = new MyStock { Product_Id = 2, Stock = "Stock 2" };
dListProduct.DataSource = new List<MyStock> { stock1, stock2 };
dListProduct.DataBind();
}
protected void dListProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
Label lbl = (Label)e.Item.FindControl("lblPId");
Response.Write(lbl.Text);
}
}
public class MyStock
{
public int Product_Id { get; set; }
public string Stock { get; set; }
}
.ASPX:
<asp:DataList ID="dListProduct" runat="server" RepeatColumns="4" OnItemCommand="dListProduct_ItemCommand">
<ItemTemplate>
<div>
<table class="table-responsive" border="1">
<tr>
<td>
<asp:Label runat="server" Text="Available Stock: "></asp:Label><asp:Label ID="Label1" runat="server" Text='<%# Eval("Stock")%>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" Text="Id"></asp:Label><asp:Label ID="lblPId" runat="server" Text='<%# Eval("Product_Id")%>' Visible="false"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddtoCart" runat="server" Text="Add to Cart" />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:DataList>
Related
I have the following mark up for a User Control (.ascx)
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Select Logical Symbol to Search:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlComponentType" runat="server"
onselectedindexchanged="ddlComponentType_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
</td>
<td>
<asp:CheckBox ID="chkAdvSearchAllLibs" runat="server" ToolTip="Check this box to search all available libraries" Text="Search All Libraries"/>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Search by Logical Symbol Properties:"></asp:Label>
</td>
<td>
</td>
</tr>
On page Load
protected void Page_Load(object sender, EventArgs e)
{
SearchResults(ref attributeSearch, compTypeID);
}
where SearchResults is
private void SearchResults(ref string attributeSearch, int compTypeID)
{
DataTable dtResults = this.AdvancedSearchControl.GetSearchResults(ref attributeSearch, compTypeID);
}
And in my UserControl.ascx.cs
public DataTable GetSearchResults(ref string _attrVals, int compTypeID)
{
//Other Logic Goes Here
IEnumerable<Model.ComponentInfo.ComponentType> compTypeResult = from compTypes in BLLibrary.GetComponentTypeBasedOnLib(this.CurrentLibraryId, this.CurrentLibrary, this.chkAdvSearchAllLibs.Checked) select compTypes.Value;
}
this.chkAdvSearchAllLibs.Checked is always false no matter if the check-box is checked on page and posted back or not.
Server side:
Add AutoPostBack="True" to the CheckBox. It's not posting back.
Client side:
<asp:CheckBox runat="server" ID="cb" onclick="checkboxchanged(this);" />
function checkboxchanged( sender ) {
if ( sender.checked ) {
// clicked and checked
} else {
// clicked and unchecked
}
}
I have a repeater control. In OnItemCommand method of repeater, after doing some operations I am trying to refresh the repeater list so that it could show me the updated list. But somehow repeater list is not updated in method. However when I reload the page then repeater list gets updated with most recent items.
ASPX:
<asp:Label ID="Message1" runat="server" ForeColor="Blue" Text=""></asp:Label>
<div id="ListingAgentsData" class="panel panel-default" style="width: 920px;">
<asp:Repeater ID="rptagentList" runat="server" OnItemCommand="rptagentList_OnItemCommand">
<HeaderTemplate>
<table id="results1" cellpadding="4" cellspacing="1" width="100%">
<tr>
<td>
<strong>AgentID</strong>
</td>
<td>
<strong>Email</strong>
</td>
<td>
<strong>FullName</strong>
</td>
<td>
<strong>Driver License/Passport</strong>
</td>
<td>
<strong>Action</strong>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblDayofweek" runat="server" Text='<%#Eval("AgentID")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblTime" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
</td>
<td>
<asp:Label ID="lblCharges" runat="server" Text='<%#Eval("FullName")%>'></asp:Label>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("DriverLicense")%>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="ibtn" runat="server" Text="Approve" CommandName="Approve" CommandArgument='<%#Eval("AgentID")%>' />
<asp:LinkButton ID="LinkButton1" runat="server" Text="Reject" CommandName="Reject" CommandArgument='<%#Eval("AgentID")%>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.CS:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] == null)
Response.Redirect("../Login/Default.aspx");
else
{
Init();
}
}
protected void rptagentList_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
try
{
if (e.CommandName == "Approve")
{
int agentId = Convert.ToInt32(e.CommandArgument);
GM gm = new GM();
if (gm.ApproveListingAgent(agentId))
{
Message1.Text = "Listing Agent with ID:"+agentId+" is approved!";
Init();
}
}
else if (e.CommandName == "Reject")
{
}
}
catch (Exception ex)
{
throw ex;
}
}
private void Init()
{
ListingAgent Agent = new ListingAgent();
DataTable dt = Agent.getPendingListingAgents();
if (dt.Rows.Count > 0)
{
rptagentList.DataSource = dt;
rptagentList.DataBind();
}
}
What am I missing?
Please help!
<asp:DropDownList ID="ddlGroupSearch" CssClass="cssTextbox" runat="server">
<asp:ListItem Value="1" Text="Referring Physician Wise"></asp:ListItem>
<asp:ListItem Value="2" Text="Specialty Wise"></asp:ListItem>
</asp:DropDownList>
<asp:Repeater runat="server" ID="repeaterParent" OnItemDataBound="repeaterParent_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblREF_PHY_ID" runat="server" Text='<%# this.RenderGroup(Eval("REF_PHY_ID") as string)%>'></asp:Label>
</td>
</tr>
<tr>
<td class="cssTableListTd">
<asp:Label ID="lblPatNameListData" runat="server" Text='<%#Eval("Patient_Name")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatIDListData" runat="server" Text='<%#Eval("Patient_ID")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatSexListData" runat="server" Text='<%#Eval("Sex")%>'></asp:Label>
</td>
<td class="cssTableListTd">
<asp:Label ID="lblPatBirthDateListData" runat="server" Text='<%#Eval("Patients_Birth_Date")%>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
c#
protected void Page_Load(object sender, EventArgs e)
{
//get data bind to repeater
repeaterParent.DataSource = DataTable;
repeaterParent.DataBind();.
}
string lastValue = "";
protected string RenderGroup(string currentValue)
{
if(currentValue == this.lastValue)
return "";//Group has changed
this.lastValue = currentValue;
return currentValue;
}
I have to show report group wise. I have a DropDownList in my page. Now according to the DropDownList value I have to call RenderGroup function. How should I call in repeater?
I have taken a reference of this link
ref link
Did you tried by applying group by functionality in to DataTable that you are binding to repeater?
I am pointing to something like below.
Ref: Link
Thanks!
I want button in the datalist OnClick gets text from both textboxes on the same row of the button.. how can i refer to that using C# keeping in mind that I want to use my own stored procedures and functions "OnClicking" buttons without using SqlDataSource Control
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table class="auto-style2">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UDI") %>'></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("describtion") %>'></asp:TextBox>
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="Modify" CommandArgument='<%# Eval("UDI") %>' OnCommand="Button2_Command" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
and here is the code behind
AdminControlEntities db = new AdminControlEntities();
var x=db.sp_GetAllProducts(); //Stored procedure that returns a selection of data
DataList1.DataSource = x.ToList();
DataList1.DataBind();
till here i get my data viewed correctly, I need to Update using my own stored procedure in this example from TextBox1 and TextBox2 to the Label1 ID
ADD OnItemCommand to your mark up
<asp:DataList ID="DataList1" runat="server" OnItemCommand="Modify_ItemCommand" >
Then on Code Behind:
protected void Modify_ItemCommand(object source, DataListCommandEventArgs e)
{
/* select the row index */
int index = Convert.ToInt32(e.Item.ItemIndex);
/*To get and Textbox of selected row*/
TextBox txtbx = (TextBox)e.Item.FindControl("TextBox1");
/* Assigning Value to your textbox */
txtbx.Text = "What ever you want here";
}
You can declare the button using the property CommandName and then use the ItemCommand event controller.
Source: http://msdn.microsoft.com/es-es/library/es4e4e0e(v=vs.100).aspx
Here, a working example (Note that I'm using an UpdatePanel. Due the content of the page is going to be change, if you do not use it you will get a server error. More info here):
Test.aspx
<asp:UpdatePanel ID="upDataList1" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lUID" runat="server" Text='<%# Eval("UID") %>' />
</td>
<td>
<asp:TextBox ID="tbName" runat="server" Text='<%# Eval("name") %>' />
</td>
<td>
<asp:TextBox ID="tbDescription" runat="server" Text='<%# Eval("description") %>' />
</td>
<td>
<asp:Button ID="bModify" runat="server" Text="Modify" CommandName="Modify" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
Test.aspx.cs
public partial class Test : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
var x = db.sp_GetAllProducts();
DataList1.DataSource = x;
DataList1.DataBind();
}
}
protected void DataList1_ItemCommand(Object sender, DataListCommandEventArgs e) {
String a = ((TextBox) e.Item.FindControl("tbName")).Text;
String b = ((TextBox) e.Item.FindControl("tbDescription")).Text;
((Label) e.Item.FindControl("lUID")).Text = a + " " + b;
}
}
public class db {
public String UID { get; set; }
public String name { get; set; }
public String description { get; set; }
public db(String UID, String name, String description) {
this.UID = UID;
this.name = name;
this.description = description;
}
public static List<db> sp_GetAllProducts() {
List<db> list = new List<db>();
list.Add(new db("1", "1a", "1b"));
list.Add(new db("2", "2a", "2b"));
list.Add(new db("3", "3a", "3b"));
list.Add(new db("4", "4a", "4b"));
list.Add(new db("5", "5a", "5b"));
list.Add(new db("6", "6a", "6b"));
return list;
}
}
I have next controls in my ASP.NET page:
<asp:Content ID="headerPanelContent" ContentPlaceHolderID="mainContent" runat="server">
<asp:Repeater ID="rptHomePage" runat="server" DataSourceID="dsHomePage" OnItemCreated="rptHomePage_ItemCreated">
<ItemTemplate>
<div class="content-forum-section">
<table class="forum-table-view">
<tr class="content-forum-name f-background">
<td colspan="3">
<h2 class="t-color-white"><%# Eval("forumName") %></h2>
<asp:HiddenField ID="hdnForumID" Value='<%# Eval("forumID") %>' runat="server" />
</td>
</tr>
<tr class="f-background t-color-white">
<td style="width: 85%;">Section
</td>
<td style="width: 9%;">Themes
</td>
<td style="width: 9%;">Messages
</td>
</tr>
<asp:Repeater ID="rptSections" runat="server" DataSourceID="dsSectionsInForum" OnItemCreated="rptSections_ItemCreated">
<ItemTemplate>
<tr class="lightgrey-background">
<td>
<div class="forum-section-container">
<a href='<%# "./Section.aspx?id=" + Eval("SectionId").ToString() %>'><%#Eval("Name") %></a>
<br />
<asp:Repeater ID="rptSubsections" runat="server" DataSourceID="dsSubsectionsInSection">
<ItemTemplate>
<div class="subsection-link">
<a href='<%# "./Subsection.aspx?id=" + Eval("SubsectionId").ToString() %>'><%# Eval("Name") %></a>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsSubsectionsInSection" runat="server">
</asp:LinqDataSource>
</div>
</td>
<td>0
</td>
<td>0
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsSectionsInForum" runat="server"></asp:LinqDataSource>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:LinqDataSource ID="dsHomePage" runat="server"
ContextTypeName="PWO_Projekt.ForumDBDataContext"
Select="new(Id as forumID, Name as forumName)"
TableName="Forums">
</asp:LinqDataSource>
</asp:Content>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rptHomePage_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var forumDetails = (dynamic)e.Item.DataItem;
int forumID = forumDetails.forumID;
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
lds.TableName = "Sections";
lds.Where = "ForumId == #id";
lds.WhereParameters.Add("id", DbType.Int32, forumID.ToString());
lds.DataBind();
}
protected void rptSections_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var sectionDetails = (dynamic)e.Item.DataItem;
int sectionID = sectionDetails.SectionId;
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSubsectionsInSection");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
lds.TableName = "Subsections";
lds.Where = "SectionId == #id";
lds.WhereParameters.Add("id", DbType.Int32, sectionID.ToString());
lds.DataBind();
}
Also I have on this page my user control as login form:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.ascx.cs" Inherits="PWO_Projekt.Controls.LoginForm" %>
<form>
<table>
<tr>
<td>Login
</td>
<td>
<asp:TextBox ID="txtLogin" runat="server" CssClass="smallfont" Columns="15"></asp:TextBox>
</td>
<td>
<asp:CheckBox ID="chRemeber" runat="server" />
Remember me
</td>
</tr>
<tr>
<td>Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="smallfont" Columns="15"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnLogin" runat="server" Text="Log in" OnClick="btnLogin_Click" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="lblError" runat="server" Text="" ForeColor="Red"></asp:Label>
</td>
</tr>
</table>
And code behind:
protected void btnLogin_Click(object sender, EventArgs e)
{
string login = txtLogin.Text.Trim();
string password = CommonFunctions.getMd5Hash(txtPassword.Text.Trim());
using (ForumDBDataContext db = new ForumDBDataContext())
{
db.Connection.ConnectionString = CommonFunctions.getConnectionString();
var user =
from u in db.Users
where (u.Login == login) && (u.Password == password)
select u;
if (user.Count() == 1)
{
Session["UserLogin"] = login;
Response.Redirect("./");
}
}
}
But after pressing Login button I have next error on my page:
Cannot perform runtime binding on a null reference Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code.
{
var forumDetails = (dynamic)e.Item.DataItem;
int forumID = forumDetails.forumID; //error is here
LinqDataSource lds = (LinqDataSource)e.Item.FindControl("dsSectionsInForum");
lds.ContextTypeName = "PWO_Projekt.ForumDBDataContext";
And I don't understand what the problem here. This exception rises only after pressing
Login button (As I understand after PostBack)
The DataItem property is set only when you are calling DataBind() on the repeater. After a postback, the DataItem is no more present.
You should replace Item_Created, which is fired on all requests by Item_Databound which is fired when applying the databinding.