I want to bind data from one field to gridview, but in gridview it must be displayed as two column, how can I make it possible? Can anybody help me..
Using a Repeater having float (with < 50% width) on item style you can achieve something like that.
markup
<asp:Repeater runat="server" ID="rptMyData">
<ItemTemplate>
<div style="float:left;width: 45%">
<%# Eval("MyField") %>
</div>
</ItemTemplate>
</asp:Repeater>
Related
Hello and thanks for taking your time to help me.
I'm trying to change the text of a textbox thats located inside my repeater.
<asp:Repeater runat="server" ID="rpCategories">
<HeaderTemplate>
<ul id="nav_down" class="nav_down">
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
<FooterTemplate>
<li></li>
<li>Contact</li>
<li><a id="cart_logo"></a>
<asp:Panel runat="server" ID="pnlBasket">
<asp:textbox runat="server" id="txtTotalCount" Enabled="false" CssClass="ltTotalCount"></asp:textbox>
</asp:Panel>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
It's the asp:textbox with the id="txtTotalCount" that I want to change the text of.
Here is my C# code:
TextBox ltTotalCount = (TextBox)FindControl("lblTotalCount");
ltTotalCount.Text = "1";
But if I run the code I get this error : Object reference not set to an instance of an object.
Would be so happy if someone could tell me what I'm doing wrong.
Becuase lblTotalCount is inside a parent control - the repeater, you have to reference it through the repeater.
You should be able to just add the id of your repeater before FindControl, like this...
TextBox ltTotalCount = (TextBox)rpCategories.FindControl("lblTotalCount");
You have to specify the repeater as the parent control to look for the text box and also since it's repeater its quite possible to have more than one text box with that Id so you have to specify which repeater item to look into like so:
TextBox ltTotalCount = rpCategories.Items[0].FindControl("txtTotalCount") as TextBox;
This will return the textbox in the first row of the repeater.
And you should use the Id value not the CssClass value
The ID is probably being 'enhanced' to prevent duplicate IDs in the rendered HTML. You can verify this by looking at the html.
You can add this to the textbox: ClientIDMode="Static" to make the ID stay the same.
You are getting the error because FindControl is returning a null but you are trying to access it anyway.
[Edit] Someone pointed out that this shouldn't work. I agree, here is what does work:
Control FooterTemplate = rpCategories.Controls[rpCategories.Controls.Count - 1].Controls[0];
TextBox MyTextBox = FooterTemplate.FindControl("txtTotalCount") as TextBox;
Is it possible to programatically insert C# Inline Expressions as the values for ASP.NET Controls in your server-side code?
I'm currently using a DataList to display a lot of data in a table. I want to be able to dynamically change the columns of that table, so I need to be able to edit what controls are in its ItemTemplate.
However, in addition to editing the controls in the ItemTemplate, I need to be able to alter the value that is binded to each control in the template, because I want to dynamically change what is being displayed.
So what I currently have is a static table that doesn't change:
<asp:DataList ID="dataList" runat="server" OnSelectedIndexChanged="dataList_OnSelectedIndexChanged" DataSourceID="peopleData">
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="NameLink" OnClientClick="onPageUpdate()" CommandName="Select" Text='<%# Eval(this.Name) %>' ForeColor='<%# Eval("this.NameColor") %>' runat=Server" />
</td>
<td>
<asp:Label Text='<%# Eval("this.Values[\"Age\"]") %>' ForeColor='<%# Eval("this.ValueColors[\"Age\"]") %>' runat="Server">
</td>
// OTHER COLUMNS WITH DIFFERENT DATA
</tr>
</table>
</ItemTemplate>
</asp:DataList>
// OBJECT DATA SOURCE CODE
I know how to dynamically add Controls to the ASPX web page. However, I don't know how to add the inline expressions.
I've tried doing the following but it doesn't work because of the type mismatches:
Label label = new Label();
label.Text = "<%# Eval(\"this.Values[\\\"Age\\\"]\") %>";
label.ForeColor = "<%# Eval(\"this.ValueColors[\\\"Age\\\"]\") %>";
Is there a way of achieving this or doing something similar?
My only other option that I can think of right now is to scrap using the DataList and ItemTemplate and just generate each row myself.. That's a lot more coding versus just using the ItemTemplate.
Not sure if this would help you, but look at http://msdn.microsoft.com/en-us/library/system.web.ui.databinder(v=vs.100).aspx
It is showing using of the hand-written exposure of properties. If your property were always call "magic" and you return the appropriate value for magic within your code would that get you what you need?
Is there a way to get datatable into report format below using Repeater, Gridview, DataList, etc.?
Given DataTable:
dept, section, title
-----------------------
IT,Tech,Tech1
IT,Tech,Tech2
IT,Dev,Dev1
IT,Dev,Dev2
HR,Ben,Spec1
Display Format for Repeater, DataList, GridView, etc.
IT
Tech
Tech1
Tech2
Dev
Dev1
Dev2
HR
Ben
Spec1
You can use nested repeater to fulfill this requirement, assuming no. of columns are fixed in the datatable.
You will have to put 3 repeaters.
example:
<asp:Repeater ID="repeaterDept" runat="server">
<ItemTemplate>
<asp:Repeater ID="repeaterSection" runat="server">
<ItemTemplate>
<asp:Repeater ID="repeaterTitle" runat="server">
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Now, derive distinct Departments from your datatable and bind repeaterDept with that. On, ItemDataBound event, retrieve respective child sections of the binding department and bind it with repeaterSection. Follow the same for repeaterTitle.
I think, this should fulfill your requirement.
I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?
Note: This need to be done using markup only (without code behind).
Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.
I have already referred the following:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/
<asp:TemplateField HeaderText="#" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
By the way, this solution proposed in article you referred. Why you don't like it and ask here?
The best place to do this would be to use the templatefield
<asp:TemplateField HeaderText="Row Number">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
The BoundField displays the value of specified DataSource field as text.
The TemplateField allows to mix html or make use of web controls.
Please refer to the following explanation to confirm the difference.
http://forums.asp.net/t/1804988.aspx/1
The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.
var rowCount = $('#myTable tr').length;
And you should fill the table footer with rowCount value.
Totally new to webforms user controls, I am bit confused, on how to create a user control and fill some data on it.
for(int i = 0; i < Price.EpList.Count(); i++)
{
Price.EpList[i].Amount.ToString();
Price.EpList[i].Code.ToString();
Price.EpList[i].Desc.ToString();
Price.EpList[i].ID.ToString();
}
EpList is a list that contains info that i want to display in webpage on tabular format with checkboxes on each row.
Take a look at the Repeater Control. You don't have to loop through your list, you just bind the list to the repeater and define the html template you want for each repeated item.
http://www.w3schools.com/aspnet/aspnet_repeater.asp
EDIT: That article uses Visual Basic, so here's the C# translation:
Assuming this repeater:
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<%--Html goes here--%>
<%# Eval("Amount")%>
<%# Eval("Code")%>
<%# Eval("Desc")%>
<%# Eval("ID")%>
</ItemTemplate>
</asp:Repeater>
In code behind:
uxEpList.DataSource = Price.Eplist;
uxEpList.DataBind();
If you need to nest a repeater inside another one (using the Desc property from your comment) you can do it like this, by setting the DataSource property declaratively (note the single quotes):
<asp:Repeater runat="server" ID="uxEpList">
<ItemTemplate>
<asp:Repeater Datasource='<%# Eval("Desc")%>' runat="server">
<ItemTemplate>
//etc...