asp.net列表框

时间:2010-05-07 23:00:44

标签: c# asp.net asp.net-2.0

为什么这段代码不起作用?当我运行它时,VWD Express会显示以下错误:

  

对象引用未设置为对象的实例。

     

第73行:kom.Parameters.Add(“@ subcategories”,SqlDbType.Text).Value = s_categoreis.SelectedItem.ToString();

这是我的ascx文件:

 <asp:ListBox ID="categories" runat="server" Height="380px" CssClass="kat" AutoPostBack="true" DataSourceID="SqlDataSource1" 
        DataTextField="Categories" DataValueField="ID" 
        onselectedindexchanged="kategorije_SelectedIndexChanged"></asp:ListBox>


     <asp:Button ID="Button1" CssClass="my" runat="server" Text="click" 
            onclick="Button1_Click" />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate> <asp:ListBox ID="s_categoreis" CssClass="pod" Height="150px" Enabled="true" runat="server"></asp:ListBox></ContentTemplate>
           <Triggers>
           <asp:AsyncPostBackTrigger ControlID="categories" EventName="SelectedIndexChanged" />

           </Triggers>
        </asp:UpdatePanel>


    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:estudent_piooConnectionString %>" 
        SelectCommand="SELECT [ID], [Categories] FROM [categories]">
    </asp:SqlDataSource>

这是我的ascx.cs:

 SqlConnection veza;
    SqlCommand kom = new SqlCommand();
    SqlParameter par1 = new SqlParameter();
    SqlParameter par2 = new SqlParameter();
    SqlParameter par3 = new SqlParameter();
    SqlParameter par4 = new SqlParameter();
    SqlParameter par5 = new SqlParameter();
    SqlParameter par6 = new SqlParameter();
    SqlParameter par7 = new SqlParameter();
    SqlParameter par8 = new SqlParameter();
    SqlParameter par9 = new SqlParameter();

    protected void Page_Load(object sender, EventArgs e)
    {                          
            Listapod_kategorije(1);            
    }

    protected void kategorije_SelectedIndexChanged(object sender, EventArgs e)
    {
        Listapod_kategorije(Convert.ToInt32(kategorije.SelectedValue));    
    }

    private void Listapod_kategorije(int broj) {

        SqlDataSource ds = new SqlDataSource();
        ds.ConnectionString = ConfigurationManager.ConnectionStrings["estudent_piooConnectionString"].ConnectionString;
        ds.SelectCommand = "Select * from pod_kategorije where kat_id=" + broj;
        pod_kategorije.DataSource = ds;
        pod_kategorije.DataTextField = "pkategorija";
        pod_kategorije.DataValueField = "ID";
        pod_kategorije.DataBind();                
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Guid jk = new Guid();

        object datum = DateTime.Now;
        veza = new SqlConnection(@"...");

            Random broj = new Random();
            int b_kor = broj.Next(1, 1000);
            kom.Parameters.Add("@text", SqlDbType.Text).Value = str;
            kom.Parameters.Add("@user", SqlDbType.UniqueIdentifier).Value = jk;

            kom.Parameters.Add("@date", SqlDbType.DateTime).Value = datum;
            kom.Parameters.Add("@visits", SqlDbType.Int).Value = 0;
            kom.Parameters.Add("@answers", SqlDbType.Int).Value = 0;
            kom.Parameters.Add("@username", SqlDbType.Text).Value = "unknown_" + b_kor.ToString(); ;
            kom.Parameters.Add("@categories", SqlDbType.Text).Value = categories.SelectedItem.ToString();
            kom.Parameters.Add("@sub_categories", SqlDbType.Text).Value = s_categoreis.SelectedItem.ToString();
            veza.Open();
            kom.ExecuteNonQuery();
            veza.Close();
            Response.Redirect("default.aspx");

4 个答案:

答案 0 :(得分:2)

我无法分辨出s_categoreis在您的代码中绑定的位置。我的猜测是你在回帖后绑定列表框的DataSource,这会导致你点击按钮后所做的任何选择都会丢失。

确保在绑定列表框之前检查页面是否已回发,如下所示:

if (!IsPostBack)
{
  //bind s_categoreis here
}

答案 1 :(得分:0)

我猜测s_categoreis.SelectedItem属性为null。这可能是由于列表框没有选定的值。

您可以使用调试器并逐步执行代码,将鼠标悬停在标识符上以及使用监视/快速监视/即时窗口来测试其中的一部分。

答案 2 :(得分:0)

SelectedItem是一个ListItem对象,你应该改变

s_categoreis.SelectedItem.ToString();

要么:

s_categoreis.SelectedItem.Text;

或:

s_categoreis.SelectedItem.Value;

答案 3 :(得分:0)

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:estudent_piooConnectionString %>" 
    SelectCommand="SELECT [ID], [Categories] FROM [categories]">
</asp:SqlDataSource>