I Need to Add a button Add To Cart in Data-list.
The problem is, When i Click on btn ADD Button, Datalist1_ItemCommand() is never reached, but a Postback event occurs
I need to Add a Button that reads the current item values & performs some operations on it.
ASP Code
<asp:DataList ID="DL_Products" runat="server" RepeatColumns="3" OnItemCommand="Datalist1_ItemCommand">
<ItemTemplate>
<div class="Item">
<div class="title">
<asp:Label ID="lbl_Brand" runat="server" Text='<%# Eval("Brand") %>'></asp:Label>
</div>
<div class="Info">
Price/Piece :
<asp:Label ID="lbl_Price" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
Rs
<br />
<asp:Button ID="btn" CommandName="AddtoCart" runat="server" Text="Add" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
C#/ Code Behind :
protected void Datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "AddtoCart")
{
Label l1 = (Label)e.Item.FindControl("lbl_Price");
string a = l1.Text;
Response.Write(l1.Text);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
load_User_data();
Brand_SelectedIndexChanged(null, null);
load_data();
}
}
add if(!ispostback){ bind datalist} on page load
Related
Get LinkButton Embedded Label value?
Also issue with LinkButton on postback comes back empty.
I basically need a clickable row to run a server side function is there a better way then a LinkButton?
I'm basically creating a search dropdown.
ASPX
<asp:ListView ID="listView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<ItemTemplate>
<asp:Repeater ID="SearchResults" OnItemCommand="SetValues_ItemCommand" runat="server">
<ItemTemplate>
<div class="form-row">
<asp:LinkButton ID="MemberInfo" runat="server" class="list-group-item list-group-item-action flex-column align-items-start mb-2" OnClick="MemberInfo_Click" CommandArgument='<%# Container.ItemIndex %>'>
<div class="col-lg-12 mb-2">
<h5 class="mb-1">
<asp:Label ID="PrimaryOrganization" runat="server" Text='<%# Eval("PrimaryOrganization") %>'></asp:Label>
</h5>
</div>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
CS
protected void MemberInfo_Click(object sender, EventArgs e)
{
LinkButton MemberInfoBNT = (LinkButton)sender;
bool bIsConverted = int.TryParse(MemberInfoBNT.CommandArgument.ToString(), out int index);
if (bIsConverted)
{
Repeater SearchResultsObject = MemberInfoBNT.Parent.Parent as Repeater;
ListViewDataItem listViewData = MemberInfoBNT.Parent.Parent.Parent as ListViewDataItem;
int Listviewindex = listViewData.DataItemIndex;
Label PrimaryOrganization = (Label)SearchResultsObject.Items[index].FindControl("PrimaryOrganization");
TextBox registrantEmailValue = (TextBox)listView.Items[Listviewindex].FindControl("registrantEmail");
registrantEmailValue.Text = PrimaryOrganization.Text;
}
}
Image of results
Select2 provides you a customizable dropdownlist with support for searching
I have a problem with EditItemTemplate.
I'm using Telerik RadUpload in asp listView.
In validating Event of radUpload, I want to check some Requirements so I need to findControl two control: radUpload and Label
For this I am using ItemDataBound Event of listView
but the problem is here, first ItemDataBound event execute and after that RadUpload Validating event will execute, therefor the radUpload Control and Label control remain empty
I handle this in a way by using static control, but i think that is not good idea.
Do you have a solution for that?
Another problem is with label. However I using static label and in trace shows the label have text but in output label not seen
<asp:ListView ID="LvAdminRing" runat="server" ItemPlaceholderID="ItemPlaceHolder" GroupPlaceholderID="GroupPlaceHolder"
OnItemEditing="LvAdminRing_ItemEditing" OnItemDataBound="onItemDataBound" OnItemUpdating="LvAdminRing_ItemUpdating">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="GroupPlaceHolder"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<GroupTemplate>
<div>
<asp:PlaceHolder runat="server" ID="ItemPlaceHolder"></asp:PlaceHolder>
</div>
</GroupTemplate>
<ItemTemplate>
<div class="RadRingTileDIV col-lg-12" runat="server">
<div class="row RadRingItemsDIV">
<asp:Label ID="lblPrice" CssClass="CustDispBlock CustItemFonts" runat="server" Text='<%# Eval("XPrice")%>'></asp:Label>
<asp:Label ID="lblCode" CssClass="CustDispBlock CustItemFonts" runat="server" Text='<%# Eval("XCode") %>'></asp:Label>
</div>
<div class="row RadRingItemsDIV ">
<asp:ImageButton ID="ImgRingEdit" runat="server" CommandName="Edit" ImageUrl="~/Image/Admin/CommandsPic/Edit.gif" />
<asp:ImageButton ID="ImgRingDel" runat="server" CommandName="Delete" ImageUrl="~/Image/Admin/CommandsPic/Delete.gif" />
</div>
</div>
</ItemTemplate>
<EditItemTemplate>
<div class="RadRingTileDIV col-lg-12" runat="server">
<div class="RadRingItemsDIV row">
<img src="ksdla" class="AdminImg CustDispBlock" />
<telerik:RadUpload ID="RupAdminRingPic" runat="server" AllowedFileExtensions=".png,.jpg,.jpeg,.jpe" AllowedMimeTypes="image/png,image/x-png,image/jpeg,image/pjpeg"
MaxFileInputsCount="1" MaxFileSize="52000" OverwriteExistingFiles="False" TargetFolder="~/Image/Products/Ring"
OnValidatingFile="RupAdminRingPic_ValidatingFile" ToolTip="انتخاب عکس جدید با پسوند های png،jpg،jpeg و jpe" ControlObjectsVisibility="CheckBoxes">
</telerik:RadUpload>
</div>
<div class="row RadRingItemsDIV">
<br />
<asp:Button ID="Button1" runat="server" Text="test" CommandName="Update" />
</div>
<div class="row RadRingItemsDIV">
<br />
<asp:Label ID="LblError" CssClass="CustDispBlock CustZ-Index" Visible="false" runat="server" Text=""></asp:Label>
</div>
</div>
</EditItemTemplate>
</asp:ListView>
public static Label lblError;
public static RadUpload RuEditPic;
public static Label lblError;
protected void RupAdminRingPic_ValidatingFile(object sender, Telerik.Web.UI.Upload.ValidateFileEventArgs e)
{
//Label test = (Label)LvItems.FindControl("LblError");
string[] AllowedFileExt = RuEditPic.AllowedFileExtensions;
foreach ( string AllowedExt in AllowedFileExt )
{
if (e.UploadedFile.ContentLength > RuEditPic.MaxFileSize)
{
lblError.Text = "some error";
lblError.Visible = true;
}
}
}
protected void onItemDataBound(object sender, ListViewItemEventArgs e)
{
int x = (e.Item as ListViewDataItem).DataItemIndex;
if (LvAdminRing.EditIndex == (e.Item as ListViewDataItem).DataItemIndex)
{
LvItems = LvAdminRing.Items as ListViewDataItem;
lblError = (e.Item.FindControl("LblError") as Label);
//errNoti = (e.Item.FindControl("LblError") as Label);
RuEditPic = (e.Item.FindControl("RupAdminRingPic") as RadUpload);
}
}
You should try this.
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (dataItem.DisplayIndex == ListView1.EditIndex)
{
TextBox tb = e.Item.FindControl("tbFK_MenuID") as TextBox;
}
}
}
I have a textbox which is kept inside Datalist. I need to find it via ID, so that i can insert text written to that textbox to the database.Here is my aspx page containing textbox.
<asp:Content ID="Content1" ContentPlaceHolderID="ccont" Runat="Server">
<div id="ccont">
<asp:DataList ID="mydatalist" ItemStyle-CssClass="lft_c_down" runat="server">
<ItemTemplate>
<div id="wholeC">
<div id="ctop">
<div id="lft_l">
<div id="lft_c_top">
<asp:Image runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ipath")%>' Height="250px" Width="300px" />
<br/>
</div>
<div id="lft_c_down">
<b>Product Name:</b>
<asp:Label ID="lbl2" Text='<%#DataBinder.Eval(Container.DataItem,"products") %>' runat="server" />
<br/>
<b>brand:</b>
<asp:Label ID="lbl1" Text='<%#DataBinder.Eval(Container.DataItem,"brand") %>' runat="server" />
<br/>
<b>Price:</b>
<asp:Label ID="Label1" Text='<%#DataBinder.Eval(Container.DataItem,"price") %>' runat="server" />
</div>
</div>
<div id="lft_r">
<b>Details:</b>
<asp:Label ID="Label2" Text='<%#DataBinder.Eval(Container.DataItem,"description") %>' runat="server" />
</div>
</div>
<div id="cmt">
<asp:TextBox ID="tb_cmt" runat="server" Height="35px" Width="620" placeholder="comment.." />
<asp:Button ID="Button1" runat="server" Text="Comment" backcolor="black" BorderStyle="None" Font-Names="Consolas" Font-Overline="False"
ForeColor="White" Height="34px" Width="108px" OnClick="cmt_Click" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
</div>
The Textbox with ID="tb_cmt" is the text box i want to access in my code behind as:
protected void cmt_Click(object sender, EventArgs e)
{
// how to get the TextBox?
sq.connection();
SqlCommand cmd = new SqlCommand("insert into comment(ecomment,sid) values(#myecomment,#mysid)", sq.con);
cmd.Parameters.AddWithValue("#myecomment",tb_cmt.text)//but here tb_cmt is not recognized.
}
You can use the NamingContainer property of the button that was clicked to get the DataListItem. Then you just have to use FindControl to get the reference to your TextBox:
protected void cmt_Click(object sender, EventArgs e)
{
Button btn = (Button) sender;
DataListItem item = (DataListItem) btn.NamingContainer;
TextBox txt = (TextBox) item.FindControl("tb_cmt");
//... save
}
I have problem getting the LinkButton text in nested Repeaters
<div>
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" > <%# Container.DataItem %></asp:LinkButton>
</h6>
</ItemTemplate>
</asp:Repeater>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtnTags = (LinkButton)rp_tagsTopics.FindControl("lnkBtnTags");
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
Or you can make use of the ItemCommand event by specifying the CommandName and CommandArgument parameters of the LinkButton
<asp:LinkButton ID="lnkBtnTags" runat="server" Text=' <%# Container.DataItem %>' OnClick="LinkButton1_Click" CommandName="Redirect" CommandArgument='<%# Container.DataItem %>' > <%# Container.DataItem %></asp:LinkButton>
And in the handler use the parameters:
protected void rp_tagsTopics_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if( e.CommandName == "Redirect" )
{
Response.Redirect("~/WebsofWonder.aspx?tag=" + e.CommandArgument);
}
}
What you should do is use the sender argument in the LinkButton_Click handler to get access to the instance of LinkButton that was actually clicked, and has raised the Click event:
protected void LinkButton1_Click(object sender, EventArgs e)
{
// Use sender instead of trying to find the control within the Repeater
LinkButton lnkBtnTags = (LinkButton) sender;
Response.Redirect("~/WebsofWonder.aspx?tag=" + lnkBtnTags.Text);
}
I used a linkbutton in repeater which on click shows data in a label.Now i want that clicking again the same linkbutton hide that data,means same button for showing and hiding data.
there is a database with a table which contains ques-description,date,sub. by and ans.
On page load only question appears.
Now this is the design code:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "showanswers")
{
Control control;
control = e.Item.FindControl("date");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("subby");
if(control!=null)
control.Visible = true;
control = e.Item.FindControl("ans");
if(control!=null)
control.Visible = true;
}
And this is the html code i used:
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<table>
<b>Question<%#Container.ItemIndex + 1%>:</b><%#Eval("qstdsc") %><br />
<asp:linkbutton ID="Button1" Text="Ans." commandname="showanswers" runat ="server" /><br />
</table>
<table>
<asp:Label id="date" Text='<%# Eval("qstdat")%>' Visible="false" runat="server"/>
</table>
<table>
<asp:Label id="subby" runat="server" Text='<%# Eval("qstsubby")%>' Visible="false" />
</table>
<table>
<asp:Label id="ans" runat="server" Text='<%# Eval("qstans")%>' Visible="false" />
</table>
</ItemTemplate>
</asp:Repeater>
But i don't know how to hide data again clicking the same linkbutton.
Is it possible with a single button?
What hinders you to check if the label is visible and hide/show it accordingly?
protected void lnkBtnShowDataLabel_Click(Object sender, EventArgs e)
{
lblData.Visible = !lblData.Visible;
}