2016-11-23 37 views
0

我有一個GridView和列的一個我使用一個下拉列表中顯示的用戶的列表:如何更改gridview中的下拉列表時調用函數?

<asp:GridView style="float:left" 
    ID="gvBookings" 
    ShowHeaderWhenEmpty="true" 
    CssClass="tblResults" 
    runat="server" 
    OnRowDataBound="gvBookings_RowDataBound"       
    DataKeyField="ID" 
    AutoGenerateColumns="false" 
    allowpaging="false" 
      <Columns>  
       <asp:BoundField DataField="FinishDate" HeaderText="Finish Date"></asp:BoundField> 
       <asp:TemplateField HeaderText="Time Spent By"> 
        <ItemTemplate> 
         <asp:DropDownList id="ddlUsers" runat="server" ></asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
</asp:GridView> 

在後面的代碼我需要調用,更新數據庫時的功能下拉列表已更改。我已經爲FinishDate列做過這個操作,所以在下拉菜單中有類似的方法嗎?

後面的代碼:

protected void gvBookings_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     BHTaskClass.BookingTask booking = (BHTaskClass.BookingTask)e.Row.DataItem; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      foreach (TableCell c in e.Row.Cells) 
      { 
       if (count == 1) 
       { 
        string FinishTime = booking.FinishTime.HasValue ? booking.FinishTime.Value.ToString("hh':'mm") : ""; 
        c.Text = "<input type=\"text\" id=\"txtFinishTime" + booking.ID + "\" style=\"width:70px\" type=\"text\" onblur=\"UpdateFinishTime(" + booking.ID + ",this.value)\" value=\"" + FinishTime + "\" >"; 
       } 
       if (count == 2) 
       { 
        ddlUsers.SelectedValue = booking.TimeSpentName; 
       } 
        count++; 
      } 
     } 
    } 

所以當FinishDate文本框改變它調用UpdateFinishTime功能,這將更新數據庫。如何爲下拉列表調用函數?

回答

1

ASPX

<asp:DropDownList id="ddlUsers" runat="server" 
     AutoPostBack="true" 
     OnSelectedIndexChanged="YourFunction_Changed"> 
</asp:DropDownList> 

後面的代碼:

protected void YourFunction_Changed(object sender, EventArgs e) 
{ 
    //do stuff... 
} 
+0

我怎麼行ID ? – user123456789

+0

@ user123456789每個帖子提出一個問題。一種可能不是最佳的方法是創建自定義DropDown,它繼承DropDown並具有CommandArguemnt屬性。之後,在網格的項目綁定上,您正在查找ddl控件並將該命令參數設置爲該行的ID。如果你想尋找更優化的方式,你可以問另一個問題,比如我說每個帖子有一個問題。 – mybirthname

0

爲了讓行ID,你可以做到這一點,如下

protected void YourFunction_Changed(object sender, EventArgs e) 
{ 
    //do stuff... 
    int Index = ((GridViewRow)((sender as Control)).NamingContainer).RowIndex; 

    // your logic follows based on the Index 
}