2013-01-18 57 views
0

我使用EditItemTemplate在GridView中爲數據庫中的編輯數據創建DropDownList,但當我選擇值並單擊編輯按鈕時,我無法獲取值DropDownList。如何在ControlParameter中查找控件? asp.net c#

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> 

//代碼在數據源更新

UpdateCommand="UPDATE Profile SET RespIndexID = @ddlPosition WHERE (EmpID = @EmpID)"> 
<UpdateParameters> 
    <asp:ControlParameter ControlID="ddlPosition" Name="RespIndexID" PropertyName="SelectedValue" Type="String" /> 
<asp:Parameter Name="EmpID" />      

+0

開放用於SQL'injection' –

回答

1

首先,你要注意,你的代碼是開放的SQL Injections,所以從參數化開始查詢。

現在對於你的問題 - 你可以通過使用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的

+0

我需要在下拉列表使用GridView1_OnRowDataBound爲添加項目的命令的名稱。 – SueSaya

+0

Ypu寫道:_「當我選擇值並單擊編輯按鈕時,我無法獲得值的形式DropDownList。」_ - 編輯按鈕與OnRowDataBound有什麼關係? – Blachshma

+0

是否有另一種方法通過控制參數從下拉列表中獲取值? – SueSaya