How to use asp:placeholder inside a repeater - asp.net

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.

Related

how to add a rowspan to the first column in a gridview?

I have a gridview and in the first column I added a table to create a second row:
I would like the shipper address to span across all the columns in the gridview.
Code:
<asp:GridView CssClass="printTableResults" runat="server" ID="gvHawb"
ShowHeaderWhenEmpty="false"
DataKeyField="ID"
OnRowDataBound="gvHawb_RowDataBound"
AutoGenerateColumns="false"
allowpaging="false">
<HeaderStyle CssClass="printTableHeader" />
<RowStyle CssClass="borderB" />
<Columns>
<asp:TemplateField HeaderText="HAWB" HeaderStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<table>
<tr>
<td><asp:Label ID="lblAwbNumber" runat="server" Text='<%# Eval("AWBNumber") %>'></asp:Label></td>
</tr>
<tr>
<td><label>Shipper</label></td>
<td><asp:Label ID="lblFullShipperAddress" runat="server" Text='<%# Eval("FullShipperAddress") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pieces" HeaderStyle-Font-Bold="true" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblPieces" runat="server" Text='<%# Eval("TotalNoOfPieces") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Is there a way to make shipper span the rows of the gridview?
There is a total of 8 columns in this gridview.
You could try moving the table with the shipper address to the ItemTemplate of the last column in the outer table. Then put the table on its own tr. Something like this:
<ItemTemplate>
<asp:Label ID="lblPieces" runat="server" Text='<%# Eval("TotalNoOfPieces") %>'></asp:Label>
</tr> <!-- end the outer table row -->
<tr colspan="7"> <!-- start new row in the outer table -->
<table>
<tr>
<td><asp:Label ID="lblAwbNumber" runat="server" Text='<%# Eval("AWBNumber") %>'></asp:Label></td>
</tr>
<tr>
<td><label>Shipper</label></td>
<td><asp:Label ID="lblFullShipperAddress" runat="server" Text='<%# Eval("FullShipperAddress")%>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
Note that you should not finish up with an ending </tr> because the outer table rendering will add that.
This answer was put together without any real testing so I'm not guaranteeing that it will work "out of the box".
If you want the first cell to span all 8 columns of the row, you can add this handler to the PreRender event of the GridView:
void gvHawb_PreRender(object sender, EventArgs e)
{
foreach (GridViewRow row in gvHawb.Rows)
{
if (/* some condition */)
{
// Remove the other cells of the row
while (row.Cells.Count > 1)
{
row.Cells.RemoveAt(1);
}
// Set the ColumnSpan attribute
row.Cells[0].ColumnSpan = 8;
}
}
}
I assume that it would be done on rows that meet a specific condition.
You may also consider using a ListView, which is easier to customize than the GridView.

AsyncPostBackTrigger in nested repeater

Good day everyone,
This is my first post in here and I would like to thank you all for the great efforts in this forum by which I have already gaind a lot of skills.
I have a smalle issue with two nested repeaters. Basically, I have a dropdownlist in a child repeater which contains rating values and every time the dropdownlist is changed in the child repeater the new percentange is calculated and presented in a label in the parent repeater. This will cause full postback which is really frustrating when going through too many dropdownlists. My question is how to reflect the new calculated percentange in the label without postback. I have tried to use AsyncPostBackTriggers but no luck. Any suggestions would be appreiciated
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Wrap your aspx mark up inside update panel like this.
<asp:UpdatePanel runat="sever" ID="upParentChild" >
<ContentTemplate>
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
This will make sure that only controls inside update panel are posted back and not the whole page.

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" />

ASP.NET Repeater will not show data

I have an ASP.Net Repeater I want to use to show From and Text from a dataset and I want to add it programmically. I have all the data in a dataset and can use that and I have verified the correct number for datarows when it loads so the data is their it is just not showing. What have I missed?.
Dim data As New Data
Dim ds As New DataSet
ds = data.LOADALL()
Dim drMsg() As DataRow = ds.Tables("MESSAGESYSTEM").Select("TOID='101'")
repeatMessages.DatagSource = drMsg
Now on the html side I have:
<asp:Repeater ID="repeatMessages" runat="server" >
<HeaderTemplate>
<table>
<tr>
<th>From</th>
<th>Sublect</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label1" text='<%# Eval("FROMID") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label2" text='<%# Eval("MESSAGETEXT") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
How can I fix this code to show the data in the Message table?
Try calling repeatMessages.DataBind(). All you are doing is assigning the source but you haven't told the program to do something with the data.

UpdatePanel in each datalist row

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>

Resources