【问题标题】:Dynamically creating controls asp.net C#动态创建控件asp.net C#
【发布时间】:2016-09-13 22:26:10
【问题描述】:

我有一个网站,您可以在其中进行多项选择测验。测验中的每个问题可以有 1 到 5 个可能的答案。我在弄清楚如何根据有多少答案动态地制作测验控件时遇到了麻烦。我可以很容易地得到答案的数量。 所以如果有 3 个答案,我需要生成 3 个文本框(只读)和相应的单选按钮列表。然后当用户按下按钮时,数据被提交并加载下一个问题。我正在尝试用 C# 来完成这一切。

【问题讨论】:

  • 你是怎么做静态的?发布该代码可以让我们更好地了解您正在使用哪些框架。
  • 最多只有 5 个,我很想拥有 5 个主要是静态的选项。动态填充文本并隐藏未使用的问题。
  • 如果我静态地做,我可以使用视觉工作室中的设计视图将它们与 CSS 一起放置。我尝试过静态制作它们并切换可见性,但它似乎有点笨重。像 if(1){仅显示 1 个按钮和文本框} (if2){仅显示 2 个按钮和文本框} 等。

标签: c# asp.net .net webforms


【解决方案1】:

我曾经完全一样,但只动态创建文本框。我正在复制我的代码(现在有点忙进行更改),请根据您的需要进行编辑:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  

<html xmlns="http://www.w3.org/1999/xhtml">  

<head runat="server">  
    <title></title>  

    </script>  

</head>  

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>  

<script type="text/javascript">  
    function GetDynamicTextBox(value)  
    {  
        return '<input name = "DynamicTextBox"  class="dynamic"  type="text" value = "' + "mymonthname" + '" />' +  
            '<input name = "DynamicTextBox1"  class="dynamic"  type="text" value = "' + "myYearname" + '" />' +  
            '<input type="button" value="Remove" onclick = "RemoveTextBox(this)" />'  
    }  

    function AddTextBox()  
    {  
        var div = document.createElement('DIV');  
        div.innerHTML = GetDynamicTextBox("");  
        document.getElementById("TextBoxContainer").appendChild(div);  
    }  

    function RemoveTextBox(div)  
    {  
        document.getElementById("TextBoxContainer").removeChild(div.parentNode);  
    }  
    //    function RecreateDynamicTextboxes() {    
    //        var values = "vishal";    
    //        if (values != null) {    
    //            var html = "";    
    //            for (var i = 0; i < values.length; i++) {    
    //                html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";    
    //            }    
    //            document.getElementById("TextBoxContainer").innerHTML = html;    
    //        }    
    //    }  
</script>  

</head>  

<body>  
    <form id="form1" runat="server">  

        <div id="TextBoxContainer" style="text-align: center;" class="step">  
        </div>  

        <div>  
            <div style="text-align: center;" id="setPrinteraxcis1">  
                <input id="btnAdd" type="button" value="Add" onclick="AddTextBox()" class="ANPClass" />  
            </div>  
        </div>  
    </form>  
</body>  

要访问这些值,您需要在 .cs" 中编写以下代码

 string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
                        if (textboxValues == null || textboxValues.Length == 0)
                        {
                            string empty = "true";
                        }
                        else
                        {

                            JavaScriptSerializer serializer = new JavaScriptSerializer();
                            this.Values = serializer.Serialize(textboxValues);

                            foreach (string textboxValue in textboxValues)
                            {

                                MultipleId.Add(textboxValue);
                            }
                        }

MultipleId 是一个数组列表:

  public ArrayList MultipleId = new ArrayList();

附言 我根据一些标准阅读了您需要的要求,然后您可以在 AddTextbox 方法中使用计数器并循环创建所需数量的文本框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2010-11-18
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    相关资源
    最近更新 更多