EditItemTemplate を使用して GridView で DropDownList を作成し、データベースのデータを編集しますが、値を選択して編集ボタンをクリックすると、DropDownList から値を取得できません。
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
SqlDataAdapter dtAdapter;
DataTable dt = new DataTable();
strSQL2 = "SELECT distinct(gp.RespIndexID) RespIndexID,gr.RespDesc " +
"FROM Login as gbl " +
"LEFT JOIN Profile gp ON gbl.EmpID=gp.EmpID " +
"LEFT JOIN ResponeIndex gr ON gp.RespIndexID=gr.IndexID " +
"WHERE gbl.EmpDept=" + DropDownList3.SelectedValue.ToString() + " AND gbl.EmpSection=" + DropDownList4.SelectedValue.ToString() + " AND gbl.EmpUnit=" + DropDownList1.SelectedValue.ToString() + " AND gbl.Status is Null";
try
{
dtAdapter = new SqlDataAdapter(strSQL2, objConn);
dtAdapter.Fill(dt);
for (int i = 0; dt.Rows.Count > i; i++)
{
if (GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddlPosition = (DropDownList)e.Row.Cells[3].FindControl("ddlPosition");
HiddenField HiddenField1 = (HiddenField)e.Row.Cells[3].FindControl("HiddenField1");
ddlPosition.Items.Add(new ListItem((string)dt.Rows[i]["RespIndexID"].ToString(), dt.Rows[i]["RespIndexID"].ToString()));
if (Page.IsPostBack == true)
{
ddlPosition.SelectedValue = HiddenField1.Value;
}
//er.NewValues[i] = ddlPosition.SelectedValue.ToString();
}
}
}
catch (Exception err)
{
//Response.Write(strSQL2 + "<br />" + err);
}
}
//GridView のコード
<asp:TemplateField HeaderText="RespDesc" SortExpression="RespDesc">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("RespIndexID") %>'></asp:HiddenField>
<asp:DropDownList ID="ddlPosition" runat="server" AutoPostBack="false" AppendDataBoundItems="True"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RespIndexID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
//DataSource のコード更新
UpdateCommand="UPDATE Profile SET RespIndexID = @ddlPosition WHERE (EmpID = @EmpID)">
<UpdateParameters>
<asp:ControlParameter ControlID="ddlPosition" Name="RespIndexID" PropertyName="SelectedValue" Type="String" />
<asp:Parameter Name="EmpID" />