如何保存下拉列表的名称而不是其ID?

时间:2017-03-16 17:35:34

标签: c# asp.net sql-server

我一直致力于一个面临问题的项目 我有两个下拉列表是选择类别(ddlcategory)和选择子类别(ddlsubcategory)这里是他们 Image of the drop down list

当我保存数据时,我将ID保存到我的数据库而不是类别名称和子类别名称这是我的数据库值: Image of stored values that is Id of which i want to store the name 这是我实施的后代码:

 String constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True";
string query = "";
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindCategoryDropdown();
    }
}
protected void btnpreviewwebsite_Click1(object sender, EventArgs e)
{
    Response.Redirect("http://localhost:50550/Online Tours and Travels/index.aspx");
}
protected void btnlogout_Click(object sender, EventArgs e)
{
    Session.Abandon();
    Session.Clear();
    Response.Redirect("http://localhost:50550/Online Tours and Travels/Admin Panel/LoginForm.aspx");

}
protected void BindCategoryDropdown()
{
    //conenction path for database
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from category", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlcategory.DataSource = ds;
    ddlcategory.DataTextField = "Cat_name";
    ddlcategory.DataValueField = "Cat_id";
    ddlcategory.DataBind();
    ddlcategory.Items.Insert(0, new ListItem("--Select--", "0"));
    ddlsubcategory.Items.Insert(0, new ListItem("--Select--", "0"));
}

protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
{
    int categoryid = Convert.ToInt32(ddlcategory.SelectedValue);
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from subcategory where catid=" + categoryid, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlsubcategory.DataSource = ds;
    ddlsubcategory.DataTextField = "subcatname";
    ddlsubcategory.DataValueField = "subcatid";
    ddlsubcategory.DataBind();
    ddlsubcategory.Items.Insert(0, new ListItem("--Select--", "0"));

}
protected void btnsave_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(constr);
    String pathName1 = "~/packageimages/" + Path.GetFileName(fileuploadpic1.PostedFile.FileName);
    String pathName2 = "~/packageimages/" + Path.GetFileName(fileuploadpic2.PostedFile.FileName);
    String pathName3 = "~/packageimages/" + Path.GetFileName(fileuploadpic3.PostedFile.FileName);
    query = 
    "insert into package(packname,catid,categoryname,subcatname,packageprice,pic1,pic2,pic3,detail) values('"+txtpackagename.Text+"','"+txtcategoryid.Text+"','"+ddlcategory.Text+"','"+ddlsubcategory.Text+"','"+txtpackageprice.Text+"','"+pathName1+"','"+pathName2+"','"+pathName3+"','"+txtdetails.Text+"') ";
    SqlCommand cmd = new SqlCommand(query, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    fileuploadpic1.SaveAs(Server.MapPath("~/packageimages/" + fileuploadpic1.FileName));
    fileuploadpic2.SaveAs(Server.MapPath("~/packageimages/" + fileuploadpic2.FileName));
    fileuploadpic2.SaveAs(Server.MapPath("~/packageimages/" + fileuploadpic3.FileName));
    txtpackagename.Text = "";
    txtcategoryid.Text = "";
    txtpackageprice.Text = "";
    txtdetails.Text = "";
    string message = "Package Added !!";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}

请帮忙!谢谢

2 个答案:

答案 0 :(得分:0)

控件的名称由aspnet本身生成。根据Control所在的控制树中的位置,它可能会变成这样:

ctl00$ContentPlaceHolder1$ctl00$TextBox1

但如果你真的想得到这个名字,你可以使用UniqueID

string controlName = TextBox1.UniqueID;

答案 1 :(得分:0)

回答你的问题 - 你需要使用

ddlcategory.SelectedItem.Text不是ddlcategory.Text

但正如所建议的,首先要了解查询参数 例如here
关于using语句,例如here