当前位置: 代码迷 >> C# >> DropDownList和!IsPostBack有关问题
  详细解决方案

DropDownList和!IsPostBack有关问题

热度:55   发布时间:2016-05-05 04:08:18.0
DropDownList和!IsPostBack问题
本帖最后由 hardy0805 于 2015-04-16 14:07:54 编辑

上面是前台页面效果图,下面是后台cs代码,我想实现的目的是:
当在下拉列表选择某一项的时候,该项的具体信息就在下方格子显示出来。而且可以修改内容,并通过点击提交按钮成功提交。

然后,现在遇到的问题是:
1,当“BindClass();”及以下的几行代码(红色部分)放在 if (!IsPostBack)里面的时候,点击提交按钮,可以成功修改每一项在数据库内容。但是点击下拉列表的某一项,不能显示对应内容,一直显示第一个及产品中心的内容。
2、当以上所述的代码放在 if (!IsPostBack)外面的时候则相反,点击下拉列表的某一项,可以显示对应的内容,但是修改各自内容点击提交的时候,没办法将之修改到数据库,数据库内容还是提交之前的内容。

请问该如何解决?求助!!!!!

public partial class user_productleibei : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string userName = Convert.ToString(Session["userName"]);

            //=Convert.ToString(Session["qx"]);
            if (userName == "")
            {
                Response.Redirect("index.aspx");
            }
            BindClass();  //这是生成下拉列表内容的代码
            int id = Convert.ToInt32(this.DropDownList1.SelectedValue);
            Maticsoft.BLL.productclass bll = new Maticsoft.BLL.productclass();
            Maticsoft.Model.productclass model = bll.GetModel(id);
            this.WE_NewsContent.Text = model.content;
            this.txtpic.Text = model.propic;
            this.TextBox1.Text = model.webtitle;
            this.TextBox2.Text = model.webkeywords;
            this.TextBox3.Text = model.webdescr;


        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string res;
        upload up = new upload();
        res = up.Up(file1, "../Uploads/");
        this.Label2.Visible = true;
        this.Label2.Text = up.Resup[Convert.ToInt32(res)];
        this.txtpic.Text = up.s;
    }
    void BindClass()
    {
        List<productclass> list = Maticsoft.BLL.productclass.get_List();
        foreach (productclass model in list)
        {
            ListItem li = new ListItem();
            li.Text = "+" + model.productname;
            li.Value = model.id.ToString();
            DropDownList1.Items.Add(li);
            BindChild(model.id.ToString(), "|----");
        }
    }

    void BindChild(string ParentID, string separator)
    {
        List<productclass> list = Maticsoft.BLL.productclass.get_List(ParentID);
        foreach (productclass model in list)
        {
            ListItem li = new ListItem();
            li.Text = separator + model.productname;
            li.Value = model.id.ToString();
            DropDownList1.Items.Add(li);
            string separator_ = separator + "-------";
            BindChild(model.id.ToString(), separator_);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string strErr = "";

       
        if (this.txtpic.Text == "")
        {
            strErr += "请插入图片!\\n";
        }

        if (strErr != "")
        {
            MessageBox.Show(this, strErr);
            return;
        }

        int id = Convert.ToInt32(this.DropDownList1.SelectedValue);
        string pic = this.txtpic.Text;
        string content = this.WE_NewsContent.Text;
        Maticsoft.Model.productclass model = new Maticsoft.Model.productclass();
        string webtitle = this.TextBox1.Text;
        string webkeywords = this.TextBox2.Text;
        string webdescr = this.TextBox3.Text;

        model.id = id;
        model.propic = pic;      
        model.content = content;
        model.webtitle = webtitle;
        model.webkeywords = webkeywords;
        model.webdescr = webdescr;
      
        Maticsoft.BLL.productclass bll = new Maticsoft.BLL.productclass();
        bll.Update(model);
        MessageBox.Show(this, "更新成功");
        return;
    }
}
------解决思路----------------------
调试一下你的 void BindClass() 方法,看看是不是给不同的 item 设置了重复的 value 属性。
------解决思路----------------------
调试这类问题,不要单纯从你编程的那种思路去着手。(编程只是开发中的一小部分技术,真正的技术在于你需要另外花精力去研究如何测试和调试)

首先要看看输出的html,看看 <select> 中的 options 的value是否有重复。然后要看看 http 调试跟踪到的、浏览器回发时向服务post提交的数据中的参数中的DropdownList的Value的值是第一个value还是正确的value。最后要在 if(!IsPostback) 判断设置断点,看看 DropdownList 的 Items 有多少个(是只有第一个,还是所有行都有)。

总之按照asp.net的机制来“分段”调试,先学会把问题有信心诊断为1/3或者更小的范围,然后再继续缩小范围。
------解决思路----------------------
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>

注意AutoPostBack=“true”这个一定要有
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
//读取数据并赋值给输入框
        }
  相关解决方案