本文介绍了列表框中选择asp.net问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从kategorije选择上午项目,新项目被装载到SUB_kategorije,但是当我选择SUB_kategorije项目,当我点击链接它表明我这个错误:

When I choose am item from "kategorije", new items are loaded into "SUB_kategorije", but when I choose the item from SUB_kategorije and when i click on button it shows me this error:

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

Object reference not set to an instance of an object.

Line 101: kom.Parameters.Add("@podkategorija", SqlDbType.Text).Value =
          SUB_kategorije.SelectedItem.ToString();

这是我的源...

推荐答案

问题是你的的Page_Load 。将其更改为:

The problem is your Page_Load. Change it to:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlDataSource df = new SqlDataSource();
        df.ConnectionString = conn;
        df.SelectCommand = "SELECT [ID], [Kategorije] FROM [kategorije] ";
        kategorije.DataSource = df;
        kategorije.DataTextField = "Kategorije";
        kategorije.DataValueField = "ID";
        kategorije.DataBind();
    }
}

问题是你重新绑定 kategorije 每次你的code做了回发时间,所以在时间的button1_Click 运行时, kategorije 已重新绑定和重置,让的SelectedValue 为null。

The problem is your rebinding kategorije every time your code does a postback so by the time Button1_Click runs, kategorije has been rebinding and reset so the SelectedValue is null.

当回传发生在的Page_Load code第一次运行,那么的button1_Click 之后运行。移动该code到!的IsPostBack 检查将导致它只能运行在第一时间你的页面加载,而不是之后再次让的SelectedValue 将可用。

When a postback happens your Page_Load code runs first, then the Button1_Click runs right after. Moving that code into a !IsPostBack check will cause it to only run the first time your page loads and not again after so the SelectedValue will be available.

这篇关于列表框中选择asp.net问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 20:11