I'm trying to separate my presentation and logic as much as I can, but there's one problem.
How can i set the text-property to a dynamic value from my design file? I want to do like this:
<asp:Textbox id="txtUrl" runat="server" Text="<%= user.URL %>" />
But this will fail. Am i forced to do this from code behind?
<asp:Textbox id="txtUrl" runat="server" Text="<%# user.URL %>" />
It's all about the #. but it won't get set till txtUrl.DataBind() or something higher in the object heirarchy (like the Page) calls DataBind().
How about this :
<input type="text"
id="txtUrl" name="txtUrl" runat="server"
value='<%= user.URL %>' />
You can use binding instead of evaluation.
This code binds a text box's Text property to a user's Url property returned by MyData.GetLoggedInUser(). This allows for 2-way binding.
<asp:FormView ID="UserView" runat="server" DataSourceID="LoggedInUser">
<ItemTemplate>
<asp:TextBox ID="tb"
runat="server"
Text='<%# Bind("Url") %>'></asp:TextBox>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="LoggedInUser"
runat="server"
SelectMethod="GetLoggedInUser"
TypeName="MyData">
</asp:ObjectDataSource>
Related
I want to add child object to a TextBox, but this code gives an error
How can I achieve this?
<asp:TextBox ID="TextBox2" runat="server">
<asp:Label runat="server" Text="Label"></asp:Label>
</asp:TextBox>`
TextBox and Label are two different controls. You cant nest a Label inside a text Box.
Eg:
<asp:Label runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox>
What do you want to do here?
As there are some of the ASP.NET controls like Linkbutton which allows you to hold HTML controls inside them
<asp:LinkButton ID="lnkDelete" runat="server" CssClass="remove">
<span><font color="#fb4202">x</font> Remove</span>
</asp:LinkButton>
but it is not possible to hold a asp:Label in a asp:Textbox control
What you are trying to do is impossible. While WPF has a framework that enables this kind of things, HTML and thus ASP.NET does not.
If you are trying to set the text, try this:
<asp:TextBox ID="TextBox2" runat="server" Text="abc" />
Else, just put them side by side:
<asp:Label runat="server" Text="Label" />
<asp:TextBox ID="TextBox2" runat="server" />
Textbox is a Editable Control and Label is non-Editable Control, so you can't put Label inside TextBox. You can Put Label Just Before The Textbox as :
<asp:Label ID="Label1" runat="server" Text="Enter Text"><asp:Label>
<asp:TextBox ID="txtValue" runat="server"/>
I am using strongly typed with a Gridview,
This will work (1 BindItem)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" />
</ItemTemplate>
This will work (1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
This will not work (1 BindItem and 1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
CS0103: The name 'Item' does not exist in the current context
It seems once we use BindItem in 1 place, we can't use binding in with this control again.
I know I can wrap the textbox with a panel to control the "Enabled" property as a fix
Replace Item.Price==5 with BindItem.Price==5.
I have two Formviews on the same page. I can use FindControl on the first without issue, but it always fails on the second one.
I am using the ItemTemplate for both, both default to ReadOnly, both are bound to SQLDataSources (different ones) but I cannot for the life of me work out why FindControl works for one and not the other.
I have removed a lot of the plain text from the code below.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Apps] WHERE ([AppID] = #AppID)" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="AppID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1>
<asp:Label ID="AppNameLabel" runat="server" Text='<%# Bind("AppName") %>' />
<asp:Label ID="VersionLabel" runat="server" Text='<%# Bind("Version") %>' /> for
<asp:Label ID="OSLabel" runat="server" Text='<%# Bind("OS") %>' />
</h1>
<p>Text here</p>
<p><asp:TextBox ID="LicenseTextBox" runat="server"
Text='<%# Eval("License")%>'
TextMode="MultiLine" Width="800px" Rows="25" ReadOnly="True"></asp:TextBox></p>
<asp:HiddenField ID="AppLocation" runat="server" ViewStateMode="Inherit" Value='<%# Bind("AppLocation") %>'/>
</ItemTemplate>
</asp:FormView>
<p><asp:Literal ID="SizeLiteral" runat="server"></asp:Literal></p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:SoftSaleDBConnectionString %>"
SelectCommand="SELECT * FROM [Installations] WHERE (([AppID] = #AppID) AND ([Username] = #Username))" >
<SelectParameters>
<asp:FormParameter FormField="AppID" Name="AppID" Type="Int32" />
<asp:FormParameter FormField="Username" Name="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:FormView ID="DLFormView" runat="server" DataSourceID="SqlDataSource4"
DataKeyNames="AppID,Username">
<ItemTemplate>
<p> <asp:Label ID="DLAppNameLabel" runat="server" />
<br />
<br />
<asp:Label ID="NumberOfInstallations" runat="server" Text='<%# Bind("Installations") %>'></asp:Label>
<br />
<asp:HiddenField ID="TotalInstallations" runat="server" />
Number of New Installations:
<asp:DropDownList ID="NumberOfNewInstallations" runat="server">
</asp:DropDownList>
<br />
<asp:Label ID="TotalNumberOfInstallations" runat="server" Text="Label"></asp:Label>
</p>
</ItemTemplate>
</asp:FormView>
And the FindControl coding is as follows...
TextBox LicTextBox = (TextBox)FormView1.FindControl("LicenseTextBox");
HiddenField AppLocCode = (HiddenField)FormView1.FindControl("AppLocation");
Label AppNameCode = (Label)FormView1.FindControl("AppNameLabel");
These always work...
Label DLAppNameCode = (Label)DLFormView.FindControl("DLAppNameLabel");
This always returns null. I've read a million bosts about the controls not being rendered until databinding has completed, but surely if the two FOrmViews are set up the same way, the result should be the same.
Any help would be much apreciated :)
Matt :)
I'd like to plead stupidity and but also provide some background to how I've used what I've learnt from my stupidity.
My SqlDataSource for DLFormView was returning records, of course I then used slightly different values by accident and so no records were returned. I only figured this out by making the ItemTemplate section of the form view only contain unbound (basically plain text) data. As nothing displayed, this pointed towards the page using a different tempalte. When I put a test line in the EmptyDataTemplate, it displayed, confirming there was nothing returned by the SqlDataSource. Perhaps a School boy error not putting something in this template during development.
As it happens I need to use the EmptyDataTemplate anyway, I just hadn't got to this point. As only one template is rendered at a time, I have been able to use the same ID names in both templates. This means there is no need to test for which template is being used. (although empty template is used when DataItemsCount = 0, otherwise you can test for CurrentMode for the remaining templates.)
So a big fail on my part, but I hope people can learn from my mistake.
Matt :)
I have been trying to get these running looking at lots of different samples but with no luck.
To me it seems it's ok but what am I missing here?
<asp:Label ID="Label1" runat="server" Text="<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>" Width="100px" visible="true"></asp:Label>
Thanks
"With no luck" is not a descriptive error.
<%# is for databinding expression only. So have you called Page.DataBind() or at least Label1.DataBind() in codebehind?
You could also try
Text='<%= String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>'
Apart from that, why not doing such things in codebehind only, so you don't have issues like this?
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>' Width="100px" visible="true"></asp:Label>
you shouldn't use Text="something". you should use Text='something'
I am helping my daughter build an asp.net website that has an Access database. We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:
<a id="ContentPlaceHolder1_DataList1_lnk_6" href="#http://www.washingtonfaire.com/#" target="_blank">Washington Midsummer Renaissance Faire</a>
But when I click on the link, it tries to navigate to "http://localhost:1852/BOOMPiratesB/Raids.aspx#http://www.washingtonfaire.com/#"
What are we doing wrong?
Here is our code.
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemTemplate>
<asp:HyperLink id="lnk" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank" Text='<%# DataBinder.Eval(Container.DataItem, "VenueName")%>'>
</asp:HyperLink>
<br />
<asp:Label ID="DateTextLabel" runat="server" Text='<%# Eval("DateText") %>' />
<br />
<asp:Label ID="CityStateLabel" runat="server" Text='<%# Eval("CityState") %>' />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/my.mdb"
SelectCommand="SELECT [VenueName], [Link], [DateText], [Season], [DateStart], [CityState] FROM [qpgRaid]">
</asp:AccessDataSource>
I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.
It's very puzzling. And insight would be most appreciated.
Have a look at this question
I guess you are using an access hyperlink column to store the link. If you convert it to a text column it should start working fine, or you may have to strip the '#'s manually.