The gridview control makes databinding of complex objects rather easy. In my scenario a gridview control gets bound to a Customer object which has a few 'flat' properties and one complex property that takes an object of the type Address. The grid displays the data as expected. The problem is that I have found no way to access the values of the Address properties in code behind. For example, setting the DataKeyNames collection to DataKeyNames="Id, Address.Id" results in an error:
DataBinding: System.Data.Entity.DynamicProxies.Customer_95531162E60920A5C3C02043F6564873913B91785C856624301E8B6E89906BF6 does not contain a property with the name Address.Id.
What is the proper way to access the value of the Address.Id field in code behind? Ideally I'd like to do something like:
protected void CustomerDetailsObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (CustomersGridView.SelectedIndex < 0) return;
// Retrieving the Customer's id works:
e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Value);
// Retrieving the Address id doesn work:
e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Values["Address.Id"].ToString());
}
Here's the asp code:
<ContentTemplate>
<asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False" DataSourceID="CustomersObjectDataSource"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged" DataKeyNames="Id,Address.Id" ondatabound="CustomersGridView_DataBound">
<Columns>
<asp:TemplateField HeaderText="Aktion">
<ItemTemplate>
<asp:LinkButton runat="server" ID="SelectCustomerButton" Text="Auswählen" CommandName="Select" /> <br/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kunde" SortExpression="LastName" ItemStyle-VerticalAlign="Top" >
<ItemTemplate>
<asp:Label ID="NumberLabel" runat="server" Text='<%#"GpNr: " + Eval("Number")%>'></asp:Label><br/>
<asp:Label ID="SalutationLabel" runat="server" Text='<%#Eval("Salutation")%>'></asp:Label>
<asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title")%>'></asp:Label>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName")%>'></asp:Label><br/>
<asp:Label ID="NameContactPersonLabel" runat="server" Text='<%#"Kontakt: " + Eval("NameContactPerson")%>'></asp:Label>
</ItemTemplate>
<ItemStyle VerticalAlign="Top" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Adresse" SortExpression="Address.PostalCode" ItemStyle-VerticalAlign="Top" >
<ItemTemplate>
<asp:Label ID="AddressIdLabel" runat="server" Text = '<%#Eval("Address.Id") %>'></asp:Label>
<asp:Label ID="AddressStreetLabel" runat="server" Text='<%#Eval("Address.Street")%>'></asp:Label>
<asp:Label ID="AddressHouseNumberLabel" runat="server" Text='<%#Eval("Address.HouseNumber")%>'></asp:Label>
<asp:Label ID="AddressHouseNumberExtensionLabel" runat="server" Text='<%#Eval("Address.HouseNumberExtension")%>'></asp:Label>
<asp:Label ID="AddressDoorNumberLabel" runat="server" Text='<%#Eval("Address.DoorNumber")%>'></asp:Label><br/>
<asp:Label ID="AddressPostalCodeLabel" runat="server" Text='<%#Eval("Address.PostalCode")%>'></asp:Label>
<asp:Label ID="AddressCityLabel" runat="server" Text='<%#Eval("Address.City")%>'></asp:Label><br/>
<asp:Label ID="AddressCountryLabel" runat="server" Text='<%#Eval("Address.Country")%>'></asp:Label>
</ItemTemplate>
Thanks!!
the reason is Gridview consider datakeynames as the property name, it is using DataBinder.GetPropertyValue() during CreateChildControls() to retrieve data key values, while DataBinder.Eval support multiple level property( as long as it is separated by dot character)
Example:
object item = ...
DataBinder.Eval(item, "Address.Id"); //no problem
DataBinder.GetPropertyValue(item, "Address.Id"); //will throw exception
there are several solution to solve your problem:
derive your custom gridview(override the CreateChildControls()) that it's datakeynames support multi layer expression. You may refer the default implementation by using Reflector. (lots of work)
store the complex property values into viewstate during RowDataBound event (prefered, and also used by me too :) )
Example:
private void CustomersGridView_RowDataBound(object sender, EventArgs args)
{
if(e.Row.RowType = DataControlRowType.DataRow)
{
object id = DataBinder.Eval(e.Row.DataItem, "Id");
//DictionaryInViewState is a variable that will be stored into viewstate later
DictionaryInViewState[id] = DataBinder.Eval(e.Row.DataItem, "Address.Id");
}
}
References:
http://www.telerik.com/forums/problem-with-a-complex-datakeynames-value
Related
I have a gridview that I populate with List<>.
GridView's columns are TextBoxes (as TemplateField).
The list might contain objects from 2 different custom classes whose fields to display are not exactly the same.
For class1, I need to display in GridView:
class1.Name
class1.field1
For class2, I need to display in GridView:
class2.Name
class2.field2
So I can setup the gridview into the aspx so that it displays the class1 items:
<asp:GridView ID="DG_Table" runat="server" style="z-index: 1;
autogeneratecolumns="False"
onrowcommand="DG_Table_RowCommand"
<Columns>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Bind("field1") %>'></asp:TextBox>
</ItemTemplate>
</Columns>
</asp:GridView>
Displaying a List<Class1> into the DataGrid works fine.
Now, if I want to display items of class2 into the datagrid, I will do something like:
DG_Table.DataSource = new List<Class2>;
DG_Table.DataBind();
This will obviously cause this kind of error :
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: DataBinding: 'Class2' does not contain a property with the name 'Field1'.
So I suppose that, before binding to the List, I need to convert my second column into the code behind.
What would be the equivalent of
Text='<%# Bind("field1") %>'
into the code behind?
I expected something like this :
((TextBox)DG_Table.Columns[2]).Text = "";
But this conversion is not allowed.
Thx in advance.
You can try using the anonymous type to bind the data just before the binding the data convert them to the anonymous types and then bind them.
Change your grid view as,
<asp:GridView ID="DG_Table" runat="server" style="z-index: 1;autogeneratecolumns="False" onrowcommand="DG_Table_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name" >
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Eval("FieldValue") %>'></asp:TextBox>
</ItemTemplate>
</Columns>
</asp:GridView>
For binding your first class,
gridView1.DataSource = class1List.Select(x=> new{Name = x.Name,FieldValue = x.Field1});
gridview1.DataBind();
When binding the second class list
gridView1.DataSource = class2List.Select(x=> new{Name = x.Name,FieldValue = x.Field1});
gridview1.DataBind();
Your problem is in the second TextBox it Binds or Evals "field1" which is not a property in Class2.
You can do as same as it was mentioned in the #code of code answer or just Change the Create a Model class for the View to bind it
Class GvItemModel
{
public string Name {get; set;}
public string Field{get; set;}
}
And past it always as dataSource
var dataSource = new List<CvItemModel>();
... //Load class1 or Class2 it depends on your choice
Then
DG_Table.DataSource = dataSource ;
DG_Table.DataBind();
Change your second TextBox dataSource.
<asp:TemplateField HeaderText="field1" >
<ItemTemplate>
<asp:TextBox ID="field1" runat="server" Text='<%# Bind("Field") %>'></asp:TextBox>
</ItemTemplate>
i have a repeater control which contains grids, based on values from database, say for example i have 2 grids inside repeater control, now both the grids contains a column which have up and down buttons, now when user clicks on the button from any grids, how can i check from which grid the button is called.
below is my code where i am filling the grids on RepeaterItemDataBound Event
GridView gvw = e.Item.FindControl("grid") as GridView;
gvw.DataSource = info.GetStories(sectionNames[e.Item.ItemIndex].Trim());
gvw.DataBind();
here section name contains the name of the sections, based on number of sections, i generate the grids.
My Design looks like this:
<asp:Repeater ID="rptGrids" runat="server"
OnItemDataBound="rptGrids_ItemDataBound">
<ItemTemplate>
<asp:GridView ID="grid" runat="server" Width="100%" CellPadding="5" AllowPaging="true" ShowHeader="true" PageSize="10" AutoGenerateColumns="false" OnRowCommand="Stories_RowCommand">
<Columns>
<asp:BoundField DataField="ArticleID" HeaderText="Article ID" ItemStyle-CssClass="center" />
<asp:BoundField DataField="CategoryID" HeaderText="Category ID" ItemStyle-CssClass="center" />
<asp:BoundField DataField="Title" HeaderText = "Article Title" />
<asp:BoundField DataField="PublishDate" DataFormatString="{0:d}" HeaderText="Publish Date" ItemStyle-CssClass="center" />
<asp:TemplateField HeaderText="Select Action" ItemStyle-CssClass="center">
<ItemTemplate>
<asp:ImageButton ID="btnMoveUp" runat="server" ImageUrl="/images/up.gif" CommandArgument="Up" CommandName='<%# Container.DataItemIndex + "," + DataBinder.Eval(Container.DataItem, "StoryType") %>' />
<asp:ImageButton ID="btnMoveDown" runat="server" ImageUrl="/images/dn.gif" CommandArgument="Down" CommandName='<%# Container.DataItemIndex + "," + DataBinder.Eval(Container.DataItem, "StoryType") %>' />
<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/deny.gif" CommandArgument="Delete" OnClientClick="return confirm('Are you sure you want to delete this article?');" CommandName='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:HiddenField ID="hdStoriesSortOrder" runat="server" Value='<%# Eval("SortOrder") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div class="blank"></div>
</ItemTemplate>
</asp:Repeater>
this is my gridviews row_command event
protected void Stories_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandName.Split(',')[0]);
string section = e.CommandName.Split(',')[1].Trim().ToString();
string command = e.CommandArgument.ToString();
if (command.ToLower() == "up")
{
GridView grd = rptGrids.Items[1].FindControl("grid") as GridView; // If i specify the index here, i gets proper grid, but how to recognize at runtime.
Response.Write(grd.Rows.Count);
}
else if (command.ToLower() == "down")
{
}
}
can anyone tell me how can i get from which grid up/down button has been clicked.
You can use command argument to pass required value.
Here is sample of using imagebutton in similar way:
<asp:ImageButton ID="btnView" runat="server" ToolTip="<% $resources:AppResource,Edit %>"
SkinID="EditPage" CommandName="myCommand" CommandArgument='<%# Eval("CustomerId") %>'
PostBackUrl='<%# "~/AdminPages/Customer.aspx?id=" + Eval("CustomerId").ToString() %>' />
Take notice on CommandArgument property.You can set it with value that indicates specific gridview inside repeater.
And here is how to check the value:
protected void EntityGridViewContacts_RowCommand(object sender, GridViewCommandEventArgs e)
{
//here you can check for command name...
switch (e.CommandName)
{
case "myCommand":
//here you access command argument...
int customerId = Convert.ToInt32(e.CommandArgument.ToString());
break;
}
//here is how you access source gridview...
GridView gridView = (GridView)sender;
string controlId = gridView.ID;
}
You can also set CommandArgument using this approach:
CommandArgument='<%# GetMySpecialValue() %>'
Then you should declare function on page side something like this:
public string GetMySpecialValue()
{
return "some value";
}
I have a GridView that is made up of two database fields populated via a stored procedure and then three fields for user input (two checkbox controls and one textbox) when I click on the save button I can get the information from the three controls but nothing from the two that were populated via the database. How can I get the first two fields?
<asp:GridView ID="gvA1" runat="server" AutoGenerateColumns="false" DataKeyNames="CodeNo" AutoGenerateSelectButton="false" EnablePersistedSelection="True" Visible="false">
<Columns>
<asp:TemplateField Visible="false" >
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeNo")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Wrap="true" ItemStyle-Width="400px" HeaderText="Violation">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbPool" runat="server" Text="Pool:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="A1Accordion_cbSpa" runat="server" Text="Spa:" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Additional Comments" Visible="true">
<ItemTemplate>
<asp:TextBox ID="A1Accordion_tb" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
protected void SaveAndCollapseA1(object sender, EventArgs e)
{
//good stuff
CheckBox myCheckBox_1 = gvA1.Rows[0].FindControl("A1Accordion_cbPool") as CheckBox;
CheckBox myCheckBox_2 = gvA1.Rows[0].FindControl("A1Accordion_cbSpa") as CheckBox;
TextBox myTextBox = gvA1.Rows[0].FindControl("A1Accordion_tb") as TextBox;
//not so good stuff
String myString1 = gvA1.Rows[0].Cells[0].ToString();
String myString2 = gvA1.Rows[0].Cells[1].ToString();
}
I figured it out but I haven't been hear long enough to post the solution via the links but if you change the columns as so:
<asp:label ID="lblA1CodeNo" runat="server" text='<%# Eval("CodeNo") %>'></asp:label>
then the values are available...
Try:
<%# Bind("CodeViolationCited") %>
Instead of
<%#DataBinder.Eval(Container.DataItem, "CodeViolationCited") %>
Eval is one way, bind is two-way.
See Microsoft's documentation for data-binding expressions
I have a field in a details view shown below
<asp:BoundField DataField="DTMON_F" HeaderText="Monday Hours From: " InsertVisible="False"
ReadOnly="True" SortExpression="HOURS" Visible="false" />
<asp:TemplateField HeaderText="Monday Hours From: " SortExpression="HOURS">
<EditItemTemplate>
<uc1:TimePicker ID="tpMondayHours" runat="server"/>
</EditItemTemplate>
<InsertItemTemplate>
<%-- <uc1:TimePicker runat="server" ID="tpMondayHours" />--%>
<asp:TextBox ID="txtMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMondayHours" runat="server" Text='<%# Bind("DTMON_F") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Before "DTMON_F" is binded to the view I want to cut the string that is returned...Where and how might I do this?
You can implement the OnDataBinding event for each control instead of doing the bind inline. This will give you the ability to do whatever you like with the data before assigning it to the control.
Example using your Label. The same could be applied to the TextBox:
<asp:Label ID="lblMondayHours" runat="server"
OnDataBinding="lblMondayHours_DataBinding"></asp:Label>
protected void lblMondayHours_DataBinding(object sender, System.EventArgs e)
{
Label lbl = (Label)(sender);
string yourValue = (int)(Eval("DTMON_F"));
// *** Do whatever you want with the value now
lbl.Text = yourValue;
}
In my ASP.NET application, I have a GridView. For a particular field in this GridView, I've added an EditItemTemplate with a DropDownList. However, if the value of the field is "X", then I want to just display a label instead of the DropDownList. So how can I programatically check the field value, then decide which control to display?
Here is my EditItemTemplate:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>
</EditItemTemplate>
If the value of Level_ID is "X", then I want to use:
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>
instead of the DropDownList.
I tried embedding an if statement before the DropDownList to check Eval("Level_ID"), but that doesn't seem to work. Any thoughts?
Try this:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'
Visible='<%# Eval("Level_ID") != "X" %>'>
</asp:DropDownList>
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'
Visible='<%# Eval("Level_ID") == "X" %>'></asp:Label>
</EditItemTemplate>
Here is something that will work for ASP.Net.
You could create an RowDataBound event and hide the label or the DropDownList
<asp:GridView id="thingsGrid" runat="server" OnRowDataBound="thingsGrid_RowDataBound"
... >
...
and in your code behind:
protected void thingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var boundData = e.Row.DataItem;
...
if (boundDataMeetsCondition)
{
e.Row.Cells[4].FindControl("editThingDropDownList").Visible = false;
e.Row.Cells[4].FindControl("editThingLabel").Visible = true;//*
}
else
{
...
}
}
}
*note this is less than ideal because it hard codes the cell index, and the ID of the controls is a string that wont be checked until runtime. There are much more elegant ways to solve this problem in asp.net mvc.
the OnRowDataBound is a sledge hammer that will give you full access to your grid, page methods, and your entire application. In a very simple scenario you could also do it inline without involving the codebehind.
<asp:Label ID="Label1" runat="server" Visible='<%# Convert.ToBoolean(Eval("BooleanPropertyInData"))%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>
or
<asp:Label ID="Label1" runat="server" Visible='<%# Eval("PropertyInData").ToString()=="specialValue"%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>
in the first inline approach, your data source has to expose such a property, and in the second you are hard coding your specialValue business logic into your presentation, which is also ugly and will lead to problems with maintainability.