LinkButton Click Event in Repeater Nested Datalist in asp.net - asp.net

I have such kind of a design in a project. How can I reach button click event in repeater nested datalist in asp.net?
<asp:DataList ID="dlPosts" runat="server" Width="100%" RepeatLayout="Flow"
RepeatColumns="1" OnItemCommand="dlPosts_ItemCommand"
OnItemDataBound="dlPosts_ItemDataBound">
<ItemTemplate>
<asp:Repeater ID="repImgs" runat="server">
<ItemTemplate>
<img src="<%#Eval("Picture") %>" style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
<asp:LinkButton ID="lbYorum" runat="server" class="w3-btn w3-green w3-hover-orange" CommandName="MyUpdate" CommandArgument='<%#Bind("YazarID") %>'> Send</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:DataList>

You can just attach OnCommand event to LinkButton like regular button control.
<asp:LinkButton ID="lbYorum" runat="server"
class="w3-btn w3-green w3-hover-orange"
CommandName="MyUpdate"
CommandArgument='<%#Bind("YazarID") %>'
OnCommand="lbYorum_Command"> Send</asp:LinkButton>
Code Behind
Then retrieve YazarID from e.CommandArgument.
protected void lbYorum_Command(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
string yazarID = e.CommandArgument.ToString();
}
* Update *
if I add a new TextBox in repeater, how can I get the value of it?
You can use Parent.FindControl to find sibling controls.
...
<ItemTemplate>
<img src="<%#Eval("Picture") %>" style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
<asp:LinkButton ID="lbYorum" runat="server"
class="w3-btn w3-green w3-hover-orange"
CommandName="MyUpdate"
CommandArgument='<%#Bind("YazarID") %>'
OnCommand="lbYorum_Command"> Send</asp:LinkButton>
<asp:TextBox ID="txtYorum" runat="server" Height="50" Width="500" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
...
protected void lbYorum_Command(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
string yazarID = e.CommandArgument.ToString();
var control = sender as Control;
var txtYorum = control.Parent.FindControl("txtYorum") as TextBox;
}

Related

FindControl not working in Listview Null exception

protected void PassSessionVariable_Click(object sender, EventArgs e)
{
String strLocationID = livTour.FindControl("lblLocationID").ToString();
}
For some reason, the FindControl is getting a null exception. Any particular reasons?
Here is the code for my Listview. The find control is not finding the LocationID label.
<%--Create datasource for ListView for Tour Locations.--%>
<asp:SqlDataSource runat="server" ID="sdsListViewTour"
ConnectionString="<%$ConnectionStrings:2020LJCDT %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT LocationID, Location, Image
FROM Location
Order BY City;">
</asp:SqlDataSource>
<%--Listview--%>
<asp:ListView runat="server" ID="livTour"
DataKeyNames="Location"
DataSourceID="sdsListViewTour">
<ItemTemplate>
<div class="container p-1 bg-light">
<asp:LinkButton runat="server" ID="PassSessionVariable" OnClick="PassSessionVariable_Click">
<div class="row border-top border-bottom border-secondary" style="padding-top: 5px; padding-bottom: 5px; padding-left: 20px;">
<asp:Label runat="server" ID="lblLocationID" Text='<%# Eval("LocationID") %>' />
<div class="col text-center" style="margin: auto; color: #2699FB;">
<asp:Label runat="server" CssClass="font-weight-bold" Text='<%# Eval("Location") %>' />
</div>
<div class="col text-center">
<asp:Image runat="server" CssClass="rounded" ImageUrl='<%# "~/Image/Location/" + Eval("Image") %>' />
</div>
</div>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:ListView>
You can find location label via BindingContainer of sender as follow:
protected void PassSessionVariable_Click(object sender, EventArgs e)
{
var locationLabel = (((Control)sender).BindingContainer.FindControl("lblLocationID") as Label);
String strLocationID = locationLabel.Text;
}
In this case BindingContainer indicates the row of ListItem that you click. So that you can find location label in this row.

