如何在GridView中添加包含项目选择和文本框的下拉列表

本文关键字:选择 文本 下拉列表 项目 包含 GridView 添加 | 更新日期: 2023-09-27 18:28:18

我是asp.net开发的新手

我已经创建了一个项目,它可以正常工作,但我想从下拉列表中选择名称,但想存储名称的id而不是名称这是我的项目的Asp.net代码

protected void BindData()
        {
            DataSet ds = new DataSet();
            conn.Open();
            string cmdstr = "Select * from EmployeeDetails";
            SqlCommand cmd = new SqlCommand(cmdstr, conn);
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            cmd.ExecuteNonQuery();
            conn.Close();
            GridView1.DataSource = ds;
            GridView1.DataBind();
      }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("ADD"))
            {
                TextBox txtAddEmpID = (TextBox)GridView1.FooterRow.FindControl("txtAddEmpID");
                TextBox txtAddName = (TextBox)GridView1.FooterRow.FindControl("txtAddName");
                DropDownList ddlDesignation = (DropDownList)GridView1.FooterRow.FindControl("ddlDesignation");
                TextBox txtAddCity = (TextBox)GridView1.FooterRow.FindControl("txtAddCity");
                TextBox txtAddCountry = (TextBox)GridView1.FooterRow.FindControl("txtAddCountry");

                conn.Open();
                string cmdstr = "insert into EmployeeDetails(empid,name,designation,city,country) values(@empid,@name,@designation,@city,@country)";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                cmd.Parameters.AddWithValue("@empid", txtAddEmpID.Text);
                cmd.Parameters.AddWithValue("@name", txtAddName.Text);
                cmd.Parameters.AddWithValue("@designation", ddlDesignation.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@city", txtAddCity.Text);
                cmd.Parameters.AddWithValue("@country", txtAddCountry.Text);

                cmd.ExecuteNonQuery();
                conn.Close();
                BindData();
            }
        }
        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddlDesignation = (DropDownList)e.Row.FindControl("ddlDesignation");
                DataSet ds = new DataSet();
                conn.Open();
                string cmdstr = "Select * from Designation";
                SqlCommand cmd = new SqlCommand(cmdstr, conn);
                SqlDataAdapter adp = new SqlDataAdapter(cmd);
                adp.Fill(ds);
                ddlDesignation.DataSource = ds.Tables[0];
                ddlDesignation.DataTextField = "designation";

                ddlDesignation.DataValueField = "id";
                ddlDesignation.DataBind();
                ddlDesignation.Items.Insert(0, new ListItem("--Select--", "0"));

                this.TextBox1.Text = ddlDesignation.SelectedItem.ToString();

                conn.Close();
            }
        }

这也是我为项目编写的aspx.cs代码

<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_OnRowDataBound"
            onselectedindexchanged="GridView1_SelectedIndexChanged">
     <Columns>
                <asp:TemplateField HeaderText="Employee ID">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "empid") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddEmpID" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "name") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddName" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Designation">
                    <ItemTemplate>
                        <asp:Label ID="lblDesignation" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "designation") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="ddlDesignation" runat="server" >
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:Label ID="lblCity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "city") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCity" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:Label ID="lblCountry" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "country") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtAddCountry" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Action">
                    <FooterTemplate>
                        <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" Width="100px"></asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
    </asp:GridView>

如何在GridView中添加包含项目选择和文本框的下拉列表

您需要从dropdwon的SelectedValue属性中获取值。

cmd.Parameters.AddWithValue("@designation", ddlDesignation.SelectedValue.ToString());

你试过吗

var e = document.getElementById("ddlDesignation");
var strDesigId = e.options[e.selectedIndex].value;
document.getElementById("TextBox1").value = strDesigId;