问题描述
我在网页上做了几个动态生成的下拉菜单,可是当我选择好下拉菜单中的值,再想提交到数据库时,总是取到第一个值,请问怎么解决?部分代码:staticDropDownList[,]ddl;for(inti=0;i<numOfUsers;i++){for(intj=i+1;j<numOfUsers;j++){ddl[i,j]=newDropDownList();ddl[i,j].Items.Add(newListItem("次要","0.25"));ddl[i,j].Items.Add(newListItem("较次要","0.5"));ddl[i,j].Items.Add(newListItem("相当","1"));ddl[i,j].Items.Add(newListItem("较重要","2"));ddl[i,j].Items.Add(newListItem("重要","4"));}}我在页面上选择以后,用ddl[i,j].SelectedValue或者SelectedItem.Text都只是取道第一个值,为什么啊??
解决方案
解决方案二:
代码放在if(!Page.IsPostBack){//放这里面}
解决方案三:
访问我的博客程序员日记
解决方案四:
引用1楼yangqidong的回复:
代码放在if(!Page.IsPostBack){//放这里面}
是asp。net的,应改是这个原因!
解决方案五:
for那段我是放在!IsPostBack里面的,很奇怪啊
解决方案六:
publicpartialclassGroupSubmit:System.Web.UI.Page{DataBasedb=newDataBase();staticintnumOfUsers;staticstring[]userId;staticDropDownList[,]ddl;staticstringtable;stringgid="1";protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack){db.Open();//填充实验选择菜单stringselectProject="selectPno,PnamefromProject";SqlDataReaderprojectReader=db.SelectReader(selectProject);while(projectReader.Read()){SelectProject.Items.Add(newListItem("实验"+projectReader[0]+""+projectReader[1],projectReader[0].ToString()));}projectReader.Close();//读取小组人数stringrNumOfUsers="selectcount(*)fromGroupMemberwhereGno='"+gid+"'";numOfUsers=Convert.ToInt32(db.SelectResult(rNumOfUsers));//读取小组成员学号stringrUserId="selectuserIdfromGroupMemberwhereGno='"+gid+"'";SqlDataReaderuseridReader=db.SelectReader(rUserId);userId=newstring[numOfUsers];for(inti=0;i<numOfUsers;i++){useridReader.Read();userId[i]=useridReader[0].ToString();}useridReader.Close();db.Close();ddl=newDropDownList[numOfUsers,numOfUsers];for(inti=0;i<numOfUsers;i++){for(intj=i+1;j<numOfUsers;j++){ddl[i,j]=newDropDownList();ddl[i,j].Items.Add(newListItem("次要","0.25"));ddl[i,j].Items.Add(newListItem("较次要","0.5"));ddl[i,j].Items.Add(newListItem("相当","1"));ddl[i,j].Items.Add(newListItem("较重要","2"));ddl[i,j].Items.Add(newListItem("重要","4"));}}DrawTable();//Consistency();}//db.Close();}protectedvoidDrawTable(){stringisCommited="";switch(SelectPart.Text){case"Coding":table="User_Project_Coding";isCommited="selectCodingCmmtfromGroup_Project_MarkwhereGno='"+gid+"'andPno='"+SelectProject.SelectedValue+"'";break;case"Testing":table="User_Project_Testing";isCommited="selectTestingCmmtfromGroup_Project_MarkwhereGno='"+gid+"'andPno='"+SelectProject.SelectedValue+"'";break;case"Report":table="User_Project_Report";isCommited="selectReportCmmtfromGroup_Project_MarkwhereGno='"+gid+"'andPno='"+SelectProject.SelectedValue+"'";break;}db.Open();intcmmtValue=Convert.ToInt32(db.SelectResult(isCommited));if(cmmtValue==0){stat.Text="状态:尚未提交";Button1.Enabled=true;}elseif(cmmtValue==1){stat.Text="状态:已提交,可修改";Button1.Enabled=true;}elseif(cmmtValue==2){stat.Text="状态:已提交,不可修改";Button1.Enabled=false;}//第一行TableRowfirstRow=newTableRow();MatrixTable.Rows.Add(firstRow);//第一行第一个单元格TableCellfirstRowFirstCell=newTableCell();firstRow.Cells.Add(firstRowFirstCell);for(intcellCtr=0;cellCtr<numOfUsers;cellCtr++){//第一行其他单元格TableCellfirstRowOtherCell=newTableCell();firstRowOtherCell.Text=userId[cellCtr];firstRow.Cells.Add(firstRowOtherCell);}for(introwCtr=0;rowCtr<numOfUsers;rowCtr++){//其他几行TableRowotherRow=newTableRow();MatrixTable.Rows.Add(otherRow);TableCellotherRowFirstCell=newTableCell();otherRowFirstCell.Text=userId[rowCtr];otherRow.Cells.Add(otherRowFirstCell);for(intcellCtr=0;cellCtr<numOfUsers;cellCtr++){TableCellotherRowOtherCell=newTableCell();if(cellCtr>rowCtr){if(cmmtValue==1){//已提交,教师未确认的情况stringrtnResult="selectresultfrom"+table+"whereuserIdA='"+userId[rowCtr]+"'anduserIdB='"+userId[cellCtr]+"'";doublertn=Convert.ToDouble(db.SelectResult(rtnResult));if(rtn==0.25){ddl[rowCtr,cellCtr].SelectedValue="0.25";}elseif(rtn==0.5){ddl[rowCtr,cellCtr].SelectedValue="0.5";}elseif(rtn==1){ddl[rowCtr,cellCtr].SelectedValue="1";}elseif(rtn==2){ddl[rowCtr,cellCtr].SelectedValue="2";}elseif(rtn==4){ddl[rowCtr,cellCtr].SelectedValue="4";}}elseif(cmmtValue==2){//已提交,教师已确认的情况stringrtnResult="selectresultfrom"+table+"whereuserIdA='"+userId[rowCtr]+"'anduserIdB='"+userId[cellCtr]+"'";doublertn=Convert.ToDouble(db.SelectResult(rtnResult));if(rtn==0.25){ddl[rowCtr,cellCtr].SelectedValue="0.25";}elseif(rtn==0.5){ddl[rowCtr,cellCtr].SelectedValue="0.5";}elseif(rtn==1){ddl[rowCtr,cellCtr].SelectedValue="1";}elseif(rtn==2){ddl[rowCtr,cellCtr].SelectedValue="2";}elseif(rtn==4){ddl[rowCtr,cellCtr].SelectedValue="4";}MatrixTable.Enabled=false;}otherRowOtherCell.Controls.Add(ddl[rowCtr,cellCtr]);//将创建的DropDownList增加到单元格内}otherRow.Cells.Add(otherRowOtherCell);}}db.Close();}.....}
解决方案七:
DrawTable();把这句搬到!IsPostBack外面貌似就可以了,不过为什么啊,谁能告诉我
解决方案八:
把DropDownList的AutoPostBack的值改为True
解决方案九:
解决方案十:
法师法师打死
解决方案十一:
用ddl.Text.Trim()就可以了