Listbox in asp.net not getting selected items

前端 未结 5 1065
花落未央
花落未央 2021-01-14 10:41

I have multiple dropdown & listbox in my webpage.

I am trying to get a list of CategoryID from a lstCatID listbox i am able to populate

5条回答
  •  不知归路
    2021-01-14 11:25

    Try to add Page.IsPostback on your Page_Load like

    protected void Page_Load(object sender, EventArgs e)
    {
        // Do your API code here unless you want it to occur only the first
        // time the page loads, in which case put it in the IF statement below.
        if (!Page.IsPostBack)
        {
    
        }
    }
    

    Code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string CatID = string.Empty;
        foreach (ListItem li in lstCatID.Items)
        {
            if (li.Selected )
            {
               // TODO: Whatever you are doing with a selected item.
            }
        }
        Response.Write(CatID);
    }
    

    Once i was facing the same problem and i made Postback mistake.

    Hope it works.

提交回复
热议问题