【问题标题】:Add a drop down list to a GridView cell将下拉列表添加到 GridView 单元格
【发布时间】:2017-04-04 15:38:27
【问题描述】:

我想在 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 会更好

【问题讨论】:

    标签: c# asp.net gridview drop-down-menu tablecell


    【解决方案1】:

    我认为你必须开始使用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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      相关资源
      最近更新 更多