Need to assign selected linkbutton value in datalist to label in asp.net

I need to assign a value of linkbutton selected from datalist to a label.
How can i do this?
.aspx code
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
style="margin-left: 5px"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Eval("Description") %>' Font-Underline="False" ForeColor="Black" Font-Italic="False" ToolTip="click to open"></asp:LinkButton>
<br />
</ItemTemplate>
</asp:DataList>
<br />
<asp:Label ID="item" runat="server" Text="Label"></asp:Label>
.aspx.cs
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
item.Text = DataList1.SelectedIndex.ToString();
}
this can be done like this :
<asp:Label1 ID="Label1" runat="server"
Text='<%# Bind("your datafield") %>'
Try the following:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
var myLink = DataList1.SelectedItem.FindControl("LinkButton2") as LinkButton;
if (myLink != null)
{
item.Text = myLink.Text;
}
}

By refreshing gridview using timer, modal popup is automatically closing

Am using a modal pop for a grid view column where the grid view is set with timer.
<asp:UpdatePanel ID="GridPanel" runat="server">
<ContentTemplate>
<asp:Timer ID="autorefresh" runat="server" Interval="5000" />
<asp:GridView ID="SigmaGrid" runat="server" AutoGenerateColumns="False"
onrowcommand="SigmaGrid_RowCommand" CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" AllowPaging="True"
onpageindexchanging="SigmaGrid_PageIndexChanging">
<AlternatingRowStyle CssClass="alt" />
<PagerStyle />
<Columns>
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<ItemTemplate>
<asp:LinkButton ID="FirstName" runat="server" Text='<% # Eval("FirstName") %>' CommandName="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr" />
</asp:GridView>
<asp:Panel ID="DtlsPanel" runat="server" BackColor="White" Height="400" Width="500px" >
<table style="border: Solid 3px #626262; width: 100%; height: 100%"
cellpadding="0" cellspacing="0">
<tr style="background-color: #626262">
<td colspan="2" style="height: 10%; color: White; font-weight: bold; font-size: larger"
align="center">
Records
</td>
</tr>
<tr>
<td style="color: Black">
First Name:
</td>
<td align="center" style="color: Black">
<asp:Label ID="FNamelbl2" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnPopUp" runat="server" Style="display: none" />
<asp:ModalPopupExtender ID="DetailsPopUp1" runat="server" BackgroundCssClass="modalBackground"
TargetControlID="btnPopUp" PopupControlID="DtlsPanel" CancelControlID="btnCancel"
PopupDragHandleControlID="PopupHeader">
</asp:ModalPopupExtender>
</ContentTemplate>
</asp:UpdatePanel>
Code
protected void SigmaGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
LinkButton FirstName = (LinkButton)e.CommandSource;
GridViewRow row = (GridViewRow)FirstName.NamingContainer;
FNamelbl2.Text = FirstName.Text;
this.DetailsPopUp1.Show();
}
}
Problem
Whne i click on firstname colunn i get a pop up which displays the details of a row.. As i have used timer gridview is refreshed and pop up is automatically closing.
If you're using a ModalPopupExtender with postbacks, you should ensure in codebehind that the popup will be shown. Therefore you can use it's method Show.
For example in the page's or UserControl's PreRender:
C#
protected void Page_PreRender(object sender, System.EventArgs e)
{
if (this.Visible) {
this.DetailsPopUp1.Show();
}
}
VB.NET
Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
If Me.Visible Then
Me.DetailsPopUp1.Show()
End If
End Sub

In a asp:repeater,I use asp:checkbox,but the OnCheckedChanged doesn't fire

