2012-03-31 31 views
0

我通常基於數據創建一個gridview,所以它的行將取決於數據源吐出的內容,但這次..我需要做一些不同的事情。我需要與各行上以下創建一個5行的gridview

TextBox | DropDown | FileUploader | Checkbox 
TextBox | DropDown | FileUploader | Checkbox 
TextBox | DropDown | FileUploader | Checkbox 
TextBox | DropDown | FileUploader | Checkbox 
TextBox | DropDown | FileUploader | Checkbox 

然後,當點擊提交按鈕,將採取每行和每創造一個產品5行(我會打電話給產品類和公正的分配性質的行像所有行一樣保存產品)。

哪能那些5項GridView控件如果它不依賴於數據?它需要有GridView控件的屬性,如標題將

Product Name | Lot # | Image |  Active 

我只是不知道如何添加這些5行如果不取決於任何數據源,但仍想使用gridview屬性。

任何想法將不勝感激,第一次我不得不處理不依賴於數據的gridview。

注意:它將始終是5行..無論如何,我會檢查是否填寫所有信息以決定是否添加產品。

謝謝

回答

1

首先,讓我們創建GridView控件與標記控制和按鈕下方保存的產品...

<asp:GridView runat="server" ID="ProductGridview" AutoGenerateColumns="false"> 
    <Columns> 
     <asp:TemplateField HeaderText="Product Name"> 
      <ItemTemplate> 
       <asp:TextBox runat="server" ID="ProductNameTextBox" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Lot #"> 
      <ItemTemplate> 
       <asp:DropDownList runat="server" ID="LotNumberDropDownList"> 
        <asp:ListItem Text="1" /> 
        <asp:ListItem Text="2" /> 
        <asp:ListItem Text="3" /> 
       </asp:DropDownList> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Image"> 
      <ItemTemplate> 
       <asp:FileUpload runat="server" ID="ImageFileUpload" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Active"> 
      <ItemTemplate> 
       <asp:CheckBox Text="Active" runat="server" ID="ActiveCheckBox" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
<asp:Button Text="Save Products" runat="server" ID="SaveProductsButton" OnClick="SaveProductsButton_Click" /> 

...給我們中的每一行文本框,下拉列表,文件上傳和複選框。

現在,我們需要生成一組5行。我們可以通過GridView控件綁定到List<int>,我們可以在Page_Load產生做到這一點真的容易 ...

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     // Generate a list of 5 integers - this will be the data source for the GridView 
     List<int> rows = new List<int>(); 

     for (int i = 0; i < 5; i++) 
     { 
      rows.Add(i); 
     } 

     //Bind the Gridview to the list of integers so we get 5 rows in the UI 
     ProductGridview.DataSource = rows; 
     ProductGridview.DataBind(); 
    } 
} 

這給了我們這樣的呈現GridView控件: enter image description here

現在我們只需要用代碼讀取單擊按鈕時返回的行;我們通過遍歷行,找到每行中的控件以讀取它們的值...

protected void SaveProductsButton_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow row in ProductGridview.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
      // Have to cast the result of FindControl to the correct type 
      TextBox productTextBox = (TextBox)row.FindControl("ProductNameTextBox"); 
      DropDownList lotNumberDropDownList = (DropDownList)row.FindControl("LotNumberDropDownList"); 
      FileUpload imageFileUpload = (FileUpload)row.FindControl("ImageFileUpload"); 
      CheckBox activeCheckBox = (CheckBox)row.FindControl("ActiveCheckBox"); 

      saveProduct(productTextBox.Text, lotNumberDropDownList.SelectedItem.Text, 
            imageFileUpload.PostedFile, activeCheckBox.Checked); 
     } 
    } 
} 
+0

非常感謝,這就像迄今爲止我看到的最詳細的答案。我非常欣賞這一步,我相信它會幫助許多正在開始使用這項技術的人。 – user710502 2012-03-31 22:20:00

0

您可以創建一個List並將五項DummyObj添加到該列表中。 DummyObj是一個幫助對象,它包含您需要的列的四個屬性。

或者我錯過了什麼?

相關問題