This is a migrated thread and some comments may be shown as answers.

Using javascript to populate TextBox in EditItemTemplate

3 Answers 292 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam Wills
Top achievements
Rank 1
Adam Wills asked on 18 Jul 2011, 08:09 PM
I've been having a heck of a time trying to get Javascript to populate a textbox in a RadGrid.  I've tried many methods found on the site, but can't seem to get it working.

My EditItemTemplate is as follows:

<EditItemTemplate>
   <asp:TextBox ID="reasonsTextBox" runat="server" Text='<%# Bind("reasons") %>'></asp:TextBox>
</EditItemTemplate>

I have a RadWindow that opens, some options are selected, then the value is converted to a string, and returned.  I have my value passing fine, but just can't get it to populate into the textbox.

Any help would be appreciated.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 Jul 2011, 05:23 AM
Hello Adam,

I am not quite sure about the requirement and I suppose you want to access the EditTemplate TextBox value from client side. If that is the requirement, try the following approach in this code library.
Accessing server controls in a grid template on the client.

Thanks,
Shinu.
0
Adam Wills
Top achievements
Rank 1
answered on 19 Jul 2011, 09:39 PM
Shinu,

I can't seem to get it working.
<telerik:GridTemplateColumn DataField="reasons" FilterControlAltText="Filter reasons column" HeaderText="Reasons"
UniqueName="reasons">
  <EditItemTemplate>
    <asp:TextBox ID="reasonsTextBox" runat="server" Text='<%# Bind("reasons") %>'></asp:TextBox>
    <script type="text/javascript">registeredElements.push('<%#
Container.FindControl("reasonsTextBox").ClientID %>');
  </script>
</EditItemTemplate>
</telerik:GridTemplateColumn>

<script language="javascript" type="text/javascript">
 var registeredElements = [];
 function GetGridServerElement(serverID, tagName) {
   if (!tagName)
  tagName = "*";
  var grid = document.getElementById("<%=RadGrid2.ClientID %>");
  var elements = grid.getElementsByTagName(tagName);
  for (var i = 0; i < elements.length;
i++) {
  var element = elements[i];
  if (element.id.indexOf(serverID) >= 0)
  return element;
  }
}
 
function CloseReasons(sender,args) {
 var mynum = sender.argument;
 var serverID = "reasonsTextBox"
 var tagName = "input";
 var txtBox = GetGridServerElement(serverID, tagName);
 txtBox.value = "testing"
}

It doesn't seem as if the element is getting pushed to the array, as when I display the value of registeredElements.length, it's always 0.

Can you see anything that I'm doing wrong?
0
Adam Wills
Top achievements
Rank 1
answered on 20 Jul 2011, 02:44 PM
Turns out that I'm not necessarily doing anything wrong.... but I was using AJAX to change into Editing mode.  Once I turned that off, it worked.

So, my next question... is it possible to do this with AJAX enabled?
Tags
Grid
Asked by
Adam Wills
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Adam Wills
Top achievements
Rank 1
Share this question
or