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

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.

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

Inserting a column with hidden field in gridview

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.

ASP.NET bind single instance data

I have a data class that I want to show in a list on one page and also alone in another page. I know how to do the first:
<asp:Repeater ID="ctrl" runat="server">
<ItemTemplate>
Here's the name: <asp:Literal runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:Repeater>
On the other page, I want to show exactly one instance, and I want to reuse the item template. Is there a control that I can bind to a single instance of this class, instead of a list which contains a single element?
You can use the same Repeater but with a DataSource of on specific record/instance.
You can also use FormView control.
The FormView control gives you the ability to work with a single record from a data source. The FormView control does not specify a pre-defined layout for displaying the record. Instead, you create a template containing controls to display individual fields from the record. For information about programming the FormView control, see FormView Class in the MSDN library.

Multiple databound controls in a single GridView column

I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.

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>

Resources