我使用 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" />