Yasserzaid’s Weblog

October 2, 2009

Limit length of text in Gridview BoundField

Filed under: ASP.Net, Javascript — yasserzaid @ 3:48 pm

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

1 Comment »

  1. Thank you. it worked.
    Ajay

    Comment by ajay — February 16, 2012 @ 7:17 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.