2017-03-07 36 views
0

我正在從事我需要從我的asp下拉列表中選擇來自MySql數據庫的值的項目,如下所示:Mysql table下拉列表來自哪裏。無法在ASP.NET中的Dropdownlist中獲得正確的值C#

這是我的UI看起來像:Selecting Value which came from Database

這裏是我的ASPX文件:

<%@ Page Title="" Language="C#" MasterPageFile="~/MASTER_USER.master" AutoEventWireup="true" CodeFile="Ask_user.aspx.cs" Inherits="Ask_user" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
<div class="content-wrapper"> 
<div class="container"> 

<form class="form-horizontal" > 
<fieldset> 

<!-- Form Name --> 
<h1 align=center>ASK YOUR QUESTION!</h1> 



<!-- Multiple Radios --> 
<div class="form-group space"> 
    <label style="text-align:right;" class="col-md-4 control-label">Category</label> 
    <div class="col-md-4"> 
     <asp:DropDownList ID="DropDownList1" runat="server" CssClass="form-control" AutoPostBack="true"> 
     <asp:ListItem Value="0">Topics</asp:ListItem> 
     </asp:DropDownList> 
    </div><br /> 

</div> 

<label style="text-align:right;" class="col-md-4 control-label">Sub Category</label> 
    <div class="col-md-4"> 
     <asp:DropDownList ID="DropDownList2" runat="server" CssClass="form-control"> 
      <asp:ListItem Value="0">Sub topics</asp:ListItem> 
     </asp:DropDownList> 
    </div><br /> 

</div> 
<!-- Text input--> 

<div class="form-group space"> 
    <label style="text-align:right;" class="col-md-4 control-label" for="Title ">Title</label> 
    <div class="col-md-4"> 
    <input id="title" name="title" runat="server" placeholder="What is your question?" class="form-control input-md" type="text"> 

    </div><br /> 
</div> 

<!-- Textarea --> 
<div class="form-group space"> 
    <label style="text-align:right;" class="col-md-4 control-label" for="Disc">Description</label> 
    <div class="col-md-4">      
    <textarea class="form-control" id="Disc" runat="server" name="Disc" placeholder="A brief description about your question..."></textarea> 
    <br /> 


    <asp:Button ID="btn_ask" CssClass="btn btn-primary" runat="server" Text="ASK!" 
        OnClick="btn_insert_Click" /> 
        <span id="msg" ></span> 
    </div> 
</div> 



</fieldset> 
</form> 
</div> 
</div> 
</asp:Content> 

這裏是我的CS文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using System.Data.SqlClient; 
using MySql.Data.MySqlClient; 
using System.Data; 
using System.Collections; 
using System.Web.UI.HtmlControls; 
using System.Web.SessionState; 

public partial class Ask_user : System.Web.UI.Page 
{ 
    dbconnection con = new dbconnection("connect_to_mysql"); 
    string constr = ConfigurationManager.ConnectionStrings["connect_to_mysql"].ConnectionString; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 
      string qry = "select cat_nm, cat_id from category"; 
      DataTable dt = dbconnection.ExecuteSelect(qry); 
      DropDownList1.DataSource = dt; 
      DropDownList1.DataTextField = "cat_nm"; 
      DropDownList1.DataValueField = "cat_id"; 
      DropDownList1.DataBind(); 
     } 
     else 
     { 


      string catval = DropDownList1.SelectedValue; 
      string qry = "select * from category_sub where parent_id = " + catval; 
      DataTable dt = dbconnection.ExecuteSelect(qry); 
      DropDownList2.DataSource = dt; 
      DropDownList2.DataTextField = "cat_nm"; 
      DropDownList2.DataValueField = "cat_sub"; 
      DropDownList2.DataBind();  


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


     string cat2 = DropDownList2.SelectedValue; 

     int sb = (Convert.ToInt32(Convert.ToInt32(cat2))); 
     string cat = DropDownList1.SelectedItem.Text; 
     string title1 = title.Value.ToString(); 
     string disc = Disc.Value.ToString(); 

     string uid = (Session["uid"].ToString()); 

     string query = @"insert into question(cat_sub,catg,question,q_dis,user_id)values('" + sb + "','" + cat + "','" + title1.Replace("'", "''") + "','" + disc.Replace("'", "''") + "','" + uid + "')"; 

     dbconnection.ExecuteInsert(query); 

     string info = "Success"; 
     ClientScript.RegisterStartupScript(typeof(Page), "alert", "$('#msg').addClass('alert alert-success');$('#msg').text('" + info + "');",true); 


    } 
} 

這裏是我的問題cat_sub該值總是來11而不是12均同上甲骨文/ mysql的值是8,而不是它的cat_sub

調試值即將到來時只是從那裏的值開始。以下是一些信息:https://i.stack.imgur.com/3lcq2.png

+2

你可以直接將代碼添加到您的問題,而不是張貼在引擎收錄的。 – lenny

+0

預期的工作是什麼?即使從子類別下拉列表中選擇「切換」,我只計算11個項目而不是12個提到的 –

+0

cat_sub的值爲「12」? –

回答

0

你的孩子cateogry綁定代碼也運行,當按鈕點擊並刷新選定的childcategory之前,按鈕代碼事件。

將其從Page_Load中刪除。

SelectedIndexChanged事件添加到DropDownList1

OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 

和處理..

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string catval = DropDownList1.SelectedValue; 
     string qry = "select * from category_sub where parent_id = " + catval; 
     DataTable dt = dbconnection.ExecuteSelect(qry); 
     DropDownList2.DataSource = dt; 
     DropDownList2.DataTextField = "cat_nm"; 
     DropDownList2.DataValueField = "cat_sub"; 
     DropDownList2.DataBind(); 
    } 
+0

仍然沒有值12 ...只有第一個顯示。 –

相關問題