You just created a DropDownList object but you never add it to the GridView cells actually. So there's really no way for you to access it on postbacks. Also you may need to create the control at RowCreated event instead. Here's a working code for your reference:
ASPX
<asp:content id="Content2" contentplaceholderid="MainContent" runat="server" xmlns:asp="#unknown">
<asp:gridview id="GridView1" runat="server" onrowcreated="GridView1_RowCreated">
<columns>
<asp:templatefield>
<itemtemplate>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
</asp:content>
CODE BEHIND:
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace WebFormDemo
{
public partial class DynamicControlInGridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
BindGridView();
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
DropDownList ddlResult = new DropDownList();
ddlResult.ID = "ddlResult";
ddlResult.Items.Insert(0, new ListItem("PASS", "0"));
ddlResult.Items.Insert(1, new ListItem("FAIL", "1"));
PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
p.Controls.Add(ddlResult);
TextBox tbox = new TextBox();
tbox.ID = "TextBox1";
e.Row.Cells[0].Controls.Add(tbox);
}
}
private void BindGridView() {
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
public DataTable CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Lastname", typeof(string)));
dr = dt.NewRow();
dr["ID"] = 1;
dr["Name"] = "Vincent";
dr["LastName"] = "Durano";
dt.Rows.Add(dr);
return dt;
}
protected void Button1_Click(object sender, EventArgs e) {
foreach (GridViewRow row in GridView1.Rows) {
DropDownList dd1 = (DropDownList)row.FindControl("ddlResult");
if (dd1 != null) {
Response.Write("Found DropDown!");
}
TextBox tbox = row.FindControl("TextBox1") as TextBox;
if (tbox != null) {
Response.Write("Found TextBox!");
}
}
}
}
}