ASP 多选列表框分隔符

发布于 2024-12-05 12:57:20 字数 1244 浏览 1 评论 0原文

我遇到了一个问题,但我还没有找到任何灵魂。让我简单一点。

我有 2 个表单,第一个包含一个启用了多选模式的 ASP 列表框。我提交了表单,在另一种表单中,我仅将这段代码用于测试目的:

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];
            }
        }  
    }

问题是来自列表框的值(我在上一个表单中选择的值)由“,”分隔,例如。 “测试1,测试2,测试3”。例如,如何将此分隔符更改为“$”?我需要更改它,因为实际值可能包含“,”,并且我不会手动将它们提供给列表框。

我无法使用任何其他模式在表单之间传输此值,因为整个应用程序都使用此模型。然后,我得到的值被发送到将被操纵的工作流程,并且在工作流程中,我需要知道每个列表框项目的开始和结束位置,因此它必须是唯一的分隔符。

任何帮助表示赞赏!非常感谢,


谢谢 MatteKarla,但不幸的是,这并不能解决我的问题。是的,这是将值从一种形式转移到另一种形式的好方法。

但是,我必须将上面描述的方法与请求表单键一起使用,因为列表框是在运行时生成的许多其他“参数”之一,并将它们的值发送到采用该值的工作流方法。我无法在我的应用程序中更改这一点。

我的问题是逗号(“”)分隔符默认与多选列表框一起使用。

我认为也许有一种方法可以将该分隔符从逗号更改为另一个字符,因为逗号也可以包含在值本身中,这会造成混乱。

正如我所说,如果我选择三个值 test1test2test3,我的方法的结果将是一个看起来像 的字符串”测试1,测试2,测试3”。然而 "test1$test2$test3" 会好得多。

但我担心更改此默认分隔符是不可能的。我必须考虑一种方法来克服这个问题,例如在将值提供给列表框之前将所有预期的逗号替换为其他字符,以免造成混乱。但这并不是一个好方法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2024-12-12 12:57:20

在您的第一页/表单 (First.aspx.cs) 上,使用列表框创建一个公共属性:

public ListBox PostedListBox { get { return ListBox1; } }

将按钮的回发 url 设置为 Second.aspx

aspx 文件中 < 后的第二页code>@Page-指令添加:

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

然后在 Second.aspx.cs 上的 Form_Load 中,您可以提取值:

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

或者您可以尝试找到该控件通过使用:

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

第三次编辑:

你可以使用 GetValues:

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

返回包含每个选定项目的字符串数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文