I am trying to bind a BoundField of a DetailsView control to a child object of the datasource, I can't seem to find the right syntax.
Here's some sample code;
<asp:BoundField DataField="Address.Postcode" HeaderText="PostCode" />
This code doesn't work but you get the idea.
Using this code I get the exception;
A field or property with the name 'Address.Postcode' was not found on the selected data source.
Thanks.
you can use item template ....
take a look at this link
<asp:TemplateField HeaderText=”First Name” SortExpression=”Person.FirstName”>
<ItemTemplate>
<asp:Label ID=”FirstNameLabel” runat=”server” Text=’<%# Eval(“Person.FirstName”) %>’></asp:Label>
</ItemTemplate>
Related
I am trying to get values of string that I keep inside GridView.
When I use regular BoundFields, things work get. I get whatever is needed with:
string my_value = myGrid.Rows [rowIndex].Cells[1].Text;
However, one grid needs to have hyperlinked entries in one of the columns. I did:
<asp:BoundField DataField="domainName"
HeaderText="Domain"
SortExpression="domainName"
HtmlEncode="false"
DataFormatString="<a href=DomainConfiguration.aspx?suffix={0}>{0}</a>"
My ASPX page shows the correctly formed hyperlinks. However, if I retrieve Text for the cell, it returns "<\a href=DomainConfiguration.aspx?suffix=example.com>example.com</a>" [without the two extra slashes], instead of "example.com"
What do I need to do to get GridView working the way I want? [Yes, I would rather use GridView and not another control.]
Thank you.
Instead of asp:BoundField to show hype link it is better use asp:HyperLinkField like:
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="domainName" HeaderText="domainName" DataNavigateUrlFormatString="DomainConfiguration.aspx?suffix={0}" DataTextField="domainName" />
</Columns>
and to get string value of that column you should try this:
string my_value= ((HyperLink)myGrid.Rows[rowIndex].Cells[1].Controls[0]).Text;
instead your asp:Bounfield use
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="editLink" runat="server" onclick='<%#Eval("EditLink") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
List<MasterBook> listOfBooks = new List<MasterBook>();
after i put the masterbook objects which by the way have 3 fields( name,id and active ) into the list
GridView1.DataSource = listOfBooks;
GridView1.DataBind();
in the web form
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<asp:Label ID="BookNameText" runat="server"
Text="<%#Container.DataItem%>">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
the problem is that i want to display the bookmaster's name in the gridview column
this code print bookmaster in the column how can i make it type the name
Change:
<%#Container.DataItem%>
To:
<%#Eval("name")%>
Cast it to MasterBook:
Text="<%# ((MasterBook) Container.DataItem).Name %>">
The quick, dirty hack workaround is to overload the ToString() method of your MasterBookclass in order to return that name. Commit seppuku afterwards if you do it.
The elegant and graceful way is to make an object datasource. That way you can bind the grid view's columns to the objects' properties. In the case of a DataList or other templated data controls, you can bind your objects' properties to the data control's child controls in their templates.
The middle path is in hutchonoid's answer: evaluate a property of the object, not the object itself.
Use this sintax:
<asp:Label ID="BookNameText" runat="server"
Text="<%# ((MasterBook)Container.DataItem).Name %>">
In Insert Mode I want to show one kind of Template and Edit Mode I want to show another Kind of Template in Devexpress Control in C#.NET
This can be implemented using the following approach:
You should define the EditFormTemplate so that it contains a different set of editors for the Insert and Edit functionality. Handle the HtmlRowCreated event to hide non required editors based on the ASPxGridView's IsNewRowEditing property value.
Check out the sample project on this issue which demonstrates the solution based on user controls and binding expressions. In this solution, the EditForm template contains two user controls with the Visible property bound to the ASPxGridView.IsNewRowEditing property:
[HTML]
<uc1:Edit id="Edit1" runat="server" Visible="<%# !Container.Grid.IsNewRowEditing %>"></uc1:Edit>
<uc2:Insert id="Insert1" runat="server" Visible="<%# Container.Grid.IsNewRowEditing %>"></uc2:Insert>
Btw, this issue might also be helpful. If you need more help, please contact the DevExpress support team here.
Use an ASP template field and individual item templates inside your gridview
<asp:TemplateField id="test" runat="server">
<ItemTemplate>
<ItemTemplate>
test
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text="test" runat="server"/>
</EditItemTemplate>
</ItemTemplate>
</asp:TemplateField>
I did the above from memory. Heres a link to an example i found as well:
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx
(first of all, it seems this is a subject that is discussed many times before, but I can't find a proper answer for my case)
I have a asp.net FormView with a DropDownList for the selection of a month. The FormView is data bound to an ObjectDataSource.
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Bind("OrderDate.Month") %>' Width="100" runat="server" />
I like to bind the selected value to the nested property 'Month' of 'OrderDate' as shown above. The property OrderDate is of type DateTime. The error I'm getting while binding to a nested property is:
A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind.
What is the best solution to be able to bind to a nested propety?
Thanks in advance.
Are you retrieving data from DataSourceID="MonthsListDataSource" and tryingo to bind it to another DataBase field (SelectedValue='<%# Eval("OrderDate.Month") %>') ?
In my application a do this like this:
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("MonthsListDataSource.Month") %>' Width="100" runat="server" />
And when Updating a retrieve de DropDownList with find control, get its selected value, associate it to other table (OrderDate.Month) and save it.
Sorry my answer, but i dont know if a undertand it.
What about using Eval keyword
<asp:DropDownList ID="MonthsList" DataSourceID="MonthsListDataSource" DataTextField="Value" DataValueField="Key" SelectedValue='<%# Eval("OrderDate.Month") %>' Width="100" runat="server" />
I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.
I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.
How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.
Please Help
Thank you.
Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :
<asp:gridview id="GridView1"
autogeneratecolumns="false"
runat="server">
<asp:HyperLinkField DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
DataTextField="UserName" />
</asp:gridview>
Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.
If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField
Let me know if I'm off the mark with this
here's some code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Whatever" />
<asp:TemplateField>
<ItemTemplate>
<a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>