【问题标题】:Insert values to database table using textbox in gridview使用gridview中的文本框将值插入数据库表
【发布时间】:2013-05-21 07:33:11
【问题描述】:

我有一个包含两列的网格视图。一个名为 product,另一个名为 values(模板字段中的两列)。

product 列绑定到“product”表。 value 字段的项目模板包含一个文本框。

我需要通过网格视图中的文本框将值插入数据库表“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>

【问题讨论】:

  • 从页脚模板中,您可以添加新项目。我已经写了一篇关于这个Refer= satindersinght.blogspot.in/2012/08/…
  • 但我需要使用包含文本框的项目模板添加新项目:(并且该值应保存在数据库中
  • :据我所知ItemTemplate 用于显示数据,用于更新/编辑EditTemplate 和添加新项目Footertemplate 应使用。在这里,您的意思是说在您的情况下,项目模板可用于编辑?用户可以查看/编辑

标签: asp.net


【解决方案1】:

试试这个

    <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;
    }
}

【讨论】:

  • 你一定是做错了什么。在此处复制您的最新代码。我刚刚在上传之前测试了我的
猜你喜欢
  • 2023-03-17
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多