C# 在ASP.NET中创建动态下拉列表后回发时无法存储信息

C# 在ASP.NET中创建动态下拉列表后回发时无法存储信息,c#,asp.net,C#,Asp.net,这是我的密码 在按钮2单击事件之后,它将在表行中创建下拉列表,但当我尝试通过按钮1单击事件保存时,它将消失。我还没有找到任何解决办法。我使用了查找控件视图状态等,但它没有帮助 我想在按钮单击事件开始后存储下拉列表的选定值 public partial class StudentClassSectionMapping : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e)

这是我的密码

在按钮2单击事件之后,它将在表行中创建下拉列表,但当我尝试通过按钮1单击事件保存时,它将消失。我还没有找到任何解决办法。我使用了查找控件视图状态等,但它没有帮助

我想在按钮单击事件开始后存储下拉列表的选定值

public partial class StudentClassSectionMapping : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ClassCode.Enabled = false;

        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        UpdateModalShowFlag.Value = "true";
        Check.Value = "true";
        CreateTableRows();
    }

    private void CreateTableRows()
    {

        long h = long.Parse(LinkButtonIdCarrier.Value);
        List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allStudentsInClass = StudentsClassSectionMapping.GetStudentsinClass(h);
        ClassMaster.ClassMasterForm classCode = Schoolclasses.GetInfo(h);
        ClassCode.Text = classCode.cCode;
        List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allSectionsInClass = StudentsClassSectionMapping.GetSectionsinClass(h);
        foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList in allStudentsInClass)
        {
            TableRow row = new TableRow();
            TableCell cell1 = new TableCell();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            DropDownList t = new DropDownList();
            t.Items.Add("No Section");
            foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm sectionList in allSectionsInClass)
            {
                t.Items.Add(sectionList.ssSection);
                t.Items[t.Items.Count - 1].Value = sectionList.ssSectionID.ToString();
            }
            t.Attributes.Add("class", "form-control");

            t.ID = studentList.ssStudentId.ToString();
            cell1.Text = studentList.ssName;
            cell2.Text = studentList.ssRegistrationNumber;
            cell3.Controls.Add(t);
            row.Cells.Add(cell1);
            row.Cells.Add(cell2);
            row.Cells.Add(cell3);
            Table1.Rows.Add(row);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateTableRows();
        long h = long.Parse(LinkButtonIdCarrier.Value);
        List<StudentsClassSectionMapping.StudentsClassSectionMappingForm> allStudentsInClass = StudentsClassSectionMapping.GetStudentsinClass(h);
        foreach (StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList in allStudentsInClass)
        {

            DropDownList d = Table1.FindControl(studentList.ssStudentId.ToString()) as DropDownList;
            if (d != null)
            {


                if (d.SelectedIndex != 0)
                {
                    StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping = new StudentsClassSectionMapping.StudentsClassSectionMappingForm();
                    studentSectionMapping.ssClassId = h;
                    studentSectionMapping.ssStudentId = studentList.ssStudentId;
                    studentSectionMapping.ssStudentId = long.Parse(d.SelectedItem.Value);
                    StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
                }
                else 
                {
                    StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping = new StudentsClassSectionMapping.StudentsClassSectionMappingForm();
                    studentSectionMapping.ssClassId = h;
                    studentSectionMapping.ssStudentId = 0;
                    studentSectionMapping.ssStudentId = 0;
                    StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
                }
            }
        }
    }
public分部类StudentClassSectionMapping:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
ClassCode.Enabled=false;
}
}
受保护的无效按钮2\u单击(对象发送者,事件参数e)
{
updateModelShowFlag.Value=“true”;
Check.Value=“true”;
CreateTableRows();
}
私有void CreateTableRows()
{
long h=long.Parse(LinkButtonIdCarrier.Value);
列出allStudentsInClass=StudentsClassSectionMapping.GetStudentsinClass(h);
ClassMaster.ClassMasterForm classCode=学校班级.GetInfo(h);
ClassCode.Text=ClassCode.cCode;
列出allSectionsInClass=StudentsClassSectionMapping.GetSectionsinClass(h);
foreach(StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList在所有StudentsInClass中)
{
TableRow行=新TableRow();
TableCell cell1=新的TableCell();
TableCell cell2=新的TableCell();
TableCell cell3=新的TableCell();
DropDownList t=新的DropDownList();
t、 项目。添加(“无章节”);
foreach(StudentsClassSectionMapping.StudentsClassSectionMapping表格所有节类中的节列表)
{
t、 项目。添加(章节列表。ssSection);
t、 Items[t.Items.Count-1].Value=sectionList.ssSectionID.ToString();
}
t、 添加(“类”、“窗体控件”);
t、 ID=studentList.ssStudentId.ToString();
cell1.Text=studentList.ssName;
cell2.Text=studentList.ssRegistrationNumber;
细胞3。对照。添加(t);
row.Cells.Add(cell1);
行。单元格。添加(单元格2);
行。单元格。添加(单元格3);
表1.行。添加(行);
}
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
CreateTableRows();
long h=long.Parse(LinkButtonIdCarrier.Value);
列出allStudentsInClass=StudentsClassSectionMapping.GetStudentsinClass(h);
foreach(StudentsClassSectionMapping.StudentsClassSectionMappingForm studentList在所有StudentsInClass中)
{
DropDownList d=Table1.FindControl(studentList.ssStudentId.ToString())作为DropDownList;
如果(d!=null)
{
如果(d.SelectedIndex!=0)
{
StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping=新建StudentsClassSectionMapping.StudentsClassSectionMappingForm();
studentSectionMapping.ssClassId=h;
studentSectionMapping.ssStudentId=studentList.ssStudentId;
studentSectionMapping.ssStudentId=long.Parse(d.SelectedItem.Value);
StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
}
其他的
{
StudentsClassSectionMapping.StudentsClassSectionMappingForm studentSectionMapping=新建StudentsClassSectionMapping.StudentsClassSectionMappingForm();
studentSectionMapping.ssClassId=h;
studentSectionMapping.ssStudentId=0;
studentSectionMapping.ssStudentId=0;
StudentsClassSectionMapping.addSectionStudentMapping(studentSectionMapping);
}
}
}
}

它会消失/消失,因为您在页面上动态添加了它。如果您想要恢复它或想要保留动态创建的服务器控件,则需要重新创建并动态添加

下面是一个很好的例子,说明了如何做到这一点: