Accessing Gridview row data inside fields - asp.net

I have a Gridview bound to a linqdatasource. The gridview has a FK. i want to display a name/text field instead of the key field for this column.
I have created a method in my ASP.NET page that basically GetLookupForKey that returns the string when provided the key. However, I don't know how to send the column data for the specific row in the data declaration.
This should make it clear:
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="RoleName" runat="server" Text='<%#
GetRoleName(* I need to send in the RoleId Here*) %>' />
RoleId is both a boundfield and a DataKeyName. How can I send the RoleId to my method? Additionally, how can I achieve this without using any codebehind?
Thanks

I can see two ways right now, maybe there are even more.
1) With code behind
<asp:Literal ID="RoleName" runat="server" Text='<%# GetRoleName(Eval("RoleId")) %>'/>
This one will require a protected method GetRoleName(object roleId) in the page's code behind class.
2) Without code behind
I assume that objects Role and whatever object is referencing it are both declared in the Linq context. If so, Linq can (and even does it by default behavour) generate properties for referenced objects. That is, when you have a table with FK to Role, the corresponding object will have both RoleID and Role properties. Therefore everything can be accomplished declaratively:
<asp:Literal ID="RoleName" runat="server" Text='<%# Eval("Role.Name") %>'/>

Related

ASP.Net - GridView server-side variable for each row?

I want to add a hidden key field in a GridView that is not displayed to the user. I tried doing something like:
<asp:GridView ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="secretkey" runat="server" Value='<%# Eval("secretkey") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
But the hidden field still ends up visible as plain text in the html source. Is it possible to do something like this using server state?
Using datakeynames you can associate even multiple key to your rows. Check the following link http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
The Session would be the best place to store small data that you need across PostBacks but can't sent to the client.
You could put a Dictionary indexed by row identifier into session and use that to fetch your secret key.
Note that sessions are maintained across pages, so unless you are ok with the information being in memory till the user signs out(gets logged out eventually) you will have to delete it when done.
Yes hidden field will be visible in html source, instead you should use datakey to use your secret key

asp.net listview showing data from related entity and changing it

I am using entity framework. My model has entity tblGameInfo where in navigation properties are associations called tblPlayer1 and tblPlayer2 which links to entity tblPlayer. I have listview on my page where I want to show game info with players names. I have found that in of listview I should have e.g. one of those:
<td>
<asp:Label ID="tblPlayer1Label" runat="server"
Text='<%# Eval("tblPlayer1.FirstName")%>' />
</td>
<td>
<asp:Label ID="tblPlayer2Label" runat="server"
Text='<%# GetPlayerName(Eval("tblPlayer2.id")) %>' />
</td>
But it does not work. I got nothing displayed in a listview. I have tried many different options but cannot find working one. Probably is some simple mistake but I am new to asp.net and cannot find it.
Second problem with this is editing. In edit template there is dropdownlist with all players.
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="FullName"
DataValueField="id"
SelectedValue='<%# Bind("tblPlayer2") %>'> <- ??
</asp:DropDownList>
</td>
How do I databind it? It is not allowed to bind and change properties? It writes me that element does not exist on a list of elements.
Try the following:
Text='<%# GetPlayerName(((MyObjectType)Eval(("tblPlayer2")).id) %>' />
There may be an extra ) incorrectly there, but essentially cast the player 2 reference and then get its ID.
Also, with EF, you have to load all related objects before you can use them, so make sure the object being bound has its navigation properties loaded.
Lastly, for the drp down list,you have to cast the reference and refer to the ID of the player, which you can get from my updated sample.
HTH.
To your question 1:
I guess that you are using an EntityDataSource as datasource for your ListView, is that right?
If so, try to add the following attribute to your EntityDataSource:
Include="tblPlayer1, tblPlayer2"
This loads the related Entities when the DataSource executes the query.
To your question 2:
Here it is important to know if you are using Entity Framework version 4 or version 1. And in case you are using EF4 if you include foreign keys into your model or not.
If you include foreign keys, then you should have scalar properties like tblPlayer1ID and tblPlayer1ID or similar in your tblGameInfo entity which represent your foreign key columns in the underlying database table. In this case Bind("tblPlayer2ID") in your DropDownList should work.

ASP.NET: How to assign ID to a field in DetailsView?

I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.

Dynamic Id's not working in a GridView. Is there any way to prevent duplicate id's in a gridview?

My gridview has a template field with a label in it. I want to label the field using the QuestionID so that it doesn't create duplicate id's.
I tried doing the following:
<asp:TemplateField HeaderText="No">
<InsertItemTemplate>
<label id='<%# (string.Format("Label_{0}",
DataBinder.Eval(Container.DataItem,"QuestionID"))) %>'
runat="server"></asp:Label>
</InsertItemTemplate>
</asp:TemplateField>
But I got an error saying
The ID property of a control can only be set using the ID attribute in the tag and a simple value.
Does anyone know how to make it so that I can assign an id without it creating duplicate id's? I would like a way of accessing each one using javascript.
Thanks,
Matt
First of all, you're not getting this error because of the duplicate IDs. Whatever you give to your server control as ID, asp.net generates a new ID and renders it. Your exception is about evaluating the value into ID property I think.
IMO, let the asp.net generate the ID's for your labels. Add a class to your labels, and use JQuery to get all instances of your labels.
As an alternative, asp:Labels rendering spans as output, so you can change your code like that :
<asp:TemplateField HeaderText="No">
<InsertItemTemplate>
<span id='<%= (string.Format("Label_{0}",
DataBinder.Eval(Container.DataItem,"QuestionID"))) %>'>
</span>
</InsertItemTemplate>
</asp:TemplateField>

Question: Regarding GridView boundfield ReadOnly property

I have a Gridview boundfield where i set ReadOnly to true because i don't want user to change its value. However on the objectdatasource control's update method that boundfield became null when i try to use it as parameter in update method. Is there a way to set that value during updating?
No, just add the field name you need to the DataKeyNames attribute of the GridView. Then the value will be sent to the Update command.
When you mark a field as read-only on the GridView it renders on the page as a span element, not an input. Therefore the value is not available on PostBack. If you can construct the update statement so that it doesn't expect this field, that would be the best way to deal with this. If the update statement is autogenerated and you can't get around having the value to update, then you can either read the value from the database before doing the update (so that you have it) or include a HiddenField bound to this column and use a literal that obtains the value via Eval instead of binding (if necessary). This will require using a template.
<asp:TemplateField>
<InsertItemTemplate>
<asp:TextBox runat="server" ID="itemTextBox" />
</InsertItemTemplate>
<EditItemTemplate>
<asp:HiddenField runat="server" ID="itemHF" Value='<% Bind("Item") %>' />
<asp:Label runat="server" ID="itemLabel" Text='<% Eval("Item") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="itemLabel" Text='<% Bind("Item") %>' />
</ItemTemplate>
</asp:TemplateField>
Another approach is to add a new query to your tableadapter. Create an update query that just uses the fields desired to be updated. When selecting the update method on the ODS pick the update query. The BoundFields that are not part of the update query can now be turned to readonly=true and it should work.
I had a similar problem and solved it in a slightly different way. But in my case the parameter I wanted to use in my Update method was my primary key and was available in a Query string. So in my DataSource definition I defined the UpdateParameters section to use instead of . Then I was able to remove the parameter completely from my table and it would revert to the Query string parameter.

Resources