开发者

list box asp.net selected problem

开发者 https://www.devze.com 2022-12-30 03:36 出处:网络
When I choose am item from \"kategorije\", new items are loaded into \"SUB_kategorije\", but when I choose the it开发者_如何转开发em from SUB_kategorije and when i click on button it shows me this err

When I choose am item from "kategorije", new items are loaded into "SUB_kategorije", but when I choose the it开发者_如何转开发em 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();

This is my source...

dod_pit.ascx

dod_pit.ascx.cs


When I choose am item from "kategorije", new items are not loaded into "SUB_kategorije",Why?

 protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {

            SqlDataSource ds = new SqlDataSource();
            ds.ConnectionString = conn;
            ds.SelectCommand = "SELECT [ID], [Kategorije] FROM [kategorije] ";
            kategorije.DataSource = ds;
            kategorije.DataTextField = "Kategorije";
            kategorije.DataValueField = "ID";
            kategorije.DataBind();
            kategorije.SelectedIndex = 1;

            SqlDataSource dk = new SqlDataSource();
            dk.ConnectionString = conn;
            dk.SelectCommand = "SELECT * from pod_kategorije WHERE kat_id = " + kategorije.SelectedItem.Value;
            SUB_kategorije.DataSource = dk;
            SUB_kategorije.DataTextField = "pkategorija";
            SUB_kategorije.DataValueField = "ID";
            SUB_kategorije.DataBind();


        }







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


            SqlDataSource dk = new SqlDataSource();
            dk.ConnectionString = conn;
            dk.SelectCommand = "SELECT * from pod_kategorije WHERE kat_id = " + kategorije.SelectedItem.Value;
            SUB_kategorije.DataSource = dk;
            SUB_kategorije.DataTextField = "pkategorija";
            SUB_kategorije.DataValueField = "ID";
            SUB_kategorije.DataBind();


    }


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();
    }         
}

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号