GridView and Javascript - how to get the ID of a control from the gridview when gridview is in edit mode.


Hey All,
 I need help from  you.
I have a GridView. In the  Edit Item template of the first Column I have a textbox, an user control and hidden field. When I click on the user control it  opens a pop up from which I can choose the required value. The text value is populated in the textbox and Id in the hidden field. Here's the code for edit template.
        <EditItemTemplate>
                        <table cellpadding ="0" cellspacing ="0">
                            <tr>
                                <td>
                                    <asp:TextBox id = "txtEditKnownBy" runat ="server" ReadOnly ="true"  Text='<%# DataBinder.Eval(Container.DataItem,"userName")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <uc2:ucChoose ID="UcEditChooseKnownBy" runat="server" />
                                </td>
                                <td>
                                     <asp:HiddenField id = "hdnEditKnownById" runat ="server" Value='<%# DataBinder.Eval(Container.DataItem,"userId")%>'></asp:HiddenField>
                                </td>
                            </tr>
                        </table>
                    </EditItemTemplate>
 The usercontrol textbox thing works fine if I have them outside the grid. I pass the id of textbox and hidden field using
var vFirstName = document.getElementById("<%=txtEditKnownBy.ClientID %>"); and get them populated.
 
The problem is when I put the textbox and usecontrol inside a grid it isn't able to find the ID using document.getElementById("<%=txtEditKnownBy.ClientID %>");
so nothing is returned and an error is thrown.
Can anybody tell me how to get the ID of a control from the gridview when gridview is in edit mode.
 Thanking You
Nitin Maheshwari 
 
4
luckymaheshwari
1/30/2007 1:10:46 PM
📁 asp.net.presentation-controls
📃 72751 articles.
⭐ 3 followers.

💬 6 Replies
👁️‍🗨️ 9840 Views



You need to use FindControl to get to it. Try something like this.
Me.Gridview1.Rows(Me.Gridview1.EditItemIndex).FindControl("txtEditKnownBy").ClientId
-1
jstreet0204
1/30/2007 1:50:32 PM

Hi
Have you checked what id it brings when txtbox in GridView.(use ViewSource to find)
Some time you have to use uniqueID of txtbox. I hope this works for you.
 or if you want value of that txtbox just use in ItemCreated event of Grid
EditButton.Attributes.Add("Onclick","return confirm('Are you sure you want to edit this " + DataBinder.Eval(e.Item.DataItem,"userName") +"?');");
Thanks

Anuj Patel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If this post answers your question please mark it as Answered.
0
AjPtl
1/30/2007 2:02:53 PM

            
jstreet0204:

You need to use FindControl to get to it. Try something like this.

Me.Gridview1.Rows(Me.Gridview1.EditItemIndex).FindControl("txtEditKnownBy").ClientId


 
Thanx for your reply but I want to get that clinet id in javascript not in code behind.
I am doing some validations before post back.  
-1
luckymaheshwari
1/31/2007 6:02:46 AM

            
AjPtl:

Hi

Have you checked what id it brings when txtbox in GridView.(use ViewSource to find)

Some time you have to use uniqueID of txtbox. I hope this works for you.

 or if you want value of that txtbox just use in ItemCreated event of Grid

EditButton.Attributes.Add("Onclick","return confirm('Are you sure you want to edit this " + DataBinder.Eval(e.Item.DataItem,"userName") +"?');");

Thanks


 
I have checked that. That works but I don't want to hardcore the Id's so is there any other way 
0
luckymaheshwari
1/31/2007 6:11:05 AM

Hi,
If you need to access the Textbox in the javascript you could not directly get it by
var vFirstName = document.getElementById("<%=txtEditKnownBy.ClientID %>");
But, as this textbox is inside the Gridview, you need to get it by getting the gridview, and the editindex. Depending upon the editindex you could go to that row and get the cell in which the textbox is present and get it from the controls.
You can use the below code,
// Get the GridView
var GridView = document.getElementById('<%= gvData.ClientID %>');
// Get the EditIndex from a hidden variable
var EditIndex = document.getElementById('<%= hidEditIndex.ClientID %>').value;
// Now go to the EditIndex row and loop throgh the control collection to get the Textbox
var FirstName = GridView.rows[EditIndex].cells[ColumnNo].children[0]; 
// This will give you the first element in that cell, ie the TextBox
// To access the Hiddenvariable, which is the third element in the cell, we nned to use
var HiddenValue = GridView.rows[EditIndex].cells[ColumnNo].children[2]; 
 Hope it helps you.

Rajganesh
__________________________________________________
My Blog
If this post answers your question please mark it as Answered.
0
Rajganesh
1/31/2007 1:06:57 PM

I usually use Literal controls to populate variables in my javascript code. like this.
Then in the RowEditing event of the gridview add this code.
Protected
Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles Gridview1.RowEditing
Me.lit_textBoxId.Text = Me.Gridview1.Rows(e.NewEditIndex).FindControl("txtEditKnownBy").ClientId
End Sub
0
jstreet0204
2/1/2007 12:01:55 AM
Reply: