簡體   English   中英

將下拉列表添加到GridView單元格

[英]Add a drop down list to a GridView cell

我想在GridView中創建一列下拉列表,以便用戶可以從該列表中選擇一個選項。

GridView代碼:

<asp:GridView  style="float:left"  
      ID="gvBookings" 
      ShowHeaderWhenEmpty="true"
      CssClass="tblResults" 
      runat="server" 
      OnRowDataBound="gvBooking_RowDataBound"                             
      DataKeyField="ID" 
      AutoGenerateColumns="false"
      allowpaging="false" />
        <Columns>       
             <asp:BoundField DataField="FinishTime" HeaderText="Finish Time"></asp:BoundField>
             <asp:BoundField DataField="TimeSpentName" HeaderText="Time Spent By"></asp:BoundField>
         </Columns>
     </asp:GridView>

背后的代碼:

protected void gvBooking_RowDataBound(object sender, GridViewRowEventArgs e)
 {
      BHTaskClass.BookingTask booking = (BHTaskClass.BookingTask)e.Row.DataItem;
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            int count = 1;
            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 + "\" >";
               }
                count++;
            }
      }
 }

在后面的代碼中,我將FinishTime的單元格更改為文本框。 當用戶在此處輸入值時,它將調用一個更新數據庫的函數。 如何將單元格更改為下拉菜單? 我可以像創建文本框一樣在后面的代碼中做到這一點嗎?還是最好在GridView中更改Boundfield

我認為您必須開始使用TemplateField而不是BoundField 然后,您可以直接在GridView中放置一個TextBox和/或DropDownList。 然后,您可以使用OnRowCommand事件保存更改。

<asp:GridView ID="gvBookings"
          runat="server"
          OnRowDataBound="gvBookings_RowDataBound"
          OnRowCommand="gvBookings_RowCommand">
  <Columns>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:TextBox ID="TextBox1"
                 runat="server"
                 Text='<%# Eval("FinishTime") %>'></asp:TextBox>
       <asp:LinkButton ID="LinkButton1"
                      runat="server"
                      CommandName="saveTextBox"
                      CommandArgument='<%# Container.DataItemIndex %>'>Save</asp:LinkButton>
           </ItemTemplate>
         </asp:TemplateField>
        <asp:TemplateField>
      <ItemTemplate>
    <asp:DropDownList ID="DropDownList1"
                      runat="server"></asp:DropDownList>
     </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

后面的代碼

protected void gvBookings_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //find the dropdownlist in the current row with findcontrol and cast back to one
        DropDownList dropDownList = e.Row.FindControl("DropDownList1") as DropDownList;

        //add some listitems
        dropDownList.Items.Insert(0, new ListItem("Text A", "A"));
        dropDownList.Items.Insert(1, new ListItem("Text B", "B"));
        dropDownList.Items.Insert(2, new ListItem("Text C", "C"));

        //select a value in the dropdown
        dropDownList.SelectedValue = "B";
    }
}

protected void gvBookings_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //check the commandname
    if (e.CommandName == "saveTextBox")
    {
        //convert the commandargument to a row number
        int rowNumber = Convert.ToInt32(e.CommandArgument);

        //find the textbox in the current row with findcontrol and cast back to one
        TextBox textBox = gvBookings.Rows[rowNumber].FindControl("TextBox1") as TextBox;

        //do stuff with the textbox value
        Label1.Text = textBox.Text;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM