Guys plz have a look:
I tried to update a particular column by using a dropdownlist while editing inside a gridview... I am getting the following Error on updating...
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'
http://s19.postimage.org/ftytfxkyb/Error2.jpg
I am using the following code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CustId" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" BackColor="Cyan" BorderColor="Red" ForeColor="Fuchsia" OnRowCancelingEdit="GridView1_RowCancelingEdit" >
<Columns>
<asp:BoundField DataField="CustId" HeaderText="CustId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="CustAddress" HeaderText="Address" />
<asp:TemplateField HeaderText="City" SortExpression="City">
<EditItemTemplate>
<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true"
DataTextField="City" SelectedValue='<%# Bind("City") %>' AppendDataBoundItems="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Kolkata</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Bangalore</asp:ListItem>
<asp:ListItem>Katihar</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelCity" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
and for rowupdating i am using the following code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
GridViewRow row = GridView1.Rows[e.RowIndex];
objCust.CustId = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
objCust.CustAddress = ((TextBox)row.Cells[2].Controls[0]).Text;
objCust.City =((TextBox)row.Cells[3].Controls[0]).Text;
objCust.UpdateNewUser();
GridView1.EditIndex = -1;
bindGvEdit();
}
catch (Exception ex)
{
lblUpdate.Text = ex.Message;
}
}
Plz give some suggestion abt the problem..
Regards