在Gridview内部我根据Dropdownlist选择值创建动态文本框我想在数据库中保存文本框值 [英] Inside Gridview I Have Created Dynamic Textbox On The Basis Of Dropdownlist Selected Value I Want To Save The Textbox Value In Database

查看:81
本文介绍了在Gridview内部我根据Dropdownlist选择值创建动态文本框我想在数据库中保存文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Inside Gridview我在Dropdownlist选择值的基础上创建了动态文本框我想将文本框值保存在数据库中

可以任何一个帮助请

Inside Gridview i have created dynamic textbox on the basis of Dropdownlist selected value i want to save the textbox value in database
can any one help please

推荐答案

< text>请尝试使用:

< text> GridView标记

<text>Please try with :
<text>GridView Markup
<asp:gridview id="gvCustomers" datakeynames="CustomerId" runat="server" autogeneratecolumns="false" onrowediting="EditCustomer" onrowdatabound="RowDataBound" onrowupdating="UpdateCustomer" onrowcancelingedit="CancelEdit" xmlns:asp="#unknown">
<columns>
    <asp:boundfield datafield="ContactName" headertext="Contact Name" />
    <asp:templatefield headertext="City">
    <itemtemplate>
        <asp:label id="lblCity" runat="server" text="<%# Eval("City")%>"></asp:label>
    </itemtemplate>
    <edititemtemplate>
            <asp:label id="lblCity" runat="server" text="<%# Eval("City")%>" visible="false"></asp:label>
    <asp:dropdownlist id="ddlCities" runat="server">
    </asp:dropdownlist>
    </edititemtemplate>
    </asp:templatefield>
    <asp:commandfield showeditbutton="True" />
</columns>
</asp:gridview>





< text> 绑定GridView





<text>Binding the GridView

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.BindData();
    }
}
 
private void BindData()
{
    string query = "SELECT top 10 * FROM Customers";
    SqlCommand cmd = new SqlCommand(query);
    gvCustomers.DataSource = GetData(cmd);
    gvCustomers.DataBind();
}
 
private DataTable GetData(SqlCommand cmd)
{
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);
                return dt;
            }
        }
    }
}





< text> 编辑GridView行



<text>Editing the GridView Row

protected void EditCustomer(object sender, GridViewEditEventArgs e)
{
    gvCustomers.EditIndex = e.NewEditIndex;
    BindData();
}
 
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
    gvCustomers.EditIndex = -1;
    BindData();
}





< text> 绑定DropDownList



<text>Binding the DropDownList

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && gvCustomers.EditIndex == e.Row.RowIndex)
    {
        DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
        string query = "select distinct city from customers";
        SqlCommand cmd = new SqlCommand(query);
        ddlCities.DataSource = GetData(cmd);
        ddlCities.DataTextField = "city";
        ddlCities.DataValueField = "city";
        ddlCities.DataBind();
        ddlCities.Items.FindByValue((e.Row.FindControl("lblCity") as Label).Text).Selected = true;
    }
}







< text> 更新GridView行




<text>Updating the GridView Row

protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
    string city = (gvCustomers.Rows[e.RowIndex].FindControl("ddlCities") as DropDownList).SelectedItem.Value;
    string customerId = gvCustomers.DataKeys[e.RowIndex].Value.ToString();
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        string query = "update customers set city = @city where customerId = @customerId";
        using (SqlCommand cmd = new SqlCommand(query))
        {
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@city", city);
            cmd.Parameters.AddWithValue("@customerId", customerId);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}


这篇关于在Gridview内部我根据Dropdownlist选择值创建动态文本框我想在数据库中保存文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