我是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>
答案 0 :(得分:1)
您需要从dropdwon的SelectedValue
属性中获取值。
cmd.Parameters.AddWithValue("@designation", ddlDesignation.SelectedValue.ToString());
答案 1 :(得分:0)
你试过吗
var e = document.getElementById("ddlDesignation");
var strDesigId = e.options[e.selectedIndex].value;
document.getElementById("TextBox1").value = strDesigId;