2013-05-21 111 views
0

我有一個兩列的網格視圖。一個名爲產品,另一個名爲(模板字段中的兩列)。在gridview中使用文本框插入值到數據庫表

產品列被綁定到「產品」表。而字段的項目模板包含一個文本框。

我需要通過網格視圖中的文本框將值插入數據庫表「BrandProperties」,但我不知道如何做到這一點。

這裏是我的代碼:

if (!IsPostBack) 
    { 
     BindView(); 

    } 

    DataTable dt = new DataTable(); 
    SqlDataAdapter da = new SqlDataAdapter("select ID,TypeName from ProductTypes",con); 
    da.Fill(dt); 
    DropDownList1.DataSource = dt; 
    DropDownList1.DataValueField = "ID"; 
    DropDownList1.DataTextField = "TypeName"; 
    DropDownList1.DataBind(); 
} 

public void BindView() 
{ 

    DataTable dt = new DataTable(); 
    string sql = "select * from Properties"; 
    con.Open(); 
    SqlDataAdapter da = new SqlDataAdapter(sql, con); 
    da.Fill(dt); 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
    con.Close(); 
} 

aspx.code:

Text="Brand Name"></asp:Label> 
    <asp:Button ID="Button1" runat="server" BackColor="#6699FF" 
     style="z-index: 1; left: 410px; top: 391px; position: absolute" 
     Text="SAVE" onclick="Button1_Click" /> 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" 
    style="z-index: 1; left: 52px; top: 230px; position: absolute; height: 133px; width: 344px"> 
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> 
    <Columns> 
     <asp:TemplateField HeaderText="Product"> 
      <ItemTemplate> 
       <asp:Label ID="Label3" runat="server" Text='<%# Bind("PropertyName") %>' ></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Value"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
+0

從頁腳模板,你可以添加新的項目。我寫了一篇關於這個Refer = http://satindersinght.blogspot.in/2012/08/how-to-addupdate-record-using-gridview.html –

+0

,但我需要通過使用項目模板添加新項目,其中包含一個文本框:(該值應該保存在數據庫 – user2404367

+0

:據我所知,「ItemTemplate」用於顯示數據,更新/編輯「EditTemplate」用於添加新項目「Footertemplate」應該使用。你的意思是說在你的情況下,項目模板可用於編輯?用戶可以查看/編輯 –

回答

0

試試這個

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" Style="z-index: 1; left: 52px; top: 230px; 
    position: absolute; height: 133px; width: 344px"> 
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" /> 
    <Columns> 
     <asp:TemplateField HeaderText="Product"> 
      <ItemTemplate> 
       <asp:Label ID="Label3" runat="server" Text='<%# Bind("PropertyName") %>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Value"> 
      <ItemTemplate> 
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
       <asp:Button ID="btnSave" runat="server" Text="SaveValues" OnClick = "btnSave_OnClick" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 




protected void btnSave_OnClick(object sender, EventArgs e) 
{ 
    Button lb = (Button)sender; 
    GridViewRow row = (GridViewRow)lb.NamingContainer; 
    if (row != null) 
    { 
     int index = row.RowIndex; //gets the row index selected 

     TextBox tb = (TextBox)GridView1.Rows[index].FindControl("TextBox2"); 
     string YourRequiredText = tb.Text; 
    } 
} 
+0

你必須做錯了什麼,在這裏複製你的最新代碼。 –

相關問題