Important alert: (current site time 7/15/2013 8:51:22 AM EDT)
 

VB icon

Add javascript and fire it from DataGrid control

Email
Submitted on: 4/3/2003 1:39:56 PM
By: James Fleming  
Level: Advanced
User Rating: By 2 Users
Compatibility: ASP.NET
Views: 20062
 
     Allows the firing of a javascript from within a control embedded in a datagrid.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Add javascript and fire it from DataGrid control
//**************************************
why bother? you'll only take the credit anyway!
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Add javascript and fire it from DataGrid control
// Description:Allows the firing of a javascript from within a control embedded in a datagrid.
// By: James Fleming
//
// Assumes:This javascript tests the length of a multiline textbox & fires off an error message if it is too long.
Assumes you know how to build & populate a datagrid:
this example contains 2 columns (provided)
 <asp:TemplateColumn HeaderText="Next Step" ItemStyle-Width="2%">
 <ItemTemplate>
<asp:TextBox id="txtNEXT_STEP" Width=120 maxlength=50 runat="server" Rows=2 Wrap=True Text='<%# Container.DataItem("NEXT_STEP").tostring()%>' Height="40" TextMode="MultiLine">
</asp:TextBox>
<asp:RequiredFieldValidator ID="valNext_Step" ControlToValidate="txtNEXT_STEP" Display="None" ErrorMessage="The Next Step is a required field."></asp:RequiredFieldValidator>
 </ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Comments" ItemStyle-Width="2%">
 <ItemTemplate>
<asp:TextBox id="txtComments" Width=200 maxlength=40 runat="server" Text='<%# Container.DataItem("COMMENTS").tostring()%>' Rows="2" Height="40">
</asp:TextBox>
<asp:RequiredFieldValidator ID="valComments" ControlToValidate="txtComments" Display="None" ErrorMessage="The Comments is a required field."></asp:RequiredFieldValidator>
 </ItemTemplate>
</asp:TemplateColumn>
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1108&lngWId=10//for details.//**************************************

First the javascript:
Dim sb As New System.Text.StringBuilder()
sb.Append("<SCRIPT LANGUAGE = javascript>")
sb.Append("<!-- " & vbCrLf)
sb.Append(" function charCount(txt, chars, control){" & vbCrLf)
sb.Append("var txtLen=txt.value;" & vbCrLf)
sb.Append("var len = chars - txtLen.length; " & vbCrLf)
sb.Append("if (txtLen.length > chars){" & vbCrLf)
sb.Append("alert('The length of the ' + control + ' may only be ' + chars + ' long. \n The length must be shorter by ' + -len + ' characters.');" & vbCrLf)
sb.Append("}" & vbCrLf)
sb.Append("}" & vbCrLf)
sb.Append("--></script>" & vbCrLf)
RegisterClientScriptBlock("cs", sb.ToString)
Next:
In the code behind during the loading of the grid or during the edit event you add the Attributes to the textboxs in the grid:
For i = 0 To grdData.Items.Count - 1
 CType(grdData.Items(i).FindControl("txtNEXT_STEP"), TextBox).Attributes.Add("onBlur", "charCount(" & CType(grdData.Items(i).FindControl("txtNEXT_STEP"), TextBox).ClientID & ",50, 'Next Step')")
 CType(grdData.Items(i).FindControl("txtComments"), TextBox).Attributes.Add("onBlur", "charCount(" & CType(grdData.Items(i).FindControl("txtComments"), TextBox).ClientID & ",255, 'Comments')")
Next


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
7/17/2003 9:19:12 AM

The code is good. Thanks. The comments are CSharp-style and the code is VB. This odd. Furthermore, the code should be formatted to fit on a page 8.5
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.