在CS文件中动态生成CheckBox控件,RadioButton控件,要求点击页面上的Button按钮后,返回用户选择的答案。
确切的说,本人正在开发在线考试随机选题这一块,随机选题已经做好。关键就是怎样将题目显示在页面上,当用户“交卷”的时候,获取用户做题目的答案。
我是通过CS文件,将题目信息生成控件,动态地添加至页面中的Panel中的。但当点击Button交卷的时候,页面刷新了,获取不到值。请高手指导,本人QQ:7999450

解决方案 »

  1.   

    我前不久就做过一个这样的系统
    你要获得提交之后的答案,可以遍历Panel控件下的RadioButton控件选择的项啊~~
    怎么会不可以呢?
      

  2.   

    获取不到生成的控件,事件在Button中写的。当点Button时,页面刷新了,生成的题目就不在了
      

  3.   

    soft_biao(巴不豆):
        你好。如果方便,请将你开发的那个页面的源码发给我。谢谢。
    本人E-mail:[email protected]
      

  4.   

    在Page_Load里动态生成RadioButton控件,并且不要放到if(!IsPostBack)里
      

  5.   

    soft_biao(巴不豆):
       你在线啊~!太好了,请把你的QQ或者msn或者popo号给我,我想通过那个将我的源文件发给你,你帮我看一下错在哪里!
      

  6.   

    有个findcontrol方法,是指在包含你那个radio的控件的parent
      

  7.   

    这里关键的问题不是获取生成的控件,而是生成的这些CheckBox控件,RadioButton控件在用户选择之后,点击“提交”按钮的时候,页面刷新了。如果能够控件页面刷新,就能获取用户选择的值了。
      

  8.   

    to hyb3280660 (胡育兵) :看了你的源文件,写得有点过于烦琐,所以我另外写了一个文件test.aspx
    在\Modules\adminmanage目录下,已发到你的邮箱里~~
      

  9.   

    用Request.Form方法可以正確取得。
      

  10.   

    to soft_biao(巴不豆):
         你好。看了你写给我的程序,但是在你写的程序中,题目是固定的,当点击“交卷”按钮的时候,你会看到题目重新加载了,正好你写的题目是固定的,所以即使题目重新加载,但还是原来的题目,这时你获取到了值,但是我要实现的是“随机抽题”,题目不是固定的,所以当点击“交卷”按钮后,题目重新加载,与第一次加载的题目是不一样的。这时就获取不到值了。如果把你写的在Page_Load中加载题目的那一段代码加上“if(!Page.IsPostBack )”后,当点击“交卷”按钮时,就报错了,因为这时题目没有重新加载,获取不到值。根本的问题还是页面刷新,题目重新加载。
    如果题目是固定的,就不存在这样的事,如果题目是随机抽取的,就获取不到值。
      

  11.   

    参见
    <?xml version="1.0" encoding="utf-8" ?> 
    <survey name="SurveyApp" displayName="Personal Survey">
    <description>This survey is a survey of personal information.</description>
    <question type="dropdown" name="FruitQuestion">
    <text>Which fruit is orange and circular?</text>
    <responses>
    <response default = 'false'>Apple</response>
    <response default = 'false'>Banana</response>
    <response default = 'false'>Orange</response>
    <response default = 'false'>Kiwi</response>
    </responses>
    </question>
    <question type="dropdown" name="SkyQuestion">
    <text>What color is the sky?</text>
    <responses>
    <response default = 'true'>Blue</response>
    <response default = 'false'>Red</response>
    <response default = 'false'>Orange</response>
    <response default = 'false'>Yellow</response>
    </responses>
    </question>
    <question type="text" name="MusicianQuestion">
    <text>Describe your favorite type of music.</text>
    <defaultResponse></defaultResponse>
    <maxCharacters>75</maxCharacters>
    </question>
    <question type="multilist" name="SoupQuestion">
    <text>Which soups do you like? (Select all that apply)</text>
    <responses>
    <response>Clam Chowder</response>
    <response default="true">Tomato</response>
    <response>Chicken Noodle</response>
    <response>Split Pea</response>
    <response>Cream of Mushroom</response>
    </responses>
    </question>
    <question type="radio" name="GenderQuestion">
    <text>What is your gender?</text>
    <responses>
    <response>Male</response>
    <response>Female</response>
    </responses>
    </question>
    </survey>
      

  12.   

    private void loadContext()
    { // Put user code to initialize the page here

    string strFormNum = this.Request.Params["Form_Num"].ToString();
    string strFormLnum  = "000"; // this.Request.Params["FORM_LNUM"].ToString();
    string strApplType = "LV";  // this.Request.Params["APPL_TYPE"].ToString(); BasePage.StructUserInfo userInfo = ((BasePage.StructUserInfo)Session["UserInfo"]);
    string strEmpNum   = userInfo.EmpNum;
    string strCmpyCode = userInfo.CompCode;
    string strLocnCode = userInfo.LocnCode;
    string strWorkCode = userInfo.WorkCmpy;
    string strWorkLocn = userInfo.WorkLocn;
    string langDiff    = userInfo.LangDiff;
    string langCode    = userInfo.LangCode; // get empolyee detail info
    NewFrmDB newFrmDB = new NewFrmDB(langDiff,langCode);
    DataTable empInfo = newFrmDB.getEmpDtls( strEmpNum , strCmpyCode , strLocnCode ); // get approvers detail info
    DataTable approverPathDT = newFrmDB.getApproverPath(strEmpNum,strCmpyCode,strLocnCode,strApplType); dsAprvTransPath aprvTransPath = new Logic.dsAprvTransPath(); //create the drop down selection and textbox automatically
    for ( int i=0; i<approverPathDT.Rows.Count;i++)
    {
    // Get Aprv Path detail
    DataRow dr = aprvTransPath.Tables["T_APRV_TRANS_PATH"].NewRow();
    dr["CMPY_CODE"] = strWorkCode;
    dr["LOCN_CODE"] = strWorkLocn;
    dr["FORM_NUM"] = strFormNum;
    dr["APPL_TYPE"] = strApplType;
    dr["FORM_LNUM"] = strFormLnum;
    dr["APRV_SEQ"] = approverPathDT.Rows[i]["APRV_SEQ"].ToString();
    dr["APRV_CHNG_REAS"] = ""; string strAprvEmpNum = approverPathDT.Rows[i]["APRV_EMP_NUM"].ToString();
    string strAprvCmpyCode = approverPathDT.Rows[i]["APRV_CMPY_CODE"].ToString();
    string strAprvLocnCode = approverPathDT.Rows[i]["APRV_LOCN_CODE"].ToString(); // System Starting up , set aprv person and org aprv person as the same one
    dr["APRV_EMP_NUM"] = strAprvEmpNum;
    dr["APRV_CMPY_CODE"] = strAprvCmpyCode;
    dr["APRV_LOCN_CODE"] = strAprvLocnCode;

    dr["ORG_APRV_EMP_NUM"] = strAprvEmpNum;
    dr["ORG_APRV_CMPY_CODE"] = strAprvCmpyCode;
    dr["ORG_APRV_LOCN_CODE"] = strAprvLocnCode;

    // create row first
    string sAprvLabl = approverPathDT.Rows[i]["APRV_LABL"].ToString();
    string sDisplay = " <TR><TD nowrap class=\"TDLabelStyle\" align=\"right\" >" + sAprvLabl.Trim() ;
    this.phd_AprvPath.Controls.Add(new LiteralControl(sDisplay));
    this.phd_AprvPath.Controls.Add(new LiteralControl("</TD>"));
    this.phd_AprvPath.Controls.Add(new LiteralControl("<TD class=\"TDControlStyle\" >"));
    // create either drop down selection or text box 
    // Depende on "APRV_INDR"'s value
    if ( approverPathDT.Rows[i]["APRV_INDR"].ToString() == "Y" ) 
    {
    // Create Dropdownlist
    System.Web.UI.WebControls.DropDownList ddl = new System.Web.UI.WebControls.DropDownList(); int AprvGrde =  Convert.ToInt16(approverPathDT.Rows[i]["APRV_GRDE"]);
    string[] ArrGrp = new String[5];
    ArrGrp[0] = empInfo.Rows[0]["GRP_CODE"].ToString();

    string AprvPassPortName = approverPathDT.Rows[i]["SPRY_NAME"].ToString();

    // get the datasource for the drop down selection
    // we will get approvers company and location , but work company code and location code!!
    DataTable approverInfo = newFrmDB.getApprover( strAprvCmpyCode , strAprvLocnCode , strAprvEmpNum ); // create the dropdownlist
    ddl.ID = "ddlAprvSeq_" + approverPathDT.Rows[i]["APRV_SEQ"].ToString();
    ddl.DataSource = approverInfo.DefaultView;
    ddl.DataTextField = "EMP_NAME";
    ddl.DataValueField = "pk_code"; 
    ddl.Width = System.Web.UI.WebControls.Unit.Pixel(325);
    ddl.CssClass = "DropDownListStyle"; // ddl.AutoPostBack=true;
    ddl.DataBind();
    this.phd_AprvPath.Controls.Add(ddl);

    dr["Aprv_Indr"] = "Y";

    // set default supervisor name here. 
    string SelectAprv = strAprvCmpyCode+"_"+strAprvLocnCode+"_"+strAprvEmpNum;
    for (int j=0; j<ddl.Items.Count;j++)
    {
    //  select the defualt approver
    if ( ddl.Items[j].Value.Equals(SelectAprv) )
    {
    ddl.SelectedIndex = j;
    break;
    }
    }

    // Draw a textbox for change in supervisor approval 
    string strHtmlTextBox = "</tr><tr><TD nowrap class=\"TDLabelStyle\" align=\"right\" >"+ this.hid_Reason.Value + "</td><td  class=\"TDControlStyle\" >";
    this.phd_AprvPath.Controls.Add(new LiteralControl(strHtmlTextBox)); System.Web.UI.WebControls.TextBox txt_ChangeApprReason = new System.Web.UI.WebControls.TextBox(); txt_ChangeApprReason.MaxLength = 50;
    txt_ChangeApprReason.ID = "txtReasSeq_"+approverPathDT.Rows[i]["APRV_SEQ"].ToString();
    txt_ChangeApprReason.Width = System.Web.UI.WebControls.Unit.Pixel(325);
    txt_ChangeApprReason.CssClass = "TextBoxStyle"; this.phd_AprvPath.Controls.Add(txt_ChangeApprReason);
    }
    else 
    {
    // string SelectAprv = AprvCmpyCode+"_"+AprvLocnCode+"_"+AprvEmpNum;
    System.Web.UI.WebControls.TextBox txt_AprvName = new System.Web.UI.WebControls.TextBox(); txt_AprvName.Text = approverPathDT.Rows[i]["SPRY_NAME"].ToString();
    txt_AprvName.Enabled = false;
    txt_AprvName.ID = "AprvSeq_" + approverPathDT.Rows[i]["APRV_SEQ"].ToString();
    txt_AprvName.Width = System.Web.UI.WebControls.Unit.Pixel(325);
    txt_AprvName.CssClass = "TextBoxStyle"; this.phd_AprvPath.Controls.Add(txt_AprvName);

    // Set Not Dropdownlist
    dr["Aprv_Indr"] = "N";
    } this.phd_AprvPath.Controls.Add(new LiteralControl("</TD></TR>")); // create DataSource for later Using
    aprvTransPath.Tables[0].Rows.InsertAt(dr,i);
    } this.ViewState["dsp_SelectAprvPath_dsAprvTransPath"] = aprvTransPath;
    }
      

  13.   

    我以为你已经解决随机这个问题,所以就简单写了一下~~由于你有3种题型,每种题型抽取10题,所以建议声明一个静态整形数组(全局)
    如private static int[] n=new int[30];
    数组n[]每10个元素填充一种题型,然后n[0]=rand11;(rand11为第1种题型的第1个随机数,依次类推),n[1]=rand12;........(随机数只取一次)
    每次绑定数据和取数据的时候都以数组n[]作为入口其实方法就是将第一次随机抽到的题目序号用数组记录下来,方便后来操作~~
      

  14.   

    非常感谢各位高手指导,在此特别谢过“soft_biao(巴不豆)”~!
    我在昨天的时候就已经写出来了,我用的不是静态数组,用的是ViewState,将每次随机产生的ID保存在里面,当点击提交按钮的时候,这时页面刷新,我再将题目从ViewState中取出数据。