2014-01-30 134 views
1

創建未在Gridview中更新其職位的用戶列表。我希望列表中的下拉列表中包含所有可能的標題選擇和下拉列表旁邊的按鈕。然後,一個人可以進入並更改下拉菜單中的標題,點擊該按鈕並更新並從列表中刪除。獲取Gridview中Dropdownlist的選定值

我有這樣的方式,我希望它看起來,但我想弄清楚如何將該行中的下拉框的SelectedValue傳遞給OnClick後面的代碼。正如你在下面看到的,我能得到的最接近的是傳遞CommandArgument中的行號。任何建議如何我可以將該特定行的下拉列表中的SelectedValue獲取到OnClick?

編輯:也許我應該使用OnRowCommand而不是OnClick?

貌似這個目前:

John Doe | DropdownList Button 
Jane Doe | DropdownList Button 
Joe Doe | DropdownList Button 
Jeff Doe | DropdownList Button 

ASPX

<asp:GridView runat="server" ID="TitleView" OnRowDataBound="TitleView_RowDataBound" AutoGenerateColumns="False"> 
       <Columns> 
        <asp:BoundField DataField="Fullname" HeaderText="Fullname" /> 
        <asp:TemplateField> 
         <ItemTemplate> 
          <div class="input-append"><asp:DropDownList CssClass="span5" ID="TitleList" runat="server"> 
          </asp:DropDownList> 
           <asp:Button ID="lbnView" runat="server" Text="Update" CssClass="btn btn-primary" OnClick="btn_Clicked" 
              CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'></asp:Button></div> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 
</asp:GridView> 

代碼隱藏

public void bindTitleView() 
    { 
     using (SqlConnection conn = new SqlConnection("")) 
     { 
      SqlCommand cmd = new SqlCommand(@"SELECT U.First + ' ' + U.Last as Fullname, U.UserID, T.Name FROM Employees U LEFT JOIN Titles T ON U.Title = T.ID WHERE U.Active = '1' AND U.Title = '92' ORDER BY Fullname ASC", conn); 
      conn.Open(); 
      SqlDataAdapter adp = new SqlDataAdapter(cmd); 
      DataSet myDataSet = new DataSet(); 
      adp.Fill(myDataSet); 
      TitleView.DataSource = myDataSet; 
      TitleView.DataBind(); 
     } 
    } 
    protected void TitleView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList ddl = (DropDownList)e.Row.FindControl("TitleList"); 
      using (SqlConnection conn = new SqlConnection("")) 
      { 
       SqlCommand cmd = new SqlCommand(@"SELECT ID, Name FROM Titles ORDER BY Name ASC", conn); 
       conn.Open(); 
       SqlDataAdapter adp = new SqlDataAdapter(cmd); 
       DataTable myDataSet = new DataTable(); 
       adp.Fill(myDataSet); 
       ddl.DataSource = myDataSet; 
       ddl.DataTextField = "Name"; 
       ddl.DataValueField = "ID"; 
       ddl.DataBind(); 
      } 
     } 
    } 
    protected void btn_Clicked(object sender, EventArgs e) 
    { 
       String rowid = ((Button)sender).CommandArgument; 
    } 

SOLUTION:我批准了以下工作對我來說,一旦我加入的IsPostBack答案!到Page_Load

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      bindTitleView(); 
     } 

回答

1

你可以做這樣的:

protected void btn_Clicked(object sender, EventArgs e) 
{ 
    int line = ((GridViewRow)((Button)sender).Parent.Parent).RowIndex; 
    DropDownList drp = ((DropDownList)TitleView.Rows[line].FindControl("TitleList")); 
    //Continue the method 
} 
+1

當我嘗試這個,我得到2作爲結果,每次不管我在下拉框中選擇。 2恰好是下拉框中的第一個值,但即使我選擇了不同的選擇,該值始終爲2.任何想法? – techora

+1

想象出來,當我綁定bindTitleView()時,我在Page_Load中忽略了if(!IsPostBack)。哎呀!感謝您的幫助。 – techora

+0

@kcray我也這麼做。大聲笑!很高興幫助你! –

1

在你btn_click寫代碼如下

protected void btn_Clicked(object sender, EventArgs e) 
{ 

    Button Sample = sender as Button; 
    GridViewRow row = Sample.NamingContainer as GridViewRow; 
    DropDownList drp = row.FindControl("TitleList") as DropDownList; 
    //Now use drp.SelectedValue 
    } 
} 

讓我知道如果這個心不是你所期待的。

相關問題