I have a repeater object that looks like the following
<asp:Repeater id="rptSpecialNotes" runat="server">
<headerTemplate><ol type="A"></HeaderTemplate>
<itemtemplate>
<li>
</li>
<asp:UpdatePanel ID="udpSpecialNotesRepeater" runat="server" UpdateMode="Always">
<ContentTemplate>
<div id="specialNotes" name='<%# Eval("itemId") %>' runat="server">
<asp:imagebutton runat="server" width="20" class="floatLeft" id="specialNotesItem" imageurl='<%# Eval("ImageUrl") %>' commandname="specialNotesImageChange" commandargument='<%# Eval("itemId") %>'></asp:imagebutton>
<span class="subject"><p><%# eval("Subject") %></p></span>
<br /><br />
<p><%# Eval("AgendaNote")%></p>
<br />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID ="followAlongControl" EventName ="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Repeater id="specialNotesAttachmentsRepeater" OnItemCommand="specialNotesAttachmentRepeater_ItemCommand" runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("attachmentRelation") %>' >
<itemtemplate>
<br />
<a href='<%# Container.DataItem("attachmentPath") %>'><%# Container.DataItem("attachmentName") %></a>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
<footerTemplate></ol></FooterTemplate>
</asp:Repeater>
I want to change the background color of the div 'specialNotes' depending on the imageurl of the image button. So if it's checked, the div would be grey, if not, then i'd leave it blank.
Is there a way to do this in the code-behind with VB?
You don't have to "Find" the div after the page has loaded, you can do this as it is getting bound. I imagine that when you say "if it's checked" that you refer to the imagebutton acting as a checkbox, and you want to change the color depending on the location(path) of the image. So I would this in the div:
<div style='<%# doColor(Eval("ImageUrl")) %>'>
Content in the div
</div>
and then this in the code behind:
Public Function doColor(img As Object) As String
If img.ToString() = "MyPath" Then
Return "background-color:red"
Else
Return "background-color:green"
End If
End Function
that way if the ImageUrl equals "MyPath" the background of your div will be red, otherwise it will be green.
Add a runat="server" to your div tag. Then your code behind, you can access that div using:
udpSpecialNotesRepeater.FindControl("div")
on your DataBound event.
It is alot easier to do it in jQuery though. You can just grab id of the div $("#specialNotes") and use the .attr function to change its background.
can use:
Control divControl=e.Item.FindControl("divControl");
Related
This code below works fine, when I click on the LinkButton the select will get fired.
However I have a hover style on the div so that it will change color if the mouse enters. This is confusing for users because when they click on the div nothing happens, only when they click on the LinkButton.
I want the CommandName="Select" to fire when a user clicks on the div. How can I do this?
<ItemTemplate>
<div class="Card">
<h4>
<asp:LinkButton ID="SelectButton" Text='<%# Eval("Name") %>' CommandName="Select" runat="server"/>
</h4>
<asp:Label runat="server" Text='<%# Eval("date") %>' />
<asp:Label runat="server" Text='<%# Eval("location") %>' />
<br />
</div>
</ItemTemplate>
You can use jquery to trigger click event of button on click of the div
$("#Card").click(function(){
$("[id$=SelectButton]").trigger('click');
});
I fixed my issues by placing the LinkButton outside the div. Now the whole div is clickable in the DataList.
<ItemTemplate>
<asp:LinkButton CommandName="Select" runat="server">
<div class="Card">
<h4>
<asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</h4>
<asp:Label runat="server" Text='<%# Eval("date") %>' />
<asp:Label runat="server" Text='<%# Eval("location") %>' />
<br />
</div>
</asp:LinkButton>
</ItemTemplate>
I am using a datalist control in which i have a div and some other asp controls.
I have an edit button in it. If I click on edit button, I want to redirect user to another page on which I have some controls and I want information to be filled up in those controls depending upon the button I have clicked in previous page.
Here is the datalist code:
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" RepeatColumns="2" RepeatDirection="Horizontal" RepeatLayout="Table">
<ItemTemplate>
<div><br /><br /><br />
<div style="background-color:Silver;height: auto; display:block;" >
<div id="threadPostLeftDiv" style="width:auto; border-style:solid; border-width:2px; border-color:Black;border-right-color:White;" >
<asp:Image runat="server" ID="ImagePreview1" style="margin-right:5px; " ImageUrl='<%# Eval("imageurl") %>' Height="100px" Width="100px" BorderStyle="None" />
</div>
<div id="threadPostRightDiv" style=" border-style:solid; border-width:2px;margin-left:-15px; border-color:Black;border-left-color:White;">
<asp:Label ID="txtHeadline1" CssClass="inputprev" style=" font-size:medium; font-weight:bolder;" Text='<%# Eval("subtitle") %>' Enabled="false"
placeholder="Headline" runat="server"></asp:Label>
<br />
<asp:Textbox ID="txtDescription1" CssClass="inputprevdesc1" Text='<%# Eval("descriptions") %>'
Enabled="false" BackColor="Silver" Rows="3" TextMode="MultiLine" placeholder="Description" runat="server"></asp:Textbox><br />
<br />
<asp:Label ID="txtOfferHeadline1" Text='<%# Eval("title") %>' Enabled="false" CssClass="inputprev1"
placeholder="Offer Headline" runat="server"></asp:Label>
<br />
</div>
</div>
<div style=" float: right;margin-right:10px; ">
<button id="btnEdit" class="css3button1" onserverclick="btnEdit_Click" runat="server">Edit</button>
</div>
</div>
</asp:DataList>
Please help me in acheiving my goal,give me the code behind file and what events to triggers.
Thank you.
Well that can be easily achived by doing the following things
bind 'btnEdit' commandargument with the id
<button id="btnEdit" class="css3button1" onserverclick="btnEdit_Click" runat="server" CommandArgument='<%# Eval("ID") %>' >Edit</button>
in btnEdit_Click event do the following
Button bt = (Button)sender;
string recordid = bt.CommandArgument;
Response.Redirect("editrecordno.aspx?recordid="+recordid,true);
in editrecordno.aspx page request the querystring and do the need full.
in Button_Click event you shoul redirect to another page with QueryString paramater
Replace your edit <button> with this code line
<asp:Button ID="Button1" PostBackUrl='DataEdit.aspx?id=<%# Eval("id") %>' runat="server" Text="Edit" />
In DataEdit.aspx put your FormView with DefaultMode=Edit
Then Add new QueryString Parameter in your DataSource
I converted html button to link button and fired onRowCOmmand event and I got my solution.
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Repeater ID="rptrtest" runat="server" OnItemCommand="rptrtest_ItemCommand" OnItemDataBound="rptrtest_ItemDataBound">
<div> <asp:TextBox ID="txtName" runat="server"/> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" > </asp:Button>
</div>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
on button click event i'm sending name to another usercontrol in same page.
i'm having repeater inside update panel this whole thing in one usercontrol my problem when i click submit button inside repeater there is no Asynchronous post back happening please any one help.
Your repeater is missing the ItemTemplate tag
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Repeater ID="rptrtest" runat="server" OnItemCommand="rptrtest_ItemCommand" OnItemDataBound="rptrtest_ItemDataBound">
<ItemTemplate>
<div> <asp:TextBox ID="txtName" runat="server"/> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" > </asp:Button> </div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
References
Repeater (MSDN)
Item Template (MSDN)
I'm trying to get the (div id="imgGallery" runat="server") element from nested Listview to modify the class name programatically.
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemPlaceHolder1" onitemdatabound="ListView1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="VersionId" runat="server" Text='<%# Eval("VersionId") %>' Visible="false" />
<asp:Label ID="MenuContent" runat="server" Text='<%# Eval("ContentText") %>' />
<br />
<asp:ListView ID="ListView2" runat="server" ItemPlaceholderID="itemPlaceHolder2">
<ItemTemplate>
<div id="imgGallery" class="images" runat="server">
<asp:Image ID="Image1" ImageUrl='<%# Eval("ImageUrl") %>' runat="server" ToolTip="Text" />
</div>
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder2"></asp:PlaceHolder>
</LayoutTemplate>
</asp:ListView>
</ItemTemplate>
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</LayoutTemplate>
<ItemSeparatorTemplate>
<br />
<hr />
<br />
</ItemSeparatorTemplate>
on my code behind I tried the code below which doesn't work... and I tried different ways but can't find solution.
ListView listView2 = (ListView)ListView1.FindControl("ListView2");
HtmlGenericControl mydiv = (HtmlGenericControl)listView2.FindControl("imgGallery");
mydiv.Attributes.Clear();
You sholuld work with inner element of databound controls on databinding stage or iterating through row collection (called databind method before)
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<asp:Repeater ID="rpChat" runat="server" OnItemDataBound="rpChat_ItemDataBound" OnItemCommand="rpChat_ItemCommand">
<ItemTemplate>
<div id="divChatWindow" title='<%# Eval("username") %>' runat="server" class="clChatWindow">
<div>
<asp:Label runat="server" Text='<%# Eval("username") %>' class="divHeader"></asp:Label>
<img src="../../Menu/close.jpg" onclick="HideDiv(this)" style="float: right; width: 20px;
height: 20px;" /></div>
<div class="chatText">
<asp:Repeater ID="rpChatMessages" runat="server">
<ItemTemplate>
<asp:Image ID="imageForFriend" runat="server" CssClass="clFriendsImage" ImageUrl='<%# "HttpImageHandler.jpg?username=" + DataBinder.Eval(Container.DataItem,"fromusername").ToString() %>' />
<asp:Label ID="chatMessage" runat="server" Text='<%# Eval("Message") %>'></asp:Label>
<br>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:TextBox ID="txtChatMessage" runat="server" Width="115px"></asp:TextBox>
<asp:LinkButton ID="btnSendChat" runat="server" CommandName="Insert" CommandArgument='<%# Eval("username") %>'>Send</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
I have edited the code... Now what should I do? I have the linkbutton inside contenttemplate. Still a postback is happening
Any postback coming from any control outside an UpdatePanel will trigger a full postback unless you add those events as triggers of your UpdatePanel
In your example,
<asp:LinkButton ID="btnSendChat" runat="server" CommandName="Insert" CommandArgument='<%# Eval("Username2") %>'>Send</asp:LinkButton>
It's outside of the UpdatePanel and since you do not have triggers in your panel that will always cause a full postback
Solutions:
Approach 1
Place the LinkButton inside the UpdatePanel.ContentTemplate
Approach 2
Add triggers to your UpdatePanel
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSendChat" EventName="Click" />
</Triggers>