UpdatePanel in each datalist row - asp.net

I am trying to update each DataList row without having to PostBack the entire DataList.
Basically I want to go with
<DataList>
<ItemTemplate>
<UpdatePanel>
</UpdatePanel>
</ItemTemplate>
</DataList>
instead of
<UpdatePanel>
<DataList>
<ItemTemplate>
</ItemTemplate>
</DataList>
</UpdatePanel>
Is this possible, or better - does it make any sense?

Yes, It is possible, I personally do that in some situation.
But you must be aware that with an UpdatePanel is not a perfect solution. As an example, the client sent the complete ViewState of the page. So there will be a gain, but not as big as you might expect.
I suggest you to read the UpdatePanel Considerations section in this link for more information.
Update :
You must also watch how you setup you template, example : you cannot put a table row in a update panel.
Don't do this :
<asp:DataList>
<HeaderTemplate>
<Table>
</HeaderTemplate>
<ItemTemplate>
<UpdatePanel>
<tr>
<td>...</td>
<td>...</td>
</tr>
</UpdatePanel>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</asp:DataList>
But you cant do :
<asp:DataList>
<HeaderTemplate>
<Table>
</HeaderTemplate>
<ItemTemplate>
<UpdatePanel>
<tr>
<td>
<UpdatePanel>
...
</UpdatePanel>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</asp:DataList>

Related

ASP.net DataGrid adding extra information to table I do not want

Here is what DataGrid does to my table:
<table id="ContentPlaceHolder1_articleList" cellspacing="0" style="border-width:0px;border-collapse:collapse;" headertext="File Name" rules="all">
How can I remove the unnecessary info from the table?
I do not want cellSpacing, anything in the style. I take care of all of thise with CSS and because these are inline it is overriding my CSS declarations.
I don't care if the ID, or rules="all" (not even sure what that does) or the HeaderText is present, but I would love to get rid of the rest.
As Curt suggested, you can use a Repeater control for more control over the output. Another option would be the ListView control, which gives you more options than the the Repeater does.
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ColumnName", "{0:#,###} bytes") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
If you really want full control over your table, I would recommend using a Repeater instead. Unlike the DataGrid control, the Repeater control doesn't render any HTML which isn't inside the ItemTemplate giving you full control over your rendered code.
<table>
<tr>
<td>Column Header</td>
</tr>
<asp:repeater id="rep" runat="server">
<itemtemplate>
<tr>
<td>
<%#eval("ColumnName") %>
</td>
</tr>
</asp:repeater>
</table>
As the the Eval/DataItem will give object/String return value. In
order to convert into 3,546 bytes output, it is required to convert it into
integer. For that onitemdatabound event registration will be required.
Mark Up
<asp:ListView ID="ListView1" runat="server" onitemdatabound="rpt_ItemDataBound">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lbl" runat="server" ></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
Code Behind
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl= (Label)e.Item.FindControl("lbl");
lbl.Text = String.Format("{0:#,### bytes}",
Convert.ToInt32(((YourClassName)e.Item.DataItem).YourProperty);
}
}

SortExpression in ASP.NET GridView not working with <HeaderTemplate>

I have an ASP.NET GridView. Now I am adding the SortExpression property tothe <TemplateField> tags, to make specific columns sortable.
Now, one of the columns has some markup content to be added in the header. The problem is, SortExpression does not work if there is a <HeaderTemplate> tag in a <TemplateField>, you have to put it inside the HeaderText property of <TemplateField>. But, all the HTML content does not work properly if I dump it inside the HeaderText property.
<asp:TemplateField SortExpression="FK_TesterID" ItemStyle-Width="300px" FooterStyle-Width="300px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
Tester
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged" ToolTip="Bulk Assign Testers !" ></asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
So you can see, if I put the entire <HeaderTemplate> property inside a headertext, it doesn't work.
But I want to have both functionalities. Can anyone help?
Then you need to provide a control in your HeaderTemplate with CommandName="Sort", for example a LinkButton.
<HeaderTemplate>
<table width="100%">
<tr>
<td align="center">
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px"
DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField="PK_ID"
Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged"
ToolTip="Bulk Assign Testers !">
</asp:DropDownList>
</td>
</tr>
</table>
</HeaderTemplate>
This is a quite old thread I have stumbled over while trying to solve exactly that described problem but the solution provided here didn't worked for me. If you have a Sorting method defined for the GridView then
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort" />
will call that method
protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
dt.DefaultView.Sort = e.SortExpression;
but e.SortExpression will be null and no sorting is happening. You have to first pass the Column's name through the CommandArgument of the LinkButton. Only then it worked in my case!
<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" CommandArgument="ColumnName" Text="Sort" />

