views:

17

answers:

2

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.

+1  A: 

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".

Ben Robinson
Thank you. However, the problem isn't in finding the item using jQuery - it's assigning an ID to the textbox for jQuery to find.
jawonlee
If you use a template field in your details view, you bind your data to nowmal asp.net controls, such as textboxes or labels, in the template. As with all .net controls you give these a unique id. The id of the rendered html control will always end with the id you give your databound asp.net control in your details view template field.
Ben Robinson
I have updated my post to demonstrate what i mean.
Ben Robinson
A: 

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.

lincolnk