asp.net FormView Data Binding with a Collection of Data - asp.net

I have a collection of records, each record has an ID and a description.
Now in my formview I have 8 textboxes and I want each text box to hold description
of each record.
So if I do this
Text='<%# Eval("Record[0].Description") %>' />
This gives an error, any other way to do it?
Also can I do it in the markup, or do I need to do it in code behind, under databound method for the formview?
Thanks..

FormView is not meant for showing List of Data.
If you have a List of Data, then you should use GridView or ListView.
Bind your FormView with a datasource having single record and then directly Eval the fields of the datasource.
i.e. do this:
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSourceId">
<ItemTemplate>
<asp:TextBox id="txtDescription"
Text='<%# Eval("Description") %>' />
<asp:TextBox id="txtName"
Text='<%# Eval("Name") %>' />
..
</ItemTemplate>
</asp:FormView>
so basically, your FormView should contain different DataField and it should be bound to a DataSource having just one Item.

You could use a repeater inside:
<asp:repeater ID="rep" runat="server" DataSource='<%# Eval("Record") &>'>
<ItemTemplate>
<asp:textbox id="txt" runat="server" Text='<%# Eval("Description") &>' />
</ItemTemplate>
</asp:repeater>
In the repeater you will bind to your outer datasource, inside the repeater your datacontext is the record

Related

How to set ItemTemplate text value to combobox DataTextField instead of DataValueField

I have a telerik radgrid with a GridTemplateColumn as seen below in a C# ASP.NET 4.0 webform.
As you can see, in the EditItemTemplate I am using a RadComboBox with an id number for DataValueField and a human readable text for DataTextField.
My question is, how can I change the Text in my ItemTemplate to show the human readable value instead of the Id? The value Alias1 comes from the grid datasource and it is the Id.
Thank you for any help you can provide!
<telerik:GridTemplateColumn UniqueName="Alias1" Display="true" DataField="Alias1" HeaderText="Alias1" SortExpression="Alias1">
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Bind("Alias1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox16" runat="server" Skin="Outlook" Height="150" DataSourceID="SqlDataSourceAliasOptions" DataTextField="aliasText" DataValueField="aliasid" SelectedValue='<%#Bind("Alias1") %>'>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
You need to change ItemTemplate binding like this:
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Eval("aliasText") %>'></asp:Label>
</ItemTemplate>
Of course your binded entity must have "aliasText" property. If you are binding something like DataTable make sure that contains "aliasText" column.

DropDownList TextValue is 1st in List

I'm a newbie when it comes to asp.net, so grateful for any help.
I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.
The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:
DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;
And here's the code form the aspx page
<asp:TemplateField HeaderText="Preferred Location"
SortExpression="PreferredLocation">
<ItemTemplate>
<asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM StaticData" />
You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.

DetailsView converts TextBoxes empty string to null

I have a basic ASP.NET Web Forms application.
I give the user the possibility to create the records by using a Web Form and to update them by using a DetailsView.
The web form stores the empty field on the TextBoxes as *empty strin*g in the DB correctly.
The DetailsView instead stores them as null fields in the DB.
Moreover in the DetailsView_ItemUpdated event if I check the e.newvalues and e.oldvalues arguments the corresponding fields are null as well (throwing an exception) that means the data are already sent as null to the server upon submit.
In the DetailsView I use templates and I set all Update parameters in the SQLDataSource as ConvertEmptyStringToNull="false".
SQLDataSource parameter:
<asp:Parameter Name="SHELF" Type="String" ConvertEmptyStringToNull="false"/>
DetailsView
<asp:DetailsView runat="server"
ID="GvProductDetail" AutoGenerateRows="False" DataKeyNames="rowid"
DataSourceID="ProductDetailData"
OnItemUpdated="gvProductDetail_ItemUpdated">
<asp:TemplateField>
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate>
<%# Eval("Shelf") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Is that possible that Bind converts the empty values to null? I thought it was enough to set the UpdateParameters rule not to convert empty string to null but apprently there is some other surprise. anybody might help?
I solved it by placing ConvertEmptyStringToNull="false" to every <asp:TemplateField> containing <asp:TextBox>:
<asp:TemplateField ConvertEmptyStringToNull="false">
<HeaderTemplate>Stock Shelf </HeaderTemplate>
<ItemTemplate> <%# Eval("Shelf") %></ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShelf" Text='<%# Bind("Shelf") %>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

Label control in ItemTemplate tag in TemplateField tag in Gridview control

I am using a label control in ItemTemplate tag in TemplateField tag in Gridview Control to show a field of data of my database .
I want if a length of data is higher than 100 charachter the label control doens't show all of data.
<ItemTemplate>
<asp:Label ID="label4" runat="server" Text='<%# Bind("Description") %>' Width="200px" Font-Names="B Nazanin" EnableTheming="false" Height="24px" BorderStyle="Dashed"></asp:Label>
</ItemTemplate>
What should I do ?
This is the simplest approach:
Text='<%# Eval("Description").ToString().Substring(0, 100) %>'

Gridview : Hyperlink and description in the same column cell

Apologies for the newbie question. My client wishes me to make a small change to the gridview on his http://www.flogitdonegal.com/SearchPage.aspx page.
Note the way the Title column is a hyperlink to view more information. This comes from a 'BriefDescription' field in the database.
How can I add 250 chars from the 'FullDescription' underneath the Title in the same cell, but I dont want it be a hyperlink.
Essentially it will be 2 fields coming into the same column.
Thanks in advance for all help.
John
If this is using a GridView you are most likely using a TemplateField as it is to display the HyperLink.
Within the ItemTemplate of the TemplateField you can specify an additional Label underneath using something as follows:
<asp:Label runat="server" id="FullDescLabel" Text='<%# DataBinder.Eval(Container.DataItem, "FullDescription") %>' />
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwCompounds" runat="server" DataSourceID="objItemsFromYourDB">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Title">
<asp:HyperLink runat="server" ID="Hperlink1" NavigateUrl='<%# Eval("BriefDescriptionUrl") %>' Text='<%# Eval("BriefDescription") %>' />
<br />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("FullDescription") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>

Resources