I've got something like that
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<label id="test" runat="server">dw</label>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Can someone tell me, how should I change label text, using C# code?
Main problem for me is- how to get access to the nested control (label, literal) from c# code?
EDITED:
<SelectedItemTemplate>
<asp:HiddenField ID="NumberEdit" runat="server"
Value='<%# Bind("numbers") %>' />
<label for="NameEdit">Name:</label>
<asp:TextBox ID="NameEdit" Width="160px" runat="server" AutoPostBack="true" OnTextChanged="NameEdit_TextChanged"
Text='<%# Bind("Name") %>' />
<br />
<label for="ShortcutEdit">Shortcut:</label>
<asp:TextBox ID="ShortcutEdit" Width="80px" runat="server"
Text='<%# Bind("Shortcut") %>' />
<br />
and I would like to generate automatically Shortcut text when user will change Name (Shortcut = 2 first letters from NameEdit)? Can you explain me, how should I do it? –
You would want to have an ItemDataBound event handler to get access to the controls for that particular item in your listview. The example on the page I linked should help you out.
First thing is that you need a data source binded with this ListView control, for example SqlDataSource, or any other allowed type you need:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [Title], [id] FROM [Articles]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1" >
// rest of the code
</asp:ListView>
Second thing is that controls from LayoutTemplate template will be rendered only if there is any data to show. So if you have datasource, but it is empty, this tamplate will not be applied. But you can use EmptyDataTemplate to display info when there is nothing from the datasource to display.
And then, when you already have datasource defined and binded to your ListView and there is data that will be displayed, the LayoutTemplate will be rendered. And then you can use FindControl method of the ListView. As for example of getting this literal:
Literal l = (Literal)lv.FindControl("litControlTitle");
It's returning null for you because you have no data to display, so controls are not rendered at all.
((Label)ListView1.FindControl("test")).Text = "Hello!";
Related
I have the following situation:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ToolTip='<%# Eval("ToolTip") %>' ImageUrl='<%# Eval("ImageUrl") %>'/>
<asp:Label ID="labValue" Text='<%# Eval("Text") %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
The Repeater contains ImageButton and Label objects. If I only use one object, I can add them from codebehind through the binding:
Repeater.DataSource = imageList;
Repeater.DataBind();
OR
Repeater.DataSource = labelList;
Repeater.DataBind();
How can I add both to one Repeater?
If you need them in one Repeater, they -are- related in that "view" (loosely used term) of the data. So why not make that view into an explicit object that contains both the image and the label? Either named, or anonymous:
return Enumerable.Zip(imageList, labellist, (image, label) => new {image, label})
Alternatively, you can just pass the images and query for the labels by index:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:Label ID="labValue" Text='<%# GetLabelText(Container.ItemIndex) %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
This requires a public GetLabelText(index) method and probably extra state in your code behind to make the ImageList available to that method. Not very nice, but it gets the job done.
<asp:datalist ID="Datalist1" runat="server"
Width="500px" >
<ItemTemplate>
<asp:Button ID="btnviewfullprofile" runat="server" Text="View Full Profile" ToolTip="Click for Full Profile of User" CommandArgument='<%#Eval("Uid")%>' CommandName="fullprofile" />
<asp:Button ID="sendinterest" runat="server" Text="Send Interest" CommandArgument='<%#Eval("Uid")%>' CommandName="sendinterest" />
<asp:Label ID="lblstatus" runat="server" Visible="False" ></asp:Label>
</ItemTemplate>
</asp:datalist>
text of label will change according to the value of status stored in database.
code for button
if (e.CommandName == "fullprofile")
{
int Id = int.Parse(e.CommandArgument.ToString());
Response.Redirect("~/FullProfile.aspx?Id=" + Id);
`enter code here` }
but what should i write for label so that text of label should change itself based on value of status stored in database
If I understand you correctly, you need to change the HTML to something like:
<asp:Label ID="lblstatus" runat="server" Visible="False"
Text='<%# Eval("DatabaseField") %>' />
You are already using this for the CommandArgument of the button. Obviously you need to replace 'DatabaseField' with the name of the field that you want to show as text. ASP.net will fill the Text attribute with the correct value from your datasource.
as you are binding commandargument of the button from database, same way you can bind the text property of the label
<asp:Label ID="lblstatus" runat="server" Visible="False" Text= '<%#Eval("textfield")%>' ></asp:Label>
If you need more advance control then you need to set them when items are getting bound see here fore details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx
I have a gridview which has a few rows (each with a unique rowId), and each line has a FileUpload control, now everything works okay with FileUpload.
(my uploaded file database image can be seen below)
I have the download button, which also works okay, however I want to make this button invisible if no file exists for the corresponding row.
Nothing proper comes to my mind.
My button and FileUpload control:
<asp:TemplateField HeaderText="BatchList">
<EditItemTemplate>
<asp:ImageButton ID="ibt_Download" runat="server" src="Images/Download.png" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' ></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadBatchList">
<HeaderTemplate>
<asp:Label ID="lbl_Header" ToolTip="Upload Batch List" runat="server" Text="UBL"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu_UploadBatchList" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click" />
</EditItemTemplate>
</asp:TemplateField>
This is how it looks on my gridView
When gridview is first created the green dots must not be visible if a file has been uploaded before.
My file database:
You can check some property of the data item (DocName in your case) if it contains a value (it might not work when copy-pasted, I'm a little bit improvising):
<asp:Button ID="btn_Upload" runat="server"
Text="Upload"
Visible='<% DataBinder.Eval(Container.DataItem, "DocName") == null %>
OnClick="btn_Upload_Click" />
Or you can create a function that will evaluate the visibility. See Mastering ASP.NET DataBinding for more.
Is something similiar to the following pseduocode possible on server side controls?
<asp:Label runat="server" ID="lbl" Text=<%=DateTime.Now.ToString(); %> />
I.e. assigning attributes.
This will work for databound controls, e.g.
<asp:Label runat="server" ID="lbl" Text="<%# DateTime.Now.ToString() %>" />
Then in your code behind, you'll need to call lbl.DataBind().
Try this:
<asp:Label runat="server" ID="lbl" Text='<%=DateTime.Now.ToString()%>' />
I am a new coder trying to experiment with jquery for my first time. I'm trying to setup a simple datalist that might be used to display comments for an item. I want a clickable link (per datalist row) to drop down a panel (per datalist row) that has comment text. so the user looks at row 1, clicks it's link to read comments, and the comments panel drops down. they scroll down and do the same for the next item.
so far i have the below code as a small test page, but it's not working. nothing happens basically. I'm hoping someone can help me out because i'm very new and just teaching myself this stuff from what I see in tutorial videos and such. I tried the clientID thing because it seems i need that to deal with the auto-generated ID's .NET will assign panels as it's rendered, but i'm not sure if i'm doing it right.
greatly appreciate your time and effort.
head section
<script src="jquery-1.4.4.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('Panel1text').hide();
});
$("#<%=HyperLink1.ClientID%>").click(function() {
$("<%=Panel1text.ClientID%>").show();
});
</script>
body section
<asp:DataList ID="DataList1" runat="server" DataKeyField="cid"
DataSourceID="SqlDataSource1" Width="645px">
<ItemTemplate>
cid:
<asp:Label ID="cidLabel" runat="server" Text='<%# Eval("cid") %>' />
<br />
cuser:
<asp:Label ID="cuserLabel" runat="server" Text='<%# Eval("cuser") %>' />
<br />
blogid:
<asp:Label ID="blogidLabel" runat="server" Text='<%# Eval("blogid") %>' />
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server">show text</asp:HyperLink>
<br />
<asp:Panel ID="Panel1text" runat="server">
<asp:Label ID="textLabel" runat="server" Text='<%# Eval("text") %>' />
</asp:Panel>
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [ocomments]"></asp:SqlDataSource>
It looks to me like you are going to have multiple elements with the id of 'HyperLink1' and 'Panel1text'. I would recommend using classes instead. Add a "class='link'" to the link element and a "class='panel'" to the panel element. Use the following CSS to initially hide the panels:
.panel { display: none; }
Then use the following jQuery to show the element:
$(document).ready(function(){
$(".link").click(function(evt){
evt.preventDefault(); // prevents the click from leaving the page
$(this).next().show(); // show the panel
});
});
You may need to fiddle with the '.next().show()' selector a bit. Not certain how ASP.NET is going to render out the elements.
Bob