.net中动态生成dropdownlist控件后取值问题

今天找了一天的关于 .net中动态生成dropdownlist控件后取值的方法,都不能直接运用,还好看到一个提示是将数据保存到hidden域中的提示。试了下果真有用,太高兴了。

动态生成的dropdownlist 在页面重新加载时视图状态丢失,导致先前的设置都没有任何用途了。所以采取将dropdownlist当前选中的值通过javascript脚本保存在hidden域中的方式,可以避免设置信息丢失。在页面重新加载时将hidden域中的值取出即可。以下是一个名为testDropdown的web窗体测试代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testDropdown.aspx.cs" Inherits="EmployeeInfoReferPurview_files_testDropdown" %>



    无标题页
   


   


   

取下拉框值


   

       
       
       

       

       

       
       

       
            1
            2
            3
            4
       

       
       

   



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class EmployeeInfoReferPurview_files_testDropdown : System.Web.UI.Page
{
    DropDownList[] ddllst = new DropDownList[3];
    ListItem li1 = new ListItem("q", "0");
    ListItem li2 = new ListItem("w", "1");
    ListItem li3= new ListItem("e", "2");
    TextBox[] txtlst = new TextBox[3];
    HiddenField[] hflst = new HiddenField[3];
    HiddenField[] hftextlst = new HiddenField[3];

    protected override void OnInit(EventArgs e)
    {
        //createtable();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        createtable();
        if (!Page.IsPostBack)
        {           
            for (int i = 0; i < 3; i++)
            {
                txtlst[i].ForeColor = Color.Red;
            }           
        }
    }

    protected void createtable()
    {
        for (int i = 0; i < 3; i++)
        {
            TableRow tr = new TableRow();
            tr.ID = "tr"+i;
            TableCell tc = new TableCell();
            tc.ID = "tc" + i;
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i;
            ddl.Items.Add(li1);
            ddl.Items.Add(li2);
            ddl.Items.Add(li3);            //ddl.Items.FindByValue(li2.Value).Selected = true;
           
            HiddenField hf = new HiddenField();
            hf.ID = "hf" + i;
            hf.Value = ddl.SelectedValue;
            HiddenField hftext = new HiddenField();
            hftext.ID = "hftext" + i;
            hftext.Value = ddl.SelectedItem.Text;
            ddl.Attributes.Add("onclick", "javascript:getvalue(hf" + i + ",ddl" + i + ",hftext" + i + ");");
            ddllst[i] = ddl;
            hflst[i] = hf;
            hftextlst[i] = hftext;
            //HiddenField hf = new HiddenField();
            //hf.ID = "hf" + i;
            //hf.Value = ddl.SelectedValue;
            tc.Controls.Add(ddl);
            tc.Controls.Add(hf);
            tc.Controls.Add(hftext);
            tr.Controls.Add(tc);
            tbl.Rows.Add(tr);

            TableRow tr1 = new TableRow();
            tr1.ID = "tr1"+i;
            TableCell tc1 = new TableCell();
            tc1.ID = "tc1" + i;
            TextBox txt = new TextBox();
            txt.ID = "txt" + i;
            //txt.ForeColor = Color.Red;
            txtlst[i] = txt;
            tc1.Controls.Add(txt);
            tr1.Controls.Add(tc1);
            tbl.Rows.Add(tr1);
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {               
        //int inta = ddllst.Length;
        //string valid=((DropDownList)e.Item.FindControl("ddlValid")).SelectedValue;
        //string a = ddllst[0].SelectedValue;
        //string b = ddllst[1].SelectedValue;
        //string c = ddllst[2].SelectedValue;           
        //string d = ddlq.SelectedValue;
        //txtq.Text = a + "-" + b +"-"+ c +"-"+d;
        for (int i = 0; i < 3; i++)
        {
            //string a = ((DropDownList)tbl.FindControl(ddllst[i].ID)).SelectedValue;
            string b = hflst[i].Value +"-"+hftextlst[i];
            txtq.Text += b + "//";
            //txtq.Text += a+" "+b+"//";
        }
    }
}

读了微软的文章(http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx?mfr=true)感觉说的不对,在页面加载时比如textbox控件,在每次加载后依然可以保存所有原来的输入信息的(字体颜色也保存)。

你可能感兴趣的:(.net技术类)