Here is the code:
<asp:Repeater runat="server" ID="repeater1" OnItemCommand="checkChange">
<ItemTemplate>
<asp:Table runat="server" Width="80%">
<asp:TableRow runat="server" HorizontalAlign="Center" Width="80%">
<asp:TableCell Width="80%" HorizontalAlign="Left">
<asp:Button ID="Button1" runat="server" Text="删除" Visible="<%#administrator %>" />
<asp:CheckBox runat="server" ID="checkBox" Text='<%#DataBinder.Eval(Container.DataItem,"IID")%>' AutoPostBack="true" OnCheckedChanged="checkChange" EnableViewState="false"/>
<asp:Label runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"invText") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
and the function is:
protected void checkChange(object sender, EventArgs e)
{
Response.Write("it works well!");
}
but when I click the checkbox,nothing happen...Thank you ...
Because you have provided single event handler to both the CheckBox and the Repeater.
But of course because both OnItemCommand and OnCheckedChanged events has same delegate signatures i.e void target(object sender, EventArgs e) so it won't raise any kind of error.
Code working fine like this :
<asp:Repeater runat="server" ID="repeater1">
<ItemTemplate>
<asp:Table ID="Table1" runat="server" Width="80%">
<asp:TableRow ID="TableRow1" runat="server" HorizontalAlign="Center" Width="80%">
<asp:TableCell Width="80%" HorizontalAlign="Left">
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CheckBox runat="server" ID="checkBox" Text="Click me if u dare." AutoPostBack="true"
OnCheckedChanged="checkChange" EnableViewState="false" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:Repeater>
Back end
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < 5; i++)
dt.Rows.Add(new object[] { "Name no.: " + i.ToString() });
repeater1.DataSource = dt;
repeater1.DataBind();
}
}
protected void checkChange(object sender, EventArgs e)
{
Response.Write("Clicked called at :" + DateTime.Now.ToString("dd-MM-yy hh:mm:ss"));
}

How to Recognize the CommandName of a LinkButton Inside an ItemTemplate af a RadListBox

I added an itemtemplate to my radlistbox and also added one label and two linkbutton(s) in it ...
my radlistbox is like below :
<telerik:RadListBox ID="RadlbOfImageGroup" runat="server" DataKeyField="ID" DataSortField="Title"
DataSourceID="sdsImagesGroup" DataTextField="Title" DataValueField="ID" Skin="BlackByMe"
EnableEmbeddedSkins="False" Width="260px" Height="365px" EmptyMessage="no rec!"
AutoPostBack="True" OnSelectedIndexChanged="RadlbOfImageGroup_SelectedIndexChanged"
CausesValidation="False">
<ItemTemplate>
<table style="width: 100%;">
<tr style="width: 100%;">
<td style="width: 64%;">
<asp:Label ID="lblTitleOfIG" runat="server" CssClass="lbl_ListBox_IG_Title" Text='<%# Eval("Title") %>'></asp:Label>
</td>
<td style="width: 18%; text-align: center;">
<asp:LinkButton ID="lbEditIG" runat="server" CausesValidation="False" CommandName="Edit"
CssClass="lb_ListBox_IG" OnClick="lbEditIG_Click">Edit</asp:LinkButton>
</td>
<td style="width: 18%; text-align: center;">
<asp:LinkButton ID="lbDeleteIG" runat="server" CausesValidation="False" CommandName="Delete"
CssClass="lb_ListBox_IG" OnClick="lbDeleteIG_Click">Delete</asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListBox>
My Problem is how can I check the CommandName of LinkButtons in code above when I click on them?
(We don't have access to these LinkButtons in CodeBehind)
I know we do not need CommandName for those LinkButtons / I Just want to know is it possible to read them from codebehind?
I'm not sure if this is a standard way of addressing this issue but it's what I use:
For Each item In RadlbOfImageGroup.Items
Dim editbutton As HtmlGenericControl = item.findcontrol("lbEditIG")
//Do something with editbutton.CommandName
Dim deletebutton As HtmlGenericControl = item.findcontrol("lbDeleteIG")
//Do something with deletebutton.CommandName
Next
The above example is in VB.Net but should translate fairly easily to C# if that's what you're using.
here is the code that has been introduced by telerik team :
protected void lbDeleteIG_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
if (btn.CommandName=="Delete")
{
Response.Write("Deleted");
}
}

Resources