Hi
try this example to Limit length of text in BoundField :-
<script type="text/javascript">
function isMaxLen(o){
var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";
if(o.getAttribute && o.value.length>nMaxLen){
o.value=o.value.substring(0,nMaxLen)
}
}
</script>
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [country] WHERE [countryid] = @countryid" InsertCommand="INSERT INTO [country]
([countryid], [countryname]) VALUES (@countryid, @countryname)"
SelectCommand="SELECT * FROM [country]" UpdateCommand="UPDATE [country] SET [countryname] = @countryname WHERE
[countryid] = @countryid">
<DeleteParameters>
<asp:Parameter Name="countryid" Type="Int64" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="countryname" Type="String" />
<asp:Parameter Name="countryid" Type="Int64" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="countryid" Type="Int64" />
<asp:Parameter Name="countryname" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="countryid"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="countryid" HeaderText="countryid" ReadOnly="True" SortExpression="countryid" />
<asp:BoundField DataField="countryname" HeaderText="countryname" SortExpression="countryname" />
</Columns>
</asp:GridView>
</div>
and in code behind :-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
TextBox work = e.Row.Cells[2].Controls[0] as TextBox;
work.Attributes.Add("onkeydown", "isMaxLen(this)");
work.Attributes.Add("maxlength","9");
}
}
}
Hope this helps
Good Luck
Thank you. it worked.
Ajay
Comment by ajay — February 16, 2012 @ 7:17 am