Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid validation 1

Status
Not open for further replies.

jrl237

IS-IT--Management
Joined
Jan 29, 2002
Messages
61
Location
US
I have a databound datagrid with a few columns of information, a button, and a template column containing a textbox. What I want to have happen is the user enters a value in the textbox for any given row, clicks the button for that row, and the value is validated client-side.

I can get the button to fire a javascript function without any problem. But what I can't figure out is how to get the values of the textbox and other fields in the grid, and pass them to (or read them from) the javascript function.

Anyone have any ideas how I could accomplish this?

TIA,

JRL
 
Here's my most-recent attempt:

Code:
<ASP:TEMPLATECOLUMN headertext="Amount Shipped">
<ITEMTEMPLATE>
  <ASP:TEXTBOX id="txtQuanShipped" runat="server" width="42px"></ASP:TEXTBOX>
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>
<ASP:TEMPLATECOLUMN>
<ITEMTEMPLATE>
<ASP:BUTTON runat="server" id="cmdAddToShip" text="Add To Shipment" commandname="AddToShip" causesvalidation="false"></ASP:BUTTON>
</ITEMTEMPLATE>
</ASP:TEMPLATECOLUMN>

I add a link to the custom javascript validator from each button using code like this:

Code:
Private Sub dgOrderDetails_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgOrderDetails.ItemDataBound
  Dim cmdAdd As Button
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    cmdAdd = CType(e.Item.Cells(5).FindControl("cmdAddToShip"), Button)
    cmdAdd.Attributes.Add("onclick", "return Valid();")
  End If
End Sub
 
Ok, here's a script that will work:

Code:
<script>
function Valid(x)
{
var btnID = x.id;
var btnName = "cmdAddToShip";
var txtName = "txtQuanShipped";
		
var startPos = btnID.indexOf(btnName,0);
		
var txtID = btnID.substring(0,startPos) + txtName + btnID.substring(startPos+btnName.length,btnID.length);
		
alert(document.getElementById(txtID).value);
		
}
</script>

When you register your script on the ItemDataBound event, add the keyword "this" to the script call:

Code:
Private Sub dgOrderDetails_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgOrderDetails.ItemDataBound
  Dim cmdAdd As Button
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    cmdAdd = CType(e.Item.Cells(5).FindControl("cmdAddToShip"), Button)
    cmdAdd.Attributes.Add("onclick", "return Valid(this);")
  End If
End Sub

Why this works:

ASP.NET builds a unique name for each server control in a row. The ID that you give those controls in your declarations is used in naming the controls.

So you have two controls:

txtQuanShipped and cmdAddToShip

When the DataGrid creates a row, the actual controls will be render with ids like:

DataGrid1_ctl1_txtQuanShipped
DataGrid1_ctl1_cmdAddToShip

What I'm doing in the JavaScript is using the id of the button that was clicked (this.id), and using JavaScript string functions to replace "cmdAddToShip" with "txtQuanShipped". Now I can go get that textbox with document.getElementById and do whatever I like with it.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Thanks, Thomas. That was exactly the piece I was missing. I tried it, and it works great.

Thanks much for your help.

JRL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top