ASP.net C#从后面的代码中读取LiteralControl <select> <option>

时间:2016-03-31 03:01:22

标签: c# jquery asp.net

在我的ASP代码中我使用创建下拉列表 C# : DivPlant.Controls.Add(new LiteralControl(“&lt; select id ='”+ SelectID +“'multiple ='multiple'&gt;”+ Options +“&lt; / select&gt;”)); ASPX:  &lt; div id =“DivPlant”runat =“server”multiple =“multiple”style =“float:left”&gt; 如何从代码中获取所选值?

1 个答案:

答案 0 :(得分:1)

您可以使用Request.Form []

访问所选值

在你的aspx页面中:

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

<div id="DivPlant" runat="server" style="float:left">
<asp:Label runat="server" id="lblSelection"></asp:Label>
<asp:Button id="btnSubmit" runat="server" onclick="btnSubmit_Click"/>    
</div>
</form>

在你的代码背后:

protected void Page_Load(object sender, EventArgs e)
    {
        string SelectID = "ddlTest";
        string Options = "<option value='volvo'>Volvo</option>"; 
        DivPlant.Controls.Add(new LiteralControl("<select name='ddlName' id='" + SelectID + "'>" + Options + "</select>"));

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblSelection.Text = Request.Form["ddlName"];

    }