0

我想问你是否可以帮助我处理我的代码,因为我没有得到我想要的结果,即从下拉列表中加载特定论文的详细信息。

目前,当页面加载时,将加载所选项目的详细信息。但是当我尝试从下拉列表中选择另一个项目时,相应的详细信息不会出现,而是仍然保留前一个。

在下拉列表的属性下,我还将autopostback设置为yes,这样它就会自动加载所选项目的相应详细信息。

请看下面的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetPaper();
        GetInk();

        GetPaperDetails(ddlPaperName.SelectedItem.Value);
        //pnlPrinting.Visible = true;
    }
}
protected void btnCompute_Click(object sender, EventArgs e)
{

}
protected void btnCancel_Click(object sender, EventArgs e)
{

}

private void GetPaper()
{
    con.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText =
        "SELECT * FROM Papers";

    SqlDataReader data = com.ExecuteReader();

    ddlPaperName.DataSource = data;
    ddlPaperName.DataValueField = "PaperID";
    ddlPaperName.DataTextField = "PaperName";
    ddlPaperName.DataBind();

    data.Close();
    con.Close();     
}

private void GetPaperDetails(string paper)
{
    con.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText =
        "SELECT * FROM Papers WHERE PaperID=" + paper;

    SqlDataReader data = com.ExecuteReader();

    while (data.Read())
    {
        lblPaperPrice.Text = data["PaperPrice"].ToString();
        lblDescription.Text = data["PaperDescription"].ToString();
        lblSpecification.Text = data["PaperSpecification"].ToString();
        imgPaper.ImageUrl = "../" + data["PaperImage"].ToString();
    }
    data.Close();
    con.Close();
}


private void GetInk()
{
    con.Open();
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    com.CommandType = CommandType.Text;
    com.CommandText =
        "SELECT * FROM Inks";

    SqlDataReader data = com.ExecuteReader();

    ddlPaperName.DataSource = data;
    ddlPaperName.DataValueField = "InkID";
    ddlPaperName.DataTextField = "InkName";
    ddlPaperName.DataBind();

    while (data.Read())
    {
        lblInkPrice.Text = data["InkPrice"].ToString();
    }
    data.Close();
    con.Close();
}
protected void ddlPaperName_SelectedIndexChanged(object sender, EventArgs e)
{
        GetPaperDetails(ddlPaperName.SelectedItem.Value);
}

在这里期待您的反馈。谢谢!

4

2 回答 2

0

我们这个代码,为此,

 if (!IsPostBack == true)
            {

                drpdownaccountnamebind();
                drpdowncountrynamebind();

            }

我希望这会有所帮助,

于 2013-10-24T04:56:25.837 回答
0

将下拉列表的 AutoPostBack 属性设置为 true。

于 2013-10-23T12:12:14.660 回答