Get LinkButton Embedded Label value - c#

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

Related

DropDownList inside GridView inside Repeater Control not working properly

I'm using Repeater control to generate tables on my dashboard. In the repeater, is a gridview to display item details. In the first column og the gridview is a dropdownlist to select an item and on selected index of that item, I'm fetching item details and the billing process continues for each table. Now the problem is that I'm only able to get the item details in the first repeater. Nothing happens when I select an item in another table or repeater. kindly help me with this issue.
Here is my ASPX markup:
<div class="row">
<asp:Repeater ID="rptrTables" runat="server" OnItemDataBound="rptrTables_ItemDataBound">
<ItemTemplate>
<div class="col-md-6">
<div class="card card-info">
<div class="card-header with-border">
<h3 class="card-title"><asp:Label ID="lblTableNumber" runat="server" Text='<%# Eval("TableName") %>'></asp:Label></h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />
<asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />
<asp:TemplateField HeaderText="Item Name">
<ItemTemplate>
<asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("UnitPrice")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Line Total">
<ItemTemplate>
<asp:Label ID="lblLineTotal" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Add Item" ConvertEmptyStringToNull="False">
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
</div>
<div class="card-footer">
<div class="pull-right">
<asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" />
<asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" />
<asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" />
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
And my c# code to fetch item details:
protected void GetItemDetails(object sender, EventArgs e)
{
foreach(RepeaterItem rptrItems in rptrTables.Items)
{
GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
foreach (GridViewRow row in gvItems.Rows)
{
DropDownList ddl = sender as DropDownList;
Control ctrl = row.FindControl("drpItemName") as DropDownList;
if (ctrl != null)
{
DropDownList ddl1 = (DropDownList)ctrl;
if (ddl.ClientID == ddl1.ClientID)
{
Label UnitPrice = row.FindControl("lblUnitPrice") as Label;
Label QTTY = row.FindControl("lblQuantity") as Label;
Label UPrice = row.FindControl("lblUnitPrice") as Label;
Label LINETOTAL = row.FindControl("lblLineTotal") as Label;
SqlConnection conn = new SqlConnection(constring);
conn.Open();
if (ctrl != null)
{
if ((ddl1.ID == ddl.ID) && (ddl1.SelectedIndex == ddl.SelectedIndex))
{
string str = "select * from ItemMaster where ItemName='" + ddl1.SelectedItem.ToString() + "'";
SqlCommand com = new SqlCommand(str, conn);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
UnitPrice.Text = reader["UnitPrice"].ToString();
QTTY.Text = reader["Quantity"].ToString(); ;
decimal totamt = Convert.ToDecimal(QTTY.Text) * Convert.ToDecimal(UnitPrice.Text);
}
reader.Close();
conn.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "swal", "swal('Item already selected, you can increase the quantity instead!', 'warning');", true);
}
}
}
}
}
}
}
There is a problem in your code. you are always selecting the first item from the repeater control,
GridView gvItems = (GridView)rptrTables.Items[0].FindControl("grdOrder");
Change your code to get the item from foreach loop, so your code will be
GridView gvItems = (GridView)rptrItems.FindControl("grdOrder");
try to use rptrItems instead of the first item of the repeater control each time.
Hope this will help.

how to access controls in ListView EditItemTemplate

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;
}
}
}

Access TextBox in DataList by ID from Button-Click handler in codebehind

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
}

working of linkbutton in repeater

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;
}

Checkbox list inside datalist

I have a checkbox list inside a datalist:
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
<p>
<asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server" ></asp:CheckBoxList>
</p>
</div>
</ItemTemplate>
</asp:DataList>
And I loaded two list items in this to say 'yes' and 'no'. How can i get the event in the selected checkbox?
You need to bind SelectedIndexChanged event and pass the arguments to get the current rowNumber or anything else you need using custom attributes (user defined attributes).
In html
<asp:DataList ID="dtlstfilter" runat="server" >
<ItemTemplate>
<div style="display: none;" id='<%#changes(Eval("FilterCode")) %>' class="p7ABcontent">
<p>
<asp:CheckBoxList AutoPostBack="true" Font-Size="12px" ID="chklist" runat="server" onselectedindexchanged="chklist_SelectedIndexChanged" CommandName="myCommand" CommandArguments="1" DataListRowNumber="1" ></asp:CheckBoxList>
</p>
</div>
</ItemTemplate>
</asp:DataList>
In Code behind
protected void chklist_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList chklst = (CheckBoxList)sender;
string commandName = chklst.Attributes["CommandName"].ToString();
string commandArguments = chklst.Attributes["commandArguments"].ToString();
string dataListRowNumber = chklst.Attributes["DataListRowNumber"].ToString();
}
Another way, just to think about. This solution minimizes the roundtrips between client and server.
Use a button's click event. You can iterate through the DataListItems and use the FindControl method to find the CheckBoxList. Now you can determine which elements are checked:
foreach (DataListItem item in dtlstfilter.Items)
{
if (item.ItemType == ListItemType.Item)
{
CheckBoxList checkBox = item.FindControl("chklist") as CheckBoxList;
}
}

Categories

Resources