Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Requirement---> I have Girdview where rows are dynamically created and it has 5 Columns I need the 5th column i.e UserParameters, which is text field where user will key in the details i want to restrict to 15 Characters.

I have already tried this <%# Eval("UserParameters").ToString().Length>25 ? (Eval("UserParameters") as string).Substring(0,25) : Eval("UserParameters") %> but no go .. Please help me to fix this problem thank you

Below is the code
ASP.NET
<asp:GridView ID="gvTestScript" runat="Server" CssClass="GridViewStyle" AutoGenerateColumns="False" CellPadding="4"
            ForeColor="Black" DataKeyNames="TestScriptID" ShowFooter="false"
            Height="110px" Style="margin-right: 0px" Width="990px" BackColor="White" OnRowDataBound="gvTestScript_RowDataBound"
            AutoGenerateEditButton="True" OnRowEditing="gvTestScript_RowEditing" OnRowCancelingEdit="gvTestScript_RowCancelingEdit"
            OnRowUpdating="gvTestScript_RowUpdating" OnRowDeleted="gvTestScript_RowDeleted"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" 
            OnRowCommand="gvTestScript_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="TestScriptID" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblTestScriptID" runat="server" Height="20px" Text='<%#Eval("TestScriptID")%>'>  </asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Options" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="images/delete.ico" CausesValidation="false"
                            CommandName="DeleteRow" OnClick="btnDelete_Click"  OnClientClick='return confirm("Are you sure you want to delete?");' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ID="btnSave" runat="server" ImageUrl="images/save.ico" CausesValidation="false"
                            CommandName="SaveRow" OnClick="btnSave_Click" />
                    </FooterTemplate>
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StepTableName" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblStepTableName" runat="server" Height="20px" Text='<%#Eval("StepName")%>'>  </asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlStepTableName" runat="server" Visible="true" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlStepTableName_SelectedIndexChanged">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlFooterStepTableName" runat="server" Visible="true" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlFooterStepTableName_SelectedIndexChanged">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Test Data" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblTestData" runat="server" Height="20px" Text='<%#Eval("Aruguments")%>'>  </asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlTestData" runat="server" Visible="true" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlTestData_SelectedIndexChanged">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlFooterTestData" runat="server" Visible="true" AutoPostBack="True"
                            OnSelectedIndexChanged="ddlFooterTestData_SelectedIndexChanged">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" runat="server" Height="20px" Text='<%#Eval("Descritpion")%>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("Descritpion")%>'
                            Visible="true">
                        </asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtFooterDescription" runat="server" Visible="true"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="UserParameters" ItemStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <%--<asp:Label ID="lblUserParameters" runat="server" Height="20px" Text='<%#Eval("UserParameters")%>'></asp:Label>--%>
                        <asp:Label ID="lblUserParameters" runat="server" Height="20px" Text='<%# Eval("UserParameters").ToString().Length>25 ? (Eval("UserParameters") as string).Substring(0,25) : Eval("UserParameters")  %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtUserParameters" runat="server" Text='<%# Bind("UserParameters") %>'
                            Visible="true">
                        </asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtFooterUserParameters" runat="server" Visible="true"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" HorizontalAlign="Justify"
                VerticalAlign="Middle" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
            <EmptyDataTemplate>
                <table>
                    <tr style="background-color: #999999;">
                        <th>
                            Option
                        </th>
                        <th>
                            StepTableName
                        </th>
                        <th>
                            Test Data
                        </th>
                        <th>
                            Description
                        </th>
                        <th>
                            UserParameters
                        </th>
                    </tr>
                    <tr>
                        <td headers="Option">
                           <asp:ImageButton ID="btnSave" runat="server" ImageUrl="images/save.ico" CausesValidation="false"
                            CommandName="FirstSaveRow" OnClick="btnSave_Click" />
                        </td>
                        <td headers="StepTableName">
                           <asp:DropDownList ID="ddlFooterStepTableName" runat="server" Visible="true" >
                            </asp:DropDownList>
                        </td>
                        <td headers="Test Data">
                             <asp:DropDownList ID="ddlFooterTestData" runat="server" Visible="true" >
                             </asp:DropDownList>
                        </td>
                        <td headers="Description">
                            <asp:TextBox ID="txtFooterDescription" runat="server" Visible="true"></asp:TextBox>
                        </td>
                        <td headers="UserParameters">
                            <asp:TextBox ID="txtFooterUserParameters" runat="server" Visible="true"></asp:TextBox>
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>
        </asp:GridView>
Posted

Hai
just use MaxLenght property for textbox.in this u will restrict,it not show any msg .


Regards
AravindB
 
Share this answer
 
Comments
ShaHam11 7-Jan-14 5:46am    
Thank you
Karthik_Mahalingam 7-Jan-14 5:56am    
5,correct
ShaHam11 9-Jan-14 6:41am    
Hi Karthik would you be able to help me out in this http://www.codeproject.com/Questions/703873/Alert-Message-when-user-moves-out-of-gridview-when
You can use MaxLength Property for a TextBox
 
Share this answer
 
Comments
Karthik_Mahalingam 7-Jan-14 5:56am    
5,correct
Gandalf_TheWhite 7-Jan-14 6:06am    
Thank You. However Your solution is better. :) 5, for it.

May be I am late to post answer the questions, as I can see while I have submitted answer people already have posted their.

I used to keep open question in tabs while working and when get chance I post the asnwer.

For Next time I should Refresh the page before submitting the answer. :)
Karthik_Mahalingam 7-Jan-14 6:34am    
:)sometimes it happens..
use MaxLength Property to restrict

ASP.NET
<asp:TextBox ID="txtUserParameters" MaxLength="25" runat="server" Text='<%# Bind("UserParameters") %>'
                       Visible="true">
                   </asp:TextBox>
 
Share this answer
 
Comments
ShaHam11 7-Jan-14 5:46am    
Thank you Karthik.. That worked :-)
Karthik_Mahalingam 7-Jan-14 5:55am    
welcoem ShaHam11 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900