FindControl not working in Listview Null exception - asp.net

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.

Related

How to access the id of a TextBox in the repeater code behind

I have a repeater below;
<asp:Repeater ID="RptRE" runat="server">
<ItemTemplate>
<div id="headingCollapse2" runat="server">
<a data-toggle="collapse" href="#link<%#Eval("IDmessage")%>" aria-expanded="false" aria-controls="link<%#Eval("IDmessage")%>">
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
<div class="media">
<span class="avata">
<img class="media" src="../pics/<%#Eval("Picsender")%>"></span>
</div>
<div class="media">
<h6 class="list"><%#Eval("SubjectMessage")%></h6>
<p class="ltext">
<span><%#Eval("DateSendMessage")%></span>
</p>
</div>
</a>
</div>
<div id="link<%#Eval("IDmessage")%>" role="tabpanel" aria-labelledby="headingCollapse2" class="card-collapse collapse" aria-expanded="false" style="">
<p><%#Eval("ContentMessage")%></p>
</div>
</ItemTemplate>
</asp:Repeater>
In line 5 I have a textbox
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
I can't get the ID of the TextBox code-behind. Is there any specific reason? Does anybody know how to get it?
Thanks
I've made a simple demo with 2 examples of how to get the data out of the Repeater. One example is per row and the other all rows at once. Just make sure you bind data in an IsPostBack check.
<asp:Repeater ID="RptRE" runat="server" OnItemCommand="RptRE_ItemCommand">
<ItemTemplate>
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="This Item" CommandName="Show" />
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="All Items" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
Code behind
protected void RptRE_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//get the correct textbox from the ItemIndex
var textbox = (TextBox)RptRE.Items[e.Item.ItemIndex].FindControl("getid");
//show result
Label1.Text = textbox.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in RptRE.Items)
{
var textbox = (TextBox)item.FindControl("getid");
Label1.Text += textbox.Text + "<br>";
}
}

LinkButton Click Event in Repeater Nested Datalist in 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;
}

My Button_click event is not firing in datalist

Back End(Aspx.CS) This Event is Not Firing
protected void btnCart_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if(b!=null)
{
name = b.CommandArgument;
Response.Redirect("/Cart.aspx");
}
}
Front End(Aspx Code) :
<asp:DataList ID="ItemList" runat="Server" CaptionAlign="Right" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Bind("Image")%>' alt='Image Not Visible' Style="height: 200px; width: 200px;" />
<div class="caption">
<h4>
<asp:Label ID="ufname" runat="server" Text='<%#Bind("Name") %>'>l</asp:Label></h4>
<p class="text-success">Rs:<asp:Label ID="ufprice" runat="server" Text='<%#Bind("Money") %>'></asp:Label></p>
<p>
<asp:Button ID="btnCart" CssClass="btn btn-primary" runat="Server" Text="Buy Now" CommandArgument='<%# Eval("Id") %>' OnClick="btnCart_Click"/>
<asp:Button ID="Veiw" runat="server" CssClass="btn btn-danger" Text="Details.." CommandArgument='<%# Eval("Id") %>' OnClick="btnCart_Click" />
</p>
</div>
</ItemTemplate>
Did you set EnableEventValidation to false?
Do you use any kind of UpdatePanel?

Delete image from folder using data list delete button

I display some images in a datalist by getting the images from folder.
Now, I want to delete the image in folder when I click the Delete button on my datalist .
Here is my delete button code:
protected void delete_onClick(object sender, EventArgs e)
{
string fileName = sender as string;
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
gvImages.DataBind();
}
I don't know how to get the exact image name which I want to delete, there is a delete button with each image.
Here is my datalist:
<div>
<asp:DataList ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal"
runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true">
<ItemTemplate>
<center>
<table>
<tr>
<td style="width: 90px; height: 90px">
<img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px;
width: 100px;" />
<br />
<asp:Button ID="Delete" Height="22px" OnClick="delete_onClick" Width="100px" runat="server"
Text="Delete Picture" /><br />
</td>
</tr>
</table>
</center>
</ItemTemplate>
</asp:DataList>
</div>
Nesting functions as you have done is a poor programming practice:
File.Delete(Server.MapPath(fileName));
Try is like this and then when you debug, you will be able to see what file you are are trying to delete:
string fileName = e.CommandArgument;
fileName = Server.MapPath(fileName);
File.Delete(fileName);
Also, are you getting an error? An exception? Why isn't there an exception handler around the code?
you should use commandName on button. And you should use OnDeleteCommand on DataList.
<div>
<asp:DataList OnDeleteCommand="Delete_Command" ID="gvImages" RepeatColumns="5" RepeatDirection="Horizontal" GridLines="Horizontal"
runat="server" BorderColor="#336699" BorderStyle="Solid" ShowHeader="true">
<ItemTemplate>
<center>
<table>
<tr>
<td style="width: 90px; height: 90px">
<img id="PICID" runat="server" src='<%# Container.DataItem %>' alt='' style="height: 100px;
width: 100px;" />
<br />
<asp:Button ID="Delete" Height="22px" CommandName="Delete" Width="100px" runat="server"
Text="Delete Picture" /><br />
</td>
</tr>
</table>
</center>
</ItemTemplate>
</asp:DataList>
</div>
Then,
For example Hold FileName:
<asp:Button CommandArgument ='<%# Container.DataItem %>' />
Then,
public void Delete_Command(Object sender, DataListCommandEventArgs e)
{
//you can hold filename on Button's CommandArgument
string fileName = e.CommandArgument;
File.Delete(Server.MapPath(fileName));
FileInfo fInfo;
fInfo = new FileInfo(fileName);
fInfo.Delete();
gvImages.DataBind();
}

How to get text in popup control extender?

i am using a popup control extender.but i am getting only index of selected value from radiobutton list.I want to get the text
Below is my source code
<div class="FloatRight">
<asp:TextBox ID="txtTeam" runat="server" Width="150px" autocomplete="off"></asp:TextBox>
<br />
<asp:Panel ID="panel" runat="server">
<div style="border: 1px outset white; width: 100px">
<asp:UpdatePanel runat="server" ID="up1">
<ContentTemplate>
<asp:RadioButtonList ID="rbteam" runat="server" AutoPostBack="true" OnSelectedIndexChanged="rbteam_SelectedIndexChanged">
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Panel>
<cc1:PopupControlExtender ID="txtTeam_PopupControlExtender" runat="server" Enabled="True"
and this is server side
protected void rbteam_SelectedIndexChanged(object sender, EventArgs e) {
if (!string.IsNullOrEmpty(rbteam.SelectedValue))
{
txtTeam_PopupControlExtender.Commit(rbteam.SelectedValue);
}
else
{
txtTeam_PopupControlExtender.Cancel();
}
rbteam.ClearSelection();
}
txtTeam_PopupControlExtender.Commit(rbteam.SelectedItem.Text);

Resources