小弟目前正在做一个文章发布系统,一极栏目,栏目建表:wyx_class,字段有:wyx_id,wyx_classname,wyx_classsort文章内容建表:wyx_text,字段有:wyx_id,wyx_title,wyx_author,wyx_classtype,wyx_content,wyx_date现在后台搞添加文章信息的页面出了问题,因为栏目分类用的是DropDownList控件绑定到wyx_class表,就这玩意儿现在不好使比如我添加一篇文章,栏目选“散文”,“散文”栏目在wyx_class表里的wyx_id是2文章添加进去后,wyx_text表里的wyx_classtype也应该是2才对,但它不是,是1当我添加其它文章,选择另外一些栏目分类时,wyx_classtype全都是1现在的问题也就是添加一篇文章选择好相应的栏目,插入数据库后,文章是进去了,但栏目没有对应
请各位大哥们帮帮忙看看哪里出了问题,谢谢!!!
【前台代码】:<table  border="1" align="center" cellpadding="0" cellspacing="0" class="tableall" bordercolor="#666666">
  <tr>
    <td colspan="2" class="tabletd1" style="background-color: #666666; text-align: center">
        <span style="font-size: 14pt; color: #ffffff"><strong>添加文章信息</strong></span></td>
  </tr>
  <tr class="tabletd2">
    <td width="20%" style="height: 21px">&nbsp;栏目分类:</td>
    <td style="width: 507px; height: 21px;">
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList></td>
  </tr>
  <tr class="tabletd3">
    <td style="height: 24px;">&nbsp;文章标题:</td>
    <td style="width: 507px; height: 24px;">
        <asp:TextBox ID="TextBox1" runat="server" Width="400px"></asp:TextBox></td>
  </tr>
  <tr class="tabletd3">
    <td>&nbsp;文章作者:</td>
    <td style="width: 507px;">
       <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
       </td>
  </tr>
  <tr class="tabletd3">
    <td style="height: 19px">&nbsp;文章内容:</td>
    <td style="width: 507px; height: 19px;">
        <asp:TextBox ID="TextBox3" runat="server" Height="300px" TextMode="MultiLine" Width="500px" Font-Names="宋体"></asp:TextBox></td>
    
  </tr>
  <tr class="tabletd2">
    <td colspan="2" style="text-align: center">
    
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交" /> <input id="Reset1" type="reset" value="清除" /></td>
    </tr>
</table>   【后台代码】:public partial class admin_admin_add : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string username = (string)Session["username"];
        }        DataSet ds = Socut.Data.ExecuteDataSet("select wyx_id,wyx_classname from wyx_class");//实例化DataSet 
        DropDownList1.DataSource = ds;
        DropDownList1.DataTextField = "wyx_classname";
        DropDownList1.DataValueField = "wyx_id";
        DropDownList1.DataBind();       //绑定数据         }
    protected void Button1_Click(object sender, EventArgs e)
    {
                string strDate = System.DateTime.Now.ToString();//当前时间
        Socut.Data.ExecuteNonQuery("INSERT INTO wyx_text (wyx_title,wyx_author,wyx_content,wyx_date,wyx_class) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + strDate + "','" + DropDownList1.SelectedItem.Value + "')");
        Response.Redirect("admin_text.aspx");
    }}

解决方案 »

  1.   

    代码写错了。你那样绑定数据,每次提交服务器都会被重新绑定,导致老是选中第一个。    protected   void   Page_Load(object   sender,   EventArgs   e) 
            { 
                    if   (!Page.IsPostBack) 
                    { 
                            string   username   =   (string)Session["username"]; 
                                   DataSet   ds   =   Socut.Data.ExecuteDataSet("select   wyx_id,wyx_classname   from   wyx_class");//实例化DataSet   
                    DropDownList1.DataSource   =   ds; 
                    DropDownList1.DataTextField   =   "wyx_classname"; 
                    DropDownList1.DataValueField   =   "wyx_id"; 
                    DropDownList1.DataBind();               //绑定数据   
                       }          } 
      

  2.   

    应该把这个去掉吗??? if       (!Page.IsPostBack)   
      

  3.   

    明白了,回头试一下看看,谢谢winner
      

  4.   

    if               (!Page.IsPostBack)
    的作用是判断是否是回传,
    那绑定DropDownList为什么要放在这里面呢。