如何在页面加载时将DropDownList值存储到变量中? [英] How to store DropDownList values into variables at page load ?

查看:58
本文介绍了如何在页面加载时将DropDownList值存储到变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将以下项目存储在DropDownList中:

123

124

125

126

如何将这些下拉列表项存储到Form加载中的变量中,而不选择任何项目:

例如:

int a = dropdownlist1 [0],

int b = dropdownlist1 [1],

int c = dropdownlist1 [2],

int d = dropdownlist1 [3],

如何使用C#在页面加载时动态发生这种情况?请帮帮我..

If I store the following items in a DropDownList:
123
124
125
126
how can I store these dropdown list items into variables like below at Form load without selecting any of the items:
For example:
int a=dropdownlist1[0],
int b=dropdownlist1[1],
int c=dropdownlist1[2],
int d=dropdownlist1[3],
How can this happen dynamically at page load time using C# ? please help me..

推荐答案

我建​​议使用数组,因为它会消耗比单个变量更少的内存空间。您可以将所有值存储在数组中,如下所示。



您将要使用的下拉列表,应该在使用前绑定(应填写)下面的代码



I suggest to use array, as it will consume less memory space than the individual variables. You can store all the values in the array, As i have shown below.

Dropdown that you are going to use, should be bind (Should be filled) before using below code

string[] arrItems = new string[dropdown.Items.Count];
    if (dropdown.Items.Count > 0)
    {
        for (int i = 0; i < dropdown.Items.Count; i++)
        {
            arrItems[i] = dropdown.Items[i].ToString();
        }
    }





希望这会对你有帮助。



Hope this will help you.


你好朋友,

好​​的,据我所知你需要遍历一组数据源项并将它们存储到一个单独的变量中进行进一步分析:



我同意RyanDev然而我会告诉你代码:



我将直接从后面的代码开始,因为你可以声明一个下拉列表aspx。

请注意我使用的是程序化数据绑定而非声明:



我用于下拉列表的ID是
Hello Friend,
Ok so by what i understand you need to iterate through a collection of datasource items and store them into a separate variable for further analysis:

I agree with RyanDev however i will show you the code:

I will start with the code behind directly, because you can declare a drop down list in the aspx.
Please note i am using programatic databinding and not declarative:

Id used by me for the dropdownlist is
引用:

drop

因此我将使用该名称来引用下拉列表控件。



hence i will use that name to refer the drop down list control.

protected void Page_Load(object sender, EventArgs e)
  {

      if (!IsPostBack)      ////this is not required in this scenario......
      {
      drop.DataSource = new List<int> { 0, 1, 2, 3, 4 }; ///i have assigned an array of ints to the datasource.
          drop.DataBind();       /// bind it to the control
          int[] array = new int[drop.Items.Count]; ///i have used an array to store the items of the collection.

          foreach(ListItem item in drop.Items) // iterate through all items
          {
              array[n] = int.Parse(item.Value); /// start storing
              Response.Write("Item:" + n + "  Value :" + array[n].ToString() + ",");///print the data in the format Item:0 Value:0
              n++;
          }
      }
  }





这将有效。

我有一些疑问:

为什么你需要一个单独的变量来存储集合中的每个项目,因为那时你将需要运行时那么多变量。 ...

间接地,我想说使用数组。



但是我的代码编写得非常糟糕,它非常冗余。没有必要使用数组来存储项目。



我可以使用DropDownList集合的Items属性来访问数据,不需要使用单独的变量,也不需要使用数组。我可以直接浏览物品的集合。



以上所有内容都是动态的,如你所说,

此代码应该符合你的目的。如果它没有运行,请告诉我。



非常感谢,

Rahul



This will work.
I have some of my doubts:
why do you need a seperate variable to store every item in the collection, because then you will be needing those many variables at runtime....
Indirectly, i want to say use an array.

However my code is very badly written, its very redundant. There was no need to take an array to store items.

I can use the Items property of the DropDownList collection to access the data, no need to use separate variables, no use of an array. I can directly traverse through the collection of Items.

Every thing above is dynamic as you said,
This code however should serve your purpose. Please let me know if it doesn't run.

Thanks a ton,
Rahul


这篇关于如何在页面加载时将DropDownList值存储到变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