How can I create a template in c# for an AXPxGridViewDataTextColumn without any markup and with using Eval to display the DataItem value?
-The problem that I am having is that the string "<%#Eval("dataTableField1")%>" shows up in the GridView for every row instead of the appropriate values.
Here is an example of my attempt:
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id=\"hr\" style=\"height:100%\"><%#Eval(\"dataTableField1\")%></div>");
Container.Controls.Add(lit);
}
}
Here is an example of what I want to do with the row height taken from this link:
<dx:GridViewDataTextColumn FieldName="Description" VisibleIndex="3">
<DataItemTemplate>
<div id="hr" style="height:100%">
<%#Eval("Description")%>
</div>
</DataItemTemplate>
</dx:GridViewDataTextColumn>
This documentation link shows similar examples of using templates in the markup but I want to do it in the code behind.
Here is a link on creating templates.
Thanks in advance,
Soenhay
Edit:
I was able to get the first element to properly display by moving the template assignment to the CustomColumnDisplayText event but all other elements displayed in the ASPxGridView show the Eval string.
I had 2 other solutions but I removed them as I think this is the best ( I would be grateful for any other suggestions/improvements ):
public override void DataBind()
{
...
GridViewDataTextColumn myCol = new GridViewDataTextColumn();
myCol.Caption = "col1";
myCol.FieldName = "dataTableField1";
myCol.DataItemTemplate = new ColumnDataItemTemplate();
theGridView.Columns.Clear();
theGridView.Columns.Add(myCol);
theGridView.DataSource = AdjustDataSource();
theGridView.DataBind();
...
}
public class ColumnDataItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
GridViewDataItemTemplateContainer Container = (container as GridViewDataItemTemplateContainer);
LiteralControl lit = new LiteralControl("<div id='hr' style='height:100%; font-size:x-large;'>" + DataBinder.Eval(Container.DataItem, Container.Column.FieldName) + "</div>");
Container.Controls.Add(lit);
}
}
At this link I have found a reason to not use the event:
"The CustomColumnDisplayText event should not be handled for template columns. "
This link helped with the DataBinder.Eval part.
Heres an example but mine is binding with a database, i usually use EditTemplte
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BSA" SortExpression="BSA">
<ItemTemplate>
<asp:Label ID="lblBSA" runat="server" Text='<%# Bind("BSA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
Related
I have
<asp:Label ID="lbl_ReadOnlyFld" runat="server"></asp:Label> <%=GetGuiLocalString("lbl_ReadOnlyFldDescr")%>
I need the text in some element so I can access it:
For example:
<asp:Label ID="lbl_InputFld" runat="server"></asp:Label><asp:Label ID="lbl_InputFldDescr" runat="server" text=' <%= GetGuiLocalString("lbl_InputFldDescr")%>'></asp:Label>
It just gives me stuff inside ''... any help would be appreciated.
Regards.
<%= is meant to directly output something on the page and can't be used inside other controls.
You should either use <%# and databind the control or set the text in the code behind. Also the other stuff should be inside those tags so it would be:
<asp:Label ID="lbl_InputFldDescr" runat="server" text='<%# " " + GetGuiLocalString("lbl_InputFldDescr")%>' />
and then lbl_InputFldDescr.DataBind(); somewhere in your code behind (provided you don't already databind the page or someting).
ASPX:
<asp:Label ID="lbl_InputFld" runat="server">my name is Jhon</asp:Label>
<asp:Label ID="lbl_InputFldDescr" runat="server" text='<%# " " + GetGuiLocalString("lbl_InputFldDescr")%>'></asp:Label>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
public string GetGuiLocalString(string id)
{
string s = "hello";
Label lbl = (Label)Form.FindControl(id);
if(lbl!=null)
{
if ( ! string.IsNullOrEmpty(lbl.Text))
s = lbl.Text;
}
return s;
}
I have a gridview that has link and description to be rendered on the page.
written the below code in gridview in .aspx
<Columns>
<asp:TemplateField>
<ItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</ItemTemplate>
<ItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<AlternatingItemTemplate>
<p>
<asp:HyperLink ID="hlLink" runat="server" Target="_self"></asp:HyperLink></p>
</AlternatingItemTemplate>
<AlternatingItemTemplate>
<p>
<asp:Literal ID="litSummary" runat="server"></asp:Literal></p>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
and below in .aspx.csin gridview rowdataboundevent
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SearchResultItem data = (SearchResultItem)e.Row.DataItem;
HyperLink hlLink = (HyperLink)e.Row.FindControl("hlLink");
Literal litSummary = (Literal)e.Row.FindControl("litSummary");
if (data.Description != null)
{
hlLink.Text = data.Title;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Description;
}
else
{
hlLink.Text = data.Path;
hlLink.NavigateUrl = data.Path.Replace("&", "&");
litSummary.Text = data.Path;
}
}
here SearchResultItem: is the result item that has link and description details.
First time when row bound event is called, it binds the data correctly, second time when called throws error "Multiple controls with the same ID 'hlLink' were found. FindControl requires that controls have unique IDs.
Please let me know whats error with the code.
Thanks
Problem : you are trying to create the same controls with same ID multiple times.
Solution : you need to remove the controls before creating them.
Try This:
void RemoveControls()
{
HyperLink l1 = (HyperLink)Page.FindControl("hlLink");
Literal l2 = (Literal)Page.FindControl("litSummary");
if(l1!= null)
Page.Controls.Remove(l1);
if(l2!= null)
Page.Controls.Remove(l2);
}
Solution 2: Pagination for Repeater control.
for implementing pagination in Repeater control you need to create PagedDataSource.
Try This:
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 8;//page sizes
Need a hand trying to get my rotating banner working properly on my website. I'm using the jquery cycle plugin which manages the rotation. Within my CMS I've got something called a smartform, which contains upto 6 pictures. The code below (something which I wrote by following a tutorial for the banner) works really well. However I would like to somehow get the index of the image and place it within the alt tags. What I am trying to achieve is the alt tag to say "Banner_(ImageIndexNumber)".
Hopefully someone can help, thanks all
C# Codebehind
private void BannerFill(int contentId)
{
try
{
uxBannerContentBlock.DefaultContentID = contentId;
uxBannerContentBlock.Fill();
string xml = uxBannerContentBlock.EkItem.Html;
SmartForm.RotatingBanner.BannerImage bannerGroup = (SmartForm.RotatingBanner.BannerImage)
Ektron.Cms.EkXml.Deserialize(typeof(SmartForm.RotatingBanner.BannerImage), xml);
List<BannerSlide> slides = GetBannerSlides(bannerGroup.Slides);
//Databind//
uxBannerRepeater.DataSource = slides;
uxBannerRepeater.DataBind();
}
catch { }
}
protected List<BannerSlide>
GetBannerSlides(SmartForm.RotatingBanner.BannerImageSlides[] bannerGroupSlides)
{
List<BannerSlide> bSlides = new List<BannerSlide>();
foreach (SmartForm.RotatingBanner.BannerImageSlides bgSlide in bannerGroupSlides)
{
bSlides.Add(new BannerSlide(bgSlide.Image.img.src));
}
return bSlides;
}
public class BannerSlide
{
//properties//
public string SlideImage { get; set; }
//constructor//
public BannerSlide(string slideImage)
{
SlideImage = slideImage;
}
}
Front end
<div class="slideshow">
<CMS:ContentBlock ID="uxBannerContentBlock" runat="server" Visible="false" />
<asp:Repeater runat="server" ID="uxBannerRepeater">
<ItemTemplate>
<img src="<%# DataBinder.Eval( Container.DataItem,"SlideImage") %>" alt="Banner_<%# Container.ItemIndex %>" />
</ItemTemplate>
</asp:Repeater>
alt='<%# "Banner_" + Container.ItemIndex %>'
I am trying to add an item to the top of the drop down. I am using ItemTemplates so i am doing the databind and trying to add one at the top that reads
[ ] All Profiles
I was able to add it, but it is overriding the binding of the actual data, so when i added this now only the [ ] All profiles is there but not the real binded data. What am I doing wrong?
By the way i am a newbie in c# :)
Thank you
public void BindData()
{
myCombo.DataSource = myDbConnection.GetValues();
myCombo.DataTextField = "Name";
myCombo.DataValueField = "ID";
myCombo.DataBind();
var tempProfiles = new[] { new { Name = "All Profiles", ID = "1" } };
myCombo.DataSource = tempProfiles;
myCombo.DataBind();
}
<telerik:RadComboBox ID="myCombo" EmptyMessage="All Types" runat="server" Width="200px">
<ItemTemplate>
<div onclick="StopPropagation(event)">
<asp:CheckBox runat="server" ID="chk1" onclick="onCheckBoxClick(this)"/>
<asp:Label runat="server" ID="lblProfile" AssociatedControlID="chk1">
<%# Eval("Name") %>
</asp:Label>
</div>
</ItemTemplate>
</telerik:RadComboBox>
In your example you're overwriting your DataSourceObject with your 1-item-list.
You should add the item "manually" after your DataBind call:
myCombo.Items.Insert(0,
new Telerik.Web.UI.RadComboBoxItem { Text = "All Profiles", Value = "1" }
);
myCombo.SelectedIndex = 0;
if you work in Bound mode all data should come from the DataSource.
what I usually do is to add an extra element in the datasource with ID 0 or int.MaxValue depending on the sort order and the position where I want to show it.
I have an ASP:Repeater Which I would like to display a list of check boxes in. These check boxes are related to a list of user preferences and the users resulting answer. See Code Bellow.
I would like to add do one of the following if possible
Option 1: It would be great if I could use the Event in the Repeater:OnItemCommand(...) to fire if any of the items change. It would seem to me that this event will only fire if there is a Button | LinkButton | ImageButton item in the list. IE it will not fire if I put in a check box with AutopostBack="True"
Option 2: Is there a way I could attach a method to an Event of CheckBox:CheckChanged I would need to pass this method a parameter saying which question/answer combo to change.
Option 3: Its your answer if you know an easier way that would be awesome.
The Code:
<asp:Repeater ID="RPTprefs" runat="server" DataSourceID="getAnswers" OnItemCommand="RPTprefs_ItemCommand">
<ItemTemplate>
<li><asp:CheckBox ID='questionID' runat="server"
Checked='<%# Eval("pr.up_is_selected") %>'
Text='<%# Eval("prp.prefs_question") %>'
AutoPostBack="true"
OnCheckedChanged="CheckChanged" /></li>
</ItemTemplate>
</asp:Repeater>
Thanks in advance
Here is what I came up with, which is basically your option #2.
In the ItemTemplate of the repeater, I use a Literal control (Visible set to false) which has the argument you wish to pass to the CheckedChanged function. The reason for using a control is because the control will retain its value in the ViewState after a post back, whereas the original data source for the Repeater will be lost.
In the OnItemCreated function, I bind the CheckChanged function for all of the check boxes to pass in the right argument. Here's my complete example. In this case, I want to pass the Id property of my data to the CheckChanged function.
Markup:
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="ItemCreated">
<ItemTemplate>
<asp:Literal ID="litArg" runat="server" Visible="false" Text='<%# Eval("Id") %>'>
</asp:Literal><%# Eval("Name") %>
<asp:CheckBox ID="chkCool" runat="server" AutoPostBack="true" Checked='<%# Eval("IsCool") %>' /><br />
</ItemTemplate>
</asp:Repeater>
Code behind:
public class SomeClass
{
public SomeClass(bool c, string n, int id)
{
IsCool = c;
Name = n;
Id = id;
}
public bool IsCool { get; set; }
public string Name { get; set; }
public int Id { get; set; }
}
.
.
.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<SomeClass> people = new List<SomeClass>();
people.Add(new SomeClass(true, "Will", 666));
people.Add(new SomeClass(true, "Dan", 2));
people.Add(new SomeClass(true, "Lea", 4));
people.Add(new SomeClass(false, "Someone", 123));
Repeater1.DataSource = people;
Repeater1.DataBind();
}
}
private void CheckChanged(int id)
{
Response.Write("CheckChanged called for item #" + id.ToString());
}
protected void ItemCreated(object sender, RepeaterItemEventArgs e)
{
//this needs to be set again on post back
CheckBox chk = (CheckBox)e.Item.FindControl("chkCool");
Literal arg = (Literal)e.Item.FindControl("litArg");
Action<object, EventArgs> handler = (s, args) => CheckChanged(Convert.ToInt32(arg.Text));
chk.CheckedChanged += new EventHandler(handler);
}
Hope that helps.