My TT.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="lit1" Text='<%#Eval("E_Name")%>' runat="server">
</asp:Literal>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Eq" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="Name">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [E_Name], [Problem], [Solution] FROM [Equipment] WHERE ([O_ID] = @O_ID)">
<SelectParameters>
<asp:QueryStringParameter Name="O_ID" QueryStringField="TT" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name] FROM [Parts]">
</asp:SqlDataSource>
My TT.aspx.cs
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Literal xx = (Literal)GridView1.Rows[e.NewEditIndex].FindControl("lit1");
String x = xx.Text;
DropDownList x1 = (DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("Eq");
x1.SelectedValue = x;
}
I'm a newbie to asp.net and I'm only testing with this code. The problem I have is that the 2nd FindControl("Eq")
always returns null
while the first one (for the Literal) returns the correct value. I tried setting the edit index of the gridview to e.NewEditIndex but doesn't seem to work.
Basically what I want to happen is if the user clicks on Edit on a row, the original databound value, which is in the label, is selected in the dropdownlist.
Can anybody guide me through this, please?