Pulling data from repeater without page postback - c#

When I click the button in the repeater, I want to get the data about the relevant row. But without the page postback. I want to use UpdatePanel but I couldn't figure out how to do it. Can you help me?
Here are sample codes:
<asp:Repeater ID="rptCariler" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("CariKodu") %></td>
<td><%#Eval("CariUnvani") %></td>
<td><%#Eval("CariGrubu") %></td>
<td><%#Eval("AdSoyad") %></td>
<td><%#Eval("Borc") %></td>
<td><%#Eval("Alacak") %></td>
<td><%#Eval("Bakiye") %></td>
<td class="xsml">
<asp:LinkButton ID="lbGetValue" runat="server" OnClick="GetValue"><span><i class="fas fa-trash-alt"></i></span></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

Well, you don't note if you need the info client side, or server side?
I mean, if you drop the whole repeater into a update panel then you don't get a full page post back.
You would (should) then tag the controls as server side. Assuming the whole repeater inside of a update panel, say like this:
<asp:Repeater ID="r2" runat="server">
<ItemTemplate>
<table id="mytable" runat="server">
<tr>
<td><%#Eval("HotelName") %></td>
<td><%#Eval("City") %></td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:Repeater>
So now the code behind can work like this:
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton mybutton = (LinkButton)sender;
RepeaterItem repItem = (RepeaterItem)mybutton.Parent.Parent.Parent.Parent ;
HtmlTableRow rRow = (HtmlTableRow)mybutton.Parent.Parent;
Debug.WriteLine("Repeater index click = " + repItem.ItemIndex);
Debug.WriteLine(rRow.Cells[0].InnerText);
Debug.WriteLine(rRow.Cells[1].InnerText);
}
This much comes down to if you want code behind here.
I mean, I would use a gridview, or listview if this is a table of data - you have better options for a "grid" of data in place of a repeater.

Related

Dynamically change repeater's data item

I have an <asp:Repeater> control that loads its data from a database.
To specify which column will be used where in the .aspx file, I use inside the Repeater's ItemTempate
<%# DataBinder.Eval(Container.DataItem, "column1")%>
Now when I need to make changes eg. I want another column instead of column1, I have to open Visual Studio, make the changes, publish the project again, and upload it to the server.
How can this control be more adjustable without having to change the source code each time?
Is there any other control that will do what I need better?
ASPX markup:
<table class="uk-table">
<thead>
<tr>
<th>User Name</th>
<th>Date 1</th>
<th>Date 2</th>
<th>State</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="Repeater2">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr class="uk-table-middle">
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<td><%# DataBinder.Eval(Container.DataItem,"column3")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"column4")%></span></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
</tbody>
</table>
You need to define a condition like the following and save your condition either in the database or an XML file or even in the web.config (on yours) and do the following. Don't forget to retrieve your condition out of the repeater if you wanna apply to all rows:
<%
var myCondition = true; // whatever it could be...
%>
<asp:Repeater runat="server" ID="Repeater2">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr class="uk-table-middle">
<% if (mycondition){ %>
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<% }else{ %>
<td><%# DataBinder.Eval(Container.DataItem,"column2")%></td>
<td><%# DataBinder.Eval(Container.DataItem,"column1")%></span></td>
<% } %>
<td><%# DataBinder.Eval(Container.DataItem,"column3")%> </td>
<td><%# DataBinder.Eval(Container.DataItem,"column4")%></span></td>
</tr>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater

Use of Repeater asp.net with datatable Jquery

I'm new to ASP.NET techonology and I'm trying to use datatable Jquery with a repeater.
But it doesn't work with the value inside the table. It work with just the head (see below). These data come from a database.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="Model2">
<HeaderTemplate>
<table id="table_id">
<thead>
<tr>
<th>Nom</th>
<th>ip</th>
<th>askit</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr>
<td><%# Eval("Li_Id")%></td>
<td><%# Eval("Li_ip")%> </td>
<td><%# Eval("Li_nom_askit")%> </td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And here it's what I get
Screen of what I have
So for example the line with id '594' is not in the datable that I have create.
I try to use a ListView but it doesn't work.
Does anybody know why ?
Thanks
You're creating a separate tbody around each row. Move the opening tbody tag to the end of the header template and the closing tbody tag to the beginning of the footer template.
(I'm assuming you're referring to the datatables.net jQuery plugin.)

linkbutton within repeater within updatepanel to spawn new window using RegisterClientScriptBlock

I've a weird situation.
I have a Repeater within an UpdatePanel. The repeater has a linkbutton which spawns a new window - or rather, which should spawn a new window. When the repeater was outside of the UpdatePanel it worked fine; now it doesn't.
I've tried making the repeater an AsyncPostBack trigger, with no luck:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rptEmployees" />
<asp:AsyncPostBackTrigger ControlID="imgClear" />
</Triggers>
I've tried making the Linkbutton an AsyncPBtrigger on ItemDataBound, with no luck:
LinkButton lb = (LinkButton)e.Item.FindControl("lbtnVcard");
sm1.RegisterAsyncPostBackControl(lb);
When I make the Linkbutton a PostBackTrigger, the popup appears once. Any subsequent time I hit one of the linkbuttons (until I do a hard refresh) no popup appears, although the ItemCommand of the Repeater is always hit:
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>var" +
#" win=window.open('vCard.aspx',null,'width=50,height=50," +
#"top=100,left=100','true');</script>", false);
Finally, here's my ItemTemplate:
<ItemTemplate>
<!--For IE9 there can be no whitespace -->
<tr>
<td width="140"><strong><%# Eval("DisplayName") %></strong><asp:HiddenField runat="server" ID="HFname" Value='<%# Eval("SamAccountName") %>'/></td>
<td width="100"><%# Eval("Office") %></td>
<td width="120"><%# Eval("WorkPhone") %></td>
<td width="120"><%# Eval("MobilePhone") %></td>
<td width="300" style="text-align: left"><%# Eval("EmailAddress") %></td>
<td><div class="button"><asp:LinkButton CommandName="vCard" runat="server" ID="lbtnVcard" Text="vCard" CssClass="button"/></div></td>
</tr>
</ItemTemplate>
Any clues/thoughts??
Thanks.
UPDATE: Still an issue, as I need to hit the server to load some data into memory.
Define the js on the page for window opening
<script type="text/javascript">
function OpenWin(){
window.open('vCard.aspx',null,'width=50,height=50,top=100,left=100','true');
}
</script>
Modify the itemtemplate to point to it.
<ItemTemplate>
<!--For IE9 there can be no whitespace -->
<tr>
<td width="140"><strong><%# Eval("DisplayName") %></strong><asp:HiddenField runat="server" ID="HFname" Value='<%# Eval("SamAccountName") %>'/></td>
<td width="100"><%# Eval("Office") %></td>
<td width="120"><%# Eval("WorkPhone") %></td>
<td width="120"><%# Eval("MobilePhone") %></td>
<td width="300" style="text-align: left"><%# Eval("EmailAddress") %></td>
<td><div class="button"><asp:LinkButton CommandName="vCard" runat="server" ID="lbtnVcard" Text="vCard" CssClass="button" OnClientClick="OpenWin()"/></div></td>
</tr>
</ItemTemplate>
You can now remove the js you wired up to the button just to see if the behaviour changes.

Using asp.net in html pages

If I want to combine asp.net code with html (mark up) file, I need to open <% %> and execute teh code.
What If I want to iterate over a database with select query while feeding information and creating rows. For example:
<table>
<%
foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
{
%>
<tr>
<td>
<asp:Label runat="Server" Text="<% dr[FirstName].toString(); %>"/>
</td>
<td>
<asp:Label runat="Server" Text="<%dr[LastName].toString();%>"/>
</td>
</tr>
<%
}
%>
</table>
Is the syntax correct..and is that practice good (it is always used in php) ? or should I bind the data to the label somehow?(no idea how. but somehow)?
If you're trying to take a set of data and display it in a table then try using control like the GridView or the Repeater.
First and most important thing - do not mix business logic and data access functionality with the data representation markup!
Supposing you are using WebForms, you can use Repeater control which is bound in the code behind of a page/control (aspx.cs/ascx.cs) so View stay decoupled and just bound to specific properties of data source:
ASPX:
<asp:Repeater ID="employees" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label runat="Server"
Text="<%# DataBinder.Eval(Container.DataItem, "FirstName") %>"/>
</td>
<td>
<asp:Label runat="Server"
Text="<%# DataBinder.Eval(Container.DataItem, "LastName") %>"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Code Behind: (Page_Load() for instance)
employees.DataSource = dataset.Tables["empoloyees"].Rows;
employees.DataBind();
I suggest using a Repeater control if you want a piece of markup to iterate over and bind to it.
<asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td> <%# DataBinder.Eval(Container.DataItem, "FirstName") %> </td>
<td> <%# DataBinder.Eval(Container.DataItem, "LastName") %> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
Alternatively, use a GridView, though I find that Repeater gives you more control over the emitted markup.
You are almost there. Try this:
<table>
<%
foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
{
%>
<tr>
<td>
<%= dr[FirstName].toString(); %>
</td>
<td>
<%= dr[LastName].toString();%>
</td>
</tr>
<%
}
%>
</table>
You should use a repeater instead something like this
<table>
<asp:Repeater runat="server" ID="userRepeater" >
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"FirstName")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"LastName")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
and in your codebehind
userRepeater.DataSource = dataset.Tables["empoloyees"];
userRepeater.DataBind();
for a list of all types of <% %> take a look here
You don't have to use <% %> at all. Just put your code in between tags with runat="server", and then go ahead and use standard ASP.NET tags (e.g. asp:TextBox runat="server"). Just make sure it's an *.aspx page.
FWIW, your question seems a little backwards to me. If you are creating *.aspx pages then you can just use standard HTML anywhere you want. If you are trying to somehow squish ASP.NET into an *.html page - then you are misunderstanding how ASP.NET works.

Repeater within Repeater (ajax based wepapp)

For the solution, I cannot use any postback methods, because this is all working through ajax. The solution need to be implemented in the asp.net code.
I have a List<WebPage> that contains a list of Links (List<Link>) and I need for all the links to bind repetitive information such as page title, id, url. Here is my current repeater.
<div id="result">
<asp:Repeater runat="server" id="results">
<Itemtemplate>
<asp:Repeater runat="server" datasource='<%# Eval("Links") %>'>
<Itemtemplate>
<tr class="gradeX odd">
<td><%# Eval("Id") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("Title") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("Url") %></td> //property of WebPage (part of results repeater)
<td><%# Eval("URL") %></td>//Property of Link
<td><%# Eval("URLType") %></td> //Property of Link
<td><%# Eval("URLState") %></td> //Property of Link
</tr>
</Itemtemplate>
</asp:Repeater>
</Itemtemplate>
</asp:Repeater>
</div>
of course this doesnt work, how can i do this?
Thanks for your help!
Try this:
DataBinder.Eval(((RepeaterItem)Container.Parent.Parent).DataItem,
"URL")
The key is to work your way back up to the parent repeater item, and then use the eval method.
Not of course actually.
I have almost the same, but into inner repeater datasource is set as DataSource='<%# GetLinks(Container.DataItem) %>'
where GetLinks returns casted enumerable of Links

Categories

Resources