0

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

4

1 回答 1

1

首先,您应该注意您的代码对SQL 注入是开放的,因此首先对查询进行参数化。

现在回答您的问题 - 您可以在代码隐藏中使用FindControl方法来获取您的 DropDownList。注册到 RoweCommand 事件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

     if (e.CommandName == "Edit")
     {
            GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

            DropDownList ddl = (DropDownList)row.FindControl("ddlPosition");

            // Do whatever you want with the dropdownlist
     }
}   

其中“编辑”是您在 LinkBut​​ton 中使用的命令的名称

于 2013-01-18T14:39:15.673 回答