I had Datalist and I want to give item style when I click on it to show user the he select this item I did my code but when I selected item It didnot have any style
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
e.Item.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
e.Item.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Item.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
e.Item.Attributes are not rendered as html, they just don't show up. Add some kind of Control, a panel for example:
<asp:DataList ID="DataList3" runat="server">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server">
<%#Container.DataItem%>
</asp:Panel>
</ItemTemplate>
</asp:DataList>
And then alter your ItemDataBound event
protected void DataList3_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Panel p = (Panel)e.Item.FindControl("Panel1");
p.Attributes.Add("onmouseover", "this.style.cursor='hand';this.style.textDecoration='underline';");
p.Attributes.Add("onmouseout", "this.style.textDecoration='none';");
// ?? p.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.DataList3, "Select$" + e.Item.ItemIndex);
}
}
Or an even better solution for the style part of your question is using css and a classname, add <ItemStyle CssClass="item" /> to your datalist.
css:
.item{cursor:pointer;text-decoration:underline;}
.item:hover{text-decoration:none;}
The "onlick" should be handled by a LinkButton if you ask me, but I don't exactly understand what you are trying to do with that.
Related
Im trying to change the Visible of my Div, but im not being able to find it. Its seems because its inside of another Repeater.
<asp:Repeater ID="uxPesquisaList" runat="server" OnItemDataBound="uxQuestList_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<strong>
<%# DataBinder.Eval(Container.DataItem,"Descricao")%></strong>
</td>
</tr>
<%-- Listagem de Respostas --%>
<asp:Repeater ID="uxRespList" runat="server">
<ItemTemplate>
<tr>
<div id="uxRespostaText" visible="false" runat="server"> ***I want to display this Div***
<td>
<asp:TextBox ID="uxResposta" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Descricao")%>' />
</td>
</div>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
My Code Behind
protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptRespostas = (Repeater)(e.Item.FindControl("uxRespList"));
QuestionarioPergunta pergunta = (QuestionarioPergunta)e.Item.DataItem;
System.Web.UI.HtmlControls.HtmlContainerControl uxRespostaText = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("uxRespostaText"); **I try this, but it always return NullExpection**
if (pergunta.TipoPergunta == "Dissertativa")
{
uxRespostaText.Visible = true;
}
rptRespostas.DataSource = ctx.QuestionarioRespostas.Where(x => x.PergId == pergunta.Id).ToList();
rptRespostas.DataBind();
}
}
You have to look for the TextBox in each item of the inner Repeater. You can set the ItemDataBound event handler of uxRespList in the markup:
<asp:Repeater ID="uxRespList" runat="server" OnItemDataBound="uxRespList_ItemDataBound">
In code-behind, you bind the data of that inner Repeater in the event handler of the outer Repeater (as you already do in your code):
protected void uxQuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptRespostas = e.Item.FindControl("uxRespList") as Repeater;
...
rptRespostas.DataSource = ...
rptRespostas.DataBind();
}
}
And you process each inner Repeater item after its data is bound:
protected void uxRespList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox uxRespostaText = e.Item.FindControl("uxRespostaText") as TextBox;
...
}
}
I am faceing a problem. I have a image view that contains a repeater. What i want to do is when the last image is shown the site needs to be redirected but i dont know how to get the current field that is shown. I was thinking that i could make a variable that holds the current id and compare it every time a new image is shown. And if the new images id is lower than the one in the other variable it has to redirect.
At the moment i dont really have any code to post because i really don know how to solve this. Hope some of you can help.
This is the code for the repeater on the html site
<div id="imgBack" runat="server" class="row" style="background-color: black;">
<div class="col-lg-12">
<div class="container">
<div class="fotorama" data-arrows="false" data-maxheight="750" data-allowfullscreen="native" data-loop="false" data-ratio="1024/750" data-nav="false" data-autoplay="5000" data-click="false" data-swipe="false" data-stopautoplayontouch="false" data-transition="crossfade" data-shuffle="true" data-fit="contain">
<asp:Repeater ID="repImgs" runat="server" OnItemDataBound="repImgs_ItemDataBound">
<ItemTemplate>
<asp:Image ID="imgs" ImageUrl='<%# string.Format("~/Images/{0}", Eval("FileName")) %>' runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</div>
</div>
And here is what i got on the back end so far
protected void repImgs_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == repImgs.Items.Count - 1)
{
Response.Redirect("FinishedSession.aspx");
}
}
}
To get the last item in your repeater you could use ItemDataBound
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == MyRepeater.Items.Count - 1)
{
// Do something
}
}
}
How do you access an asp control within a datalist. For example, I would like to, but currently cannot, access the HyperLink control or the ImageButton control by inline code, or in the code-behind file.
<asp:DataList ID="DataList1" runat="server" AlternatingItemStyle-CssClass="altArtStyle">
<HeaderTemplate>
<table>
<tr>
<td>
<asp:HyperLink ID="lnkTitle" runat="server" NavigateUrl="Default.aspx?order_by=title&direction=ASC" >
Title
</asp:HyperLink> <asp:ImageButton id="imgbtnTitle" src="/_images/hover-down.gif" runat="server"/>
</td>
</tr>
</table>
</HeaderTemplate>
Generally, you need to call FindControl on the DataListItem object, in order to find the control on the specific row. In your example, FindControl will only work on a header row, as in the following example:
Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then
Dim btn As ImageButton = e.Item.FindControl("imgbtnTitle")
If btn IsNot Nothing Then
' Do stuff here.
End If
End If
End Sub
same you can do with labels and hyperlinks
private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
ImageButton imgbutton = (ImageButton)e.Item.FindControl("imgbtnTitle");
imgbutton.ToolTip = "abc";
}
}
It depends. For example, if you wanted to change the header at runtime, in one of the object bind events, you'd do something like for this datalist header, do a findcontrol on the hyperlink and with that reference, do this...
Yes, you can access the asp controls inside the datalist by using the Datalist Item Data bound
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink TitleLink = (HyperLink)e.Item.FindControl("lnkTitle");
}
}
My question is fairly simple. This is what I have for the aspx page:
<ul>
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" /></li>
</ItemTemplate>
</asp:Repeater>
</ul>
I'm trying to get a list of hyperlinks from a SQL server into a list. This is what I have in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = Utilities.RunSelectQuery("SELECT *");
DataTable dt = ds.Tables[0];
linksList.DataSource = dt;
linksList.DataBind();
}
How do I change the NavigateUrl and Text properties in the asp:HyperLink after data's been bound to the Repeater? I want to do this in the codebehind, I can get it to work if I do it using <%# Eval("URL") %> in the aspx page but that's sort of against what ASP.NET is all about.
Edit: this is the solution that worked for me thanks to womp:
protected void linksList_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = (DataRowView)e.Item.DataItem;
HyperLink link = (HyperLink)e.Item.FindControl("link");
link.Text = row["description"].ToString();
link.NavigateUrl = row["URL"].ToString();
}
}
Actually, using the Databinder syntax in your templates is a great way to do it, I'm not sure what you mean that it's "against what ASP.Net is all about".
However, if you really want to do it in code, you can do it right in your OnItemDataBound handler (which it looks like you've created). Something like this (read: untested) should do the trick:
void linksList_OnItemDataBound(object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem) {
DataRow row = e.Item.DataItem as DataRow;
Hyperlink link = e.Item.FindControl("link"));
link.Text = row["URL"];
link.NavigateUrl = row["URL"];
}
}
}
The way to do it is just as you said. Doing it in the code behind adds unnecessary work if you only wish to display the URL. You also would want to put the tags in the HeaderTemplate and FooterTemplate.
<asp:Repeater runat="server" ID="linksList" OnItemDataBound="linksList_OnItemDataBound" >
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink runat="server" ID="link" NavigateUrl='<%# Eval("url") %>' /></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
I have a datalist control which some controls(ex: button) are in it. I want to write some code into click event of button which is in the datalist control. But in the code behind page I can't see the name of controls into datalist. How can I solve this problem?
Attach your event to the controls in the OnItemCreated event of the datalist.
EDITED TO ADD SAMPLE
private void DataList_ItemCreated(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("btnWhatever");
if (btn != null) btn.Click += new EventHandler(SomHandler);
}
}
If you don't want to add a handler to all the child events, you could instead add your code to the OnItemCommand.
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<asp:Button ID="btnDoSomething" Runat=server CommandName="DoSomething"
CommandArgument="<%# DataBinder.Eval(Container.DataItem, "SomeID")
%>"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
protected void DataList1_ItemCommand(
object source, DataListCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
//Do stuff
}
}