Click here to Skip to main content
15,887,319 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
am getting an error while updating my grid view, Unable to cast object of type 'System.Web.UI.DataBoundLiteralControl' to type 'System.Web.UI.WebControls.TextBox'"



This is my aspx code

ASP.NET
<div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
            PageSize="10" Font-Names="Arial" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
            HeaderStyle-BackColor="green" OnPageIndexChanging="OnPaging">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="InvestmentType" HeaderText="Investment Type" />
                <asp:TemplateField>
                    <HeaderTemplate>
                        Amount:
                        <asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="CountryChanged"
                            AutoPostBack="true" AppendDataBoundItems="true">
                            <asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>
                            <asp:ListItem Text="Top 10" Value="10"></asp:ListItem>
                        </asp:DropDownList>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Amount")%>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="MonthlyPay" HeaderText="Monthly Pay" />
  <asp:BoundField HeaderStyle-Width="125px" DataField="NumberOfMonths" HeaderText="Months" />
                   </Columns>
        </asp:GridView>
    </div><pre lang="HTML"><pre lang="HTML"><pre lang="HTML">



for searching records by using dropdownlist i have given that templatefield

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
  int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        TextBox textName = (TextBox)row.Cells[1].Controls[0];
        TextBox textInvestmentType = (TextBox)row.Cells[2].Controls[0];
        TextBox textAmount = (TextBox)row.Cells[3].Controls[0];
        TextBox textMonthlyPay = (TextBox)row.Cells[4].Controls[0];



am getting that error at textamount

Thanking you
Posted
Updated 27-Aug-15 1:51am
v3
Comments
F-ES Sitecore 27-Aug-15 4:22am    
The controls you are trying to case to TextBox are not text boxes. You don't have any textboxes in your code.
Member 11382784 27-Aug-15 5:21am    
am updating grid view columns
F-ES Sitecore 27-Aug-15 5:44am    
You have to cast the controls to their proper types, you can't cast to textbox if they are not of that type. To find out the actual type of your controls try something like

object c = row.Cells[1].Controls[0];

then in the debugger examine "c" to see what type it is, that is what you need to cast to.
Herman<T>.Instance 27-Aug-15 7:52am    
TextBox textName = (TextBox)row.Cells[1].Controls[0];

should be
TextBox textName = new TextBox();
textName.Text = row.Cells[1].Controls[0].Text;
Member 11382784 4-Sep-15 2:06am    
textName.Text = row.Cells[1].Controls[0].Text;

am getting error while converting at .text,if i give .Tostring() no error but when iam updating particular row it is showing as system.web.ui.literal...

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