简体   繁体   中英

How to put a label into a checkboxlist from another page for asp.net

I tried doing this on the check box list page:

protected void Page_Load(object sender, EventArgs e)
{
    (string)Session["name"] = cbl1.Items.Add;
}

but I don't know how to make it work.

You don't really "put a label into" a check box list from another page.

You might say have a check box list on one page, and you want to pass the selection(s) made in the first page, and pass to the 2nd page.

So, say we have a simple check box list of options on page 1

And a button to jump to the next page.

This markup:

        <h3>Hotel Options</h3>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            DataValueField ="ID"
            DataTextField="HotelOption" >
        </asp:CheckBoxList>
        <br />
        <br />

        <asp:Button ID="Button1" runat="server" 
            Text="Continue to next page" CssClass="btn" OnClick="Button1_Click" />

And code to load this up:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadCheckBox();
    }

    void LoadCheckBox()
    {
        DataTable rstOptions = 
            MyRst("SELECT ID, HotelOption FROM tblOptions ORDER BY ID");

        CheckBoxList1.DataSource = rstOptions;
        CheckBoxList1.DataBind();

    }

    DataTable MyRst(string strSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.Hotels))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                cmdSQL.Connection.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }

And we now have this:

在此处输入图像描述

Ok, so now our code for the button.

We will get + save the above selections, and pass to the next page:

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["MyItems"] = CheckBoxList1.Items;
        Response.Redirect("WebForm2.aspx");
    }

Ok, now our markup for the 2nd page.

We have this:

        <h3>Hotel Options - on next page</h3>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"
            DataValueField ="Value"
            DataTextField="Text" >
        </asp:CheckBoxList>
        <br />

Now, we saved the "items list" for the Check box list. The item list has a text and value property (we LOSE the colum name information that the data sourced used was).

So, note how the DataValueField="value" and DataTextField="Text" now.

So, this code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadCheckBox();
    }

    void LoadCheckBox()
    {
        ListItemCollection MyItems = (ListItemCollection)Session["MyItems"];

        foreach (ListItem OneItem in MyItems)
        {
            CheckBoxList1.Items.Add(OneItem);
        }
    }

And now on 2nd page we have this:

在此处输入图像描述

Now it is NOT clear why I can't just bind this passed list directly to the check box list, but I found a foreach loop is required.

Now, it is perhaps not clear, but maybe you ONLY wanted the label text ones selected.

You can do this with this code:

        ListItemCollection MyItems = (ListItemCollection)Session["MyItems"];

        foreach(ListItem OneItem in MyItems)
        {
            if (OneItem.Selected)
            {
                Debug.Print("Label text = " + OneItem.Text);
                Debug.Print("Check box value = " + OneItem.Value);
            }
        }

Output:

Label text = Smoking
Check box value = 1
Label text = Balcony
Check box value = 2

So, keep in mind that a check box list can often have 2 columns of data.

The display "label" or text, or the hidden value.

In my example, the "ID" is the PK value from the database, and I need that.

If you only have one "text" value, and don't care about a hidden database PK value, then often I just set both DataTextField and DataValueField to the SAME colum in the database.

And if you using looping, and adding a listitem in code? Then the settings are not the field columns anymore, but Text and Value. You thus still have the ability to have 2 values - the display text, and some key or number value for a checkbox list.

Now perhaps you have two web pages open, and you want to change/set controls in the other web page?

No, you can't do that. Other web pages might say be open to someone's banking web site. You can't mess around with other web pages the user has open, since that would be a MASSIVE security hole, and no one would risk using the world wide web then, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM