Inserting a column with hidden field in gridview - asp.net

I need to insert a column with hidden field in grid view.
The user should not know that one column is there.
I tried the following: created a css class width display:none; and assigned ItemStyle-CssClass="MyCssClass".
But result is not satisfactory.
Inserted a template field and in itemtemplate I had given a asp:HiddenField
Both method shows an extra column hidden field.
There is no value since the fields are rendered as hidden fields but that column have width nearly 10 pixels (see this image http://www.tiikoni.com/tis/view/?id=c500726). So the user feels an empty row is there.
I need to completely hide the column.
I cannot use template field with visibility=false, because I need to access its value from client side.

You can keep the hidden field in your any column. And get it's value from anywhere you live.
Here is a good link to call value from the server side
access hidden field within gridview control to set a value in javascript?
Edit 1
Add a column as follows:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server"
Value='<%# Eval("Name") %>' />
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Eval("BirthDate") %>' />
</ItemTemplate>
</asp:TemplateField>
And you can get the values of your hidden field easily.

Related

SPGridView Filter menu shown separated of LinkButton with templated header

I use Microsoft.SharePoint.WebControls.SPGridView control to write out data.
Filtering is allowed for grid.
Columns contain templated column, it define ItemTemplate and HeaderTemplate:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="linkTitleHeader" runat="server" Text="TitleHeader1"
CommandName="Sort" CommandArgument="Title"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
some text
</ItemTemplate>
</asp:TemplateField>
</Columns>
It works fine, shows header as link, its performs sorting by click, but filter menu
show on separate row:
i expect that it shows as:
I already try with no result:
Leave Text property empty and define other properties
Set Microsoft.Sharepoint.WebControls.Menu Text programmaticaly after
databind
Set link text by javascript
Have any ideas how to join Menu with LinkButton ? Thanks.
You must also specify the properties of SPGridView : FilterDataFields, FilteredDataSourcePropertyName, FilteredDataSourcePropertyFormat. Maybe this article will help you link

Databinding behaving differently on textbox and label

I have a database with a row that has "TEXTTEXT" inside a "noteText" column and null in that column for a different row. If I run a query that generates 1 rows, then run a different query that has 2 rows, Eval("noteText") returns different information when placed in a TextBox vs. placed in a Label. The TextBox and Label were created specifically for this test; it is not referenced in the codebehind. I think the TextBox for row 1 of the second query is somewhow pulling the data used in row1 of the first query.
I don't think it could be a browser-related issue since the source code of the returned page is itself incorrect.
Aspx file code:
<asp:TemplateField HeaderText ="Notes" SortExpression="noteid">
<ItemTemplate>
STARTXX
<asp:TextBox ID="AtxtNote" ViewStateMode="Disabled" runat="server"
TextMode= "SingleLine" Width="260" Text='<%# Eval("noteText") %>' />
<br />
<asp:Label ID="BtxtNote" runat="server" Width="260"
Text='<%# Eval("noteText") %>' />
<br />
ENDXX
</ItemTemplate>
</asp:TemplateField>
Source code on page:
STARTXX
<input name="ctl00$CPH1$gv$ctl02$AtxtNote" type="text"
value="TEXTTEXT" id="ctl00_ContentPlaceHolder1_gv_ctl02_AtxtNote"
style="width:260px;" />
<br />
<span id="ctl00_CPH1_gv_ctl02_BtxtNote"
style="display:inline-block;width:260px;"></span><br />
ENDXX
Actual spacing is a bit different; I edited the text of the source and output code to prevent horizontal scrolling.
I expected both Eval calls to show an empty string.
We fixed it by adding more calls to DataBind.
The current theory is that the TextBox was being submitted when clicking the button to query the database. After the DataBind happened, Asp.Net repopulated the TextBox with the data that was submitted. The TextBox now logically corresponded to a different row in the database, but was in the same position as the TextBox from the previous page view and thus had the same ClientID.
One reason for this suspicion is that the posts were triggering OnTextChanged events.
I'm still unhappy with this; I don't have full confidence that I know what's going on, even though it is now working.

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