开发者

ASP Multiselect listbox separator

开发者 https://www.devze.com 2023-04-05 19:30 出处:网络
I have encountered a problem and I didn\'t manage to find any soultions yet. Let me simplify things a bit.

I have encountered a problem and I didn't manage to find any soultions yet. Let me simplify things a bit.

I have 2 forms, the first contains an ASP ListBox with multi select mode enabled. I submit the form and in the other form I use just for testing purposes this snippet of code:

protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string formKey in Request.Form.AllKeys)
        {
            if (formKey != null)
            {
开发者_运维知识库                if (formKey.Equals("ctl00$MainContent$ListBox1"))
                    Label1.Text = Request.Form[formKey];
            }
        }  
    }

The problems is that the values that come from the listbox (the values that i selected in the previous form) are separated by "," for ex. "test1,test2,test3". How can i change this separator to "$" for example? I need to change it because the actual values may contain "," and i don't manualy feed them to the listbox.

I can't use any other mode of transfering this values between the form because the entire application uses this model. The values that i get are then sent to a workflow where there will be manipulated and in the workflow i need to know where each listbox item starts and ends so it must be an unique separator.

Any help is apreciated! Thank you very much


Thank you MatteKarla but unfortunately this does not solve my problem. Yes, this is a good way of transfering the values from one form to another.

However i must use the method I described above with Request form keys because the listbox is one of many others "parameters" that are generated at runtime and have their values sent to a workflow method that takes this values. And i can't afford to change that in my application.

My problem is that coma (",") separator is used by default with a multiselect listbox.

I thought that there maybe is a method to change that separator from coma to another char because the coma can also be included in the value itself and this will create confusion.

As i said if i select three values test1, test2 and test3, the result with my method will be a string looking like "test1,test2,test3". However a "test1$test2$test3" would be much better.

But I'm affraid that changing this default separator is not possbile. I must think at a method to overcome this problem like replacing before feeding the listbox all the intended coma from the values with some other char not to create confusion. But this is not a great way of doing it.


On your first page/form (First.aspx.cs) create a public property with the listbox:

public ListBox PostedListBox { get { return ListBox1; } }

Set the postback-url for the button to Second.aspx

Second page in the aspx-file after the @Page-directive add:

<%@ PreviousPageType VirtualPath="~/First.aspx" %>

Then in Form_Load on Second.aspx.cs you can extract the values:

if (PreviousPage != null)
{
    ListBox postedListbox = PreviousPage.PostedListBox;
    foreach (var index in postedListbox.GetSelectedIndices())
    {
        var itemText = postedListbox.Items[index].Text;
    }
}

Or you could just try to locate the control by using:

if (PreviousPage != null)
{
  var control = PreviousPage.FindControl("ListBox1") as ListBox;
}

Third Edit:

You could use GetValues:

Request.Form.GetValues("ctl00$MainContent$ListBox1");

returns a string array containing each of the selected items.

0

精彩评论

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

关注公众号