ASP literal inside a repeater footer template - asp.net

I think this is not possible by trying to add a literal into a footer template of a repeater so that I can fill it later on...
<FooterTemplate>
<asp:Literal ID="panelFooter" runat="server"></asp:Literal>
</FooterTemplate>

You can data bind the Text attribute to the result of a method in your code-behind. In this example we are going to display the total number of products in the footer:
<FooterTemplate>
Total number of products:
<asp:Literal runat="server" Text='<%# GetTotalNumberOfProducts() %>' />
</FooterTemplate>
In the code-behind we are going to create a method that calculates the number and returns it for the Literal control.
protected string GetTotalNumberOfProducts()
{
return 42.ToString();
}
Notice that we don't need to find the control by its ID. We are just returning s string and the data binding syntax will call our method in code-behind and put the result inside the control.

Related

Nested CheckBoxList using DataSet and Repeaters

I'm trying to wire up some CBL's using a repeater and dataset. The dataset contains 2 tables with the same schema and a (one, single) relation (in SQL land think of it as a self join).
When the control is rendered if I set my DataSource = the relationship, I am able to get the children elements to show; so I know the model is good, although misplaced (on purpose to test the model - see code below).
The problem is:
I am having difficulty getting the parent elements to show up at all
Not all parents have children, and the ones that don't still need to show up
Am I approaching this in the right frame of mind? i.e. Am I missing something fundamental? Implementation outside of CBL(plain text) works fine per this article
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ParentCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem,"Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<asp:CheckBoxList ID="ChildCBL" runat="server"
DataSource='<%# DataBinder.Eval(Container.DataItem, "Joined") %>'
DataTextField="TextProperty"
DataValueField="ValueProperty">
</asp:CheckBoxList>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Page Load is nothing spectacular
DataSet ds = Foo.foo();
ParentRepeater.DataSource = ds.Tables["Parent"];
ParentRepeater.DataBind();
In your code you don't bind ChildRepeater
If you want just change plain text to Checkbox in according to https://support.microsoft.com/en-us/kb/306154
then you don't need to use CheckBoxList. But you should only use single checkbox inside repeater.

Getting the bound type of templated column controls in gridView

Hi I have following grid view
<asp:GridView ID="grdSettings" runat="server" Height="400px" Width="100%"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderText="XYZ" BoundFieldName="XYZTypeName">
<ItemTemplate>
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>'>
</asp:Label>
<asp:Label ID="lblCostumerId" runat="server" Text='<%#Bind("CustomerId") %>'></asp:Label>
</asp:TemplateField>
</Columns>
</asp:GridView>
Which is bound by List of Customer ,class is as follows
Class Customer
{
public string CustomerName { get; set; }
public int CustomerId { get; set; }
}
Now on a method named GetGridStuff() , i need to iterate in every column and get the type that was bound to controls in template field. For example in case of the first control in the template field
<asp:Label ID="lblCustomerName" runat="server" Text='<%# Bind("CustomerName") %>' >
</asp:Label>
I need to know what type of property data it contains , in this case its CustomerName. I need to get this dynamically at run time as to write code in part of program which is not aware if this grid structure. I have the grid object with me and i can access all properties.
Let me try this, being away from coding for more than 5 years now and if I understand the problem correctly as you may want to handle it through server side code.
Look for an event called ItemBound (or similar forgive me for my memory), this gives you values of all the items in current row (Properties). You also need to declare temp control types (label,textbox etc) and assign corresponding value to these controls using e.FindControl with appropriate type casting, e.g. Label l = (Label)e.Findcontrol("Name")
downside of the approach you should avoid too much of process as it is going to execute on every row creating of the grid.
Let me know if you still want a precise code to handle the problem but otherwise this description should at least help you to look for Row Level Event to crack the prob and also encourage you to stick to tech forums :)

Get the this object using eval in a gridview in asp

I can't find any answer anywhere. I want to refer to the row object itself in an databinding expression in a gridview, like this:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server"
Text = '<%# GetPendingReason(Eval("this")) %>' />
</ItemTemplate>
</asp:TemplateField>
But it doesn't work because "this" doesn't refer to any attribute. Referencing individual attributes works fine, but how do you refer to the current row?
Simply use <%# Container.DataItem %>. Do not use Databinder.
If you want to refer to the current row you do that at codebehind using
GridViewRow row = GridView1.Rows[index];
at any of the GridView event.

Tuncate string in ASP.NET using VB.NET

I made a function to truncate a string in the code behind file. But how do i use it in the aspx file?
This is the textbox:
<asp:TemplateField HeaderText="page" HeaderStyle-Wrap="true">
<ItemTemplate>
<a href='<%# makepageURL( Eval("page") )%> '>
<%# Eval("page")%>
</a>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpage" TextMode="SingleLine" Rows="1" Width="100%" runat="server" Text='<% #Bind("page") %>' />
</EditItemTemplate>
</asp:TemplateField>
And this is my function:
Public Function TrimString(ByVal Value As String, ByVal Length As Integer) As String
If Value.Length > 20 Then
Return Value.Substring(Value.Length - (20 - 3)) + "..."
End If
Return Value
End Function
It's not an issue of how to use it, but actually when to use it?
If you had a regular span, you could do this:
<span><%: TrimString("somestring") %></span>
But this is a TextBox your dealing with (user input).
When should it truncate?
On Form Submit? (that would make sense).
As they type (well then you'd need to use JavaScript).
By the looks of your code snipper, your using a FormView.
So i wouldn't be calling it from the ASPX (which the equivalent of executing code during Page Render), i would be calling it during the Edit/Submit event, server-side event handler.
In other words, truncate the value the user put in, after they have submitted the form and before you persist to the database.

Totally lost – data binding expressions inside GridView’s template

1) On aspx page we define GridView control named gvwPolls, and inside its template we define a user control named pollBox1
<asp:GridView ID="GridView1" DataSourceID="objPolls" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
Question is : <%# Eval("QuestionText") %> <br />
<mb:PollBox ID="PollBox1" runat="server" PollID='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objPolls" ...></asp:ObjectDataSource>
a) I assume that inside gvwPolls’s template, the gvwPollBox1.DataBind is called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?!
b) Can someone offer some explanation how or why is gvwPollBox1.DataBind called before PollID='<%# Eval("ID") %>' and <%# Eval("QuestionText") %> expressions get evaluated?
2) Continuing with the above example:
-- pollBox1 user control defines a Repeater control named rptOptions:
<asp:Repeater runat="server" ID="rptOptions">
<ItemTemplate>
<%# Eval("pollBoxTitle") %>
</ItemTemplate>
</asp:Repeater>
-- In pollBox1’s code-behind file we bind rptOptions to a data source inside DoBinding() method.
-- We also override pollBox1’s DataBind() method:
public override void DataBind()
{
base.DataBind();
DoBinding();
}
a) I assume that due to overriding pollBox1.DataBind(), the data binding expression <%# Eval("pollBoxTitle") %> ( defined inside rptOptions’s template ) will get evaluated prior to a call to DoBinding method? If so, won’t then <%# Eval("pollBoxTitle") %> get evaluated before rptOptions is actually bound to a data source?
b) If that is the case, how then is rptOptions able to extract value ( from data source’s pollBoxtitle property) from a data source, if at the time the <%# Eval("pollBoxTitle") %>
expression got evaluated, rptOptions wasn’t yet bound to any data source?
thanx
I can't explain why the page life cycle is the way that is, probably has something to do with rendering childs before the parent object. When exactly do you call .DataBind() in the PollBox control? Try to move it into an event that is later in the life cycle, like PreRender.
There is also another way to ensure it is working the way you want to:
Subscribe to the RowDataBound Event, use .FindControl("YourPollBoxID") to get the instance of the control current bound row, set the properties and perform a manuall .DataBind();

Resources