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

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

Related

Label value read for Insert command is not the value shown in browser

Good afternoon.
I have a simple gridview with a field like this:
<asp:TemplateField HeaderText="USD Full Load Machine">
<ItemTemplate>
<asp:Label ID="lblUSDFullLoad" runat="server" Text='<%# Bind("USDFullyLoadMachine") %>' />
</ItemTemplate>
</asp:TemplateField>
The InsertCommand and Insertparameters are set up like this:
<asp:SqlDataSource (code omitted)
InsertCommand="Insert Into [xxxxTable]
([SubmissionID],USDFullyLoadMachine]) Values(#newSubID,#usdflm)">
<InsertParameters>
<asp:ControlParameter Name="newSubID" ControlID="lblNewSubmissionID" />
<asp:ControlParameter Name="usdflm" ControlID="gridConversionTotals$ctl02$lblUSDFullLoad" />
</InsertParameters>
</asp:SqlDataSource>
When I change the gridConversionTotals$ctl02$lblUSDFullLoad label's text using client-side javascript, and I execute the insert method on the sql datasource control in code behind, the value sent to SQL is NOT the new value of the label, i.e. is not the value shown on the browser, it is the value the label had when it was last databound.
I initially thought that this was because the server, upon executing the insert command, could not possibly know what the client was showing, but then this is not true, because if I change the label to a textbox and leave all the code intact, the value sent to the database is indeed the one shown on the client.
Can someone please explain why the insert method, in this instance, treats a label's and a textbox's client-side content differently?
And does this mean that generally when I want to change values client-side and then send them to the database I will need to always use textboxes?
Thank you.
After some further research I found this post https://stackoverflow.com/a/10324721/1266732, which explains that the label will not post the value back to the webserver, and therefore it is recommended to use textboxes and styling them as labels for this purpose.
At a technical level, form values are sent using <input /> elements or cookies. If your javascript is not editing an <input /> element or a cookie (or in some way modifying a GET URL), then your value will not make it to the server.

Accessing Gridview row data inside fields

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") %>'/>

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