Disable Select on a ListView

I would like to disable the ListView's Select command based upon other data in the row. For example, if the UserStatus is "T", I'd like to gray out the Select hyperlink and prevent selection of that row.
I've accomplished the same thing in a GridView by the following statement in the RowCreated event. However, I haven't been able to rework that code for a ListView.
CType(e.Row.Controls(0), WebControl).Attributes("disabled") = "true"
<asp:listview runat="server" id="ListView">
<itemtemplate>
<tr id="rowUsers" runat="server">
<td><asp:linkbutton id="btnEdit" runat="server" text="Select" onclick="btnEdit_Click" /></td>
<td><asp:label id="UserNameLabel" runat="server" text='<%# Bind("UserName") %>' /></td>
<td><asp:label id="UserStatusLabel" runat="server" text='<%# Bind("UserStatus") %>' /></td>
</tr>
</itemtemplate>
Generated output...
<tr id="ListView_rowUsers_0">
<td><a id="ListView_btnEdit_0" href="javascript:__doPostBack('ListView$ctrl0$btnEdit','')">Select</a></td>
<td><span id="ListView_UserNameLabel_0">Adams,John P</span></td>
<td><span id="ListView_UserStatusLabel_0">T</span></td>
</tr>
Better use ItemDataBound event and do a FindControl("btnEdit") and simply set the Enabled property.
How to use ListView ItemDataBound event.
in your item template you can use the databinding syntax to disable the button
<ItemTemplate>
<asp:LinkButton id="btnEdit" runat="server" text="Select"
Enabled=<%# Eval("UserStatus") == "T" %> />
</ItemTemplate>
try
CType(e.Item.Controls(0), WebControl).Attributes("disabled") = "disabled"
on ListView's ItemCreated

How display data in a gallery view mode

Is there a way to display gridview as "gallery view" instead of plain "list view". For example like ebay(you can toggle the view 1.list view, 2.gallery view, 3. side by side view). Any asp.net controls or jQuery controls will do the job for me. Please suggest.
One way to accomplish this would be to use the ListView server control to make a template that looks like the "Gallery View" you have in mind.
<asp:ListView runat="server" ID="ListView1"
DataSourceID="SqlDataSource1"
GroupItemCount="5">
<LayoutTemplate>
<table runat="server" id="table1">
<tr runat="server" id="groupPlaceholder">
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr runat="server" id="tableRow">
<td runat="server" id="itemPlaceholder" />
</tr>
</GroupTemplate>
<ItemTemplate>
<td runat="server">
<%-- Data-bound content. --%>
<asp:Label ID="NameLabel" runat="server"
Text='<%#Eval("Name") %>' />
</td>
</ItemTemplate>
</asp:ListView>

How to use asp:placeholder inside a repeater

I have a HTML table that is built with a repeater. Sometimes the table has 5 rows, sometimes it has 8 rows.
It was easy to add the header rows.
<asp:PlaceHolder runat="server" ID="additionalHeaderColumns" />
In code behind I added controls (htmlheadercells) to the contentholder:
Dim tableHeaderCell As New TableHeaderCell()
tableHeaderCell.Text = "Test"
additionalHeaderColumns.Controls.Add(tableHeaderCell)
When I try to do the same in the databound event for each row I get an errormessage that tells that its not possible to put asp:PlaceHolder inside the row. it expects tablecells.
Anybody have a solution how to expand number of columns in a repeater?
if i understand your problem as "add columns to the table depending on some data":
<asp:Repeater ID="outerRepeater" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>fixedHeaderCell</td>
<td>fixedHeaderCell2</td>
<asp:Repeater ID="innerColumnRepeater" runat="server" DataSource='<%# someFreakySourceFromYourPageOrOuterRepeater %>'>
<ItemTemplate>
<td>dynHeaderCell</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>fixedContentCell</td>
<td>fixedContentCell2</td>
<asp:Repeater ID="innerColumnRepeater" runat="server" DataSource='<%# someFreakySourceFromYourPageOrOuterRepeater %>'>
<ItemTemplate>
<td>dynContentCell</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
look further for nested repeater, there is a lot of content here.

Resources