Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownList仅选择第一个值_C#_Asp.net_Sql_Html Select - Fatal编程技术网

C# DropDownList仅选择第一个值

C# DropDownList仅选择第一个值,c#,asp.net,sql,html-select,C#,Asp.net,Sql,Html Select,我的下拉列表有问题。在阅读了这里的很多帖子后,我仍然无法让它工作,所以我会问 这是我的C#代码: 以下是我的Asp代码: <asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList> <asp:Button runat="server" ID="Vælg" Text=

我的下拉列表有问题。在阅读了这里的很多帖子后,我仍然无法让它工作,所以我会问

这是我的C#代码:

以下是我的Asp代码:

<asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
    <asp:Button runat="server" ID="Vælg" Text="Vælg Email" OnClick="Vælg_Click" />
    <asp:TextBox runat="server" ID="EmailListe" TextMode="MultiLine" />
    <asp:TextBox ID="Besked" runat="server" />

正如您所看到的,我从我的SqlDatabase中获取DropDownList值。当我在下拉列表中选择一封电子邮件并单击该按钮时,它总是将第一个值添加到文本框中,即使我选择了另一封电子邮件


我做错了什么?

使用
EmailList.SelectedValue属性,它为您提供下拉列表的选定值

另外,在您的
页面中,加载
检查回发,这样您就不会在每次加载页面时将重复条目添加到下拉列表中

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
    using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Newsletter;Integrated Security=True"))
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("Select Email From Newsletter", connection);
        EmailList.DataSource = cmd.ExecuteReader();
        EmailList.DataTextField = "Email";
        EmailList.DataBind();
    }
   }
}

使用
EmailList.SelectedValue属性,它为您提供下拉列表的选定值

另外,在您的
页面中,加载
检查回发,这样您就不会在每次加载页面时将重复条目添加到下拉列表中

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
    using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Newsletter;Integrated Security=True"))
    {
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd = new SqlCommand("Select Email From Newsletter", connection);
        EmailList.DataSource = cmd.ExecuteReader();
        EmailList.DataTextField = "Email";
        EmailList.DataBind();
    }
   }
}

只要记住ASP.NET是如何工作的,
Page\u Load
在每次服务器往返的事件处理程序之前被调用,然后列表被刷新为默认值。只需检查它不是回发:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        return;

    // Your code here
}

编辑:一个小建议;您没有处理
SqlCommand
SqlReader
,您应该提取值并尽快处理它们以释放资源。通过这种方式,它们将由GC收集,这可能是一个大问题,尤其是当您的站点流量很大时…

请记住ASP.NET是如何工作的,
Page\u Load
在每次服务器往返之前被调用,然后列表被刷新为默认值。只需检查它不是回发:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
        return;

    // Your code here
}

编辑:一个小建议;您没有处理
SqlCommand
SqlReader
,您应该提取值并尽快处理它们以释放资源。通过这种方式,它们将由GC收集,这可能是一个大问题,特别是当您的站点流量很大时…

似乎您不需要往返服务器就可以完成您要做的事情。纯JavaScript:

<script>
    function setEmail() {
        var list = document.getElementById("<%= EmailList.ClientID %>");
        var textBox = document.getElementById("<%= EmailListe.ClientID %>");
        textBox.value = list.options[list.selectedIndex].text;
    }
</script>

<asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<button onclick="setEmail(); return false;">Vælg Email</button>
<asp:TextBox runat="server" ID="EmailListe" TextMode="MultiLine" />

函数setEmail(){
var list=document.getElementById(“”);
var textBox=document.getElementById(“”);
textBox.value=list.options[list.selectedIndex].text;
}
Vælg电子邮件

似乎您不需要往返服务器就可以完成您想做的事情。纯JavaScript:

<script>
    function setEmail() {
        var list = document.getElementById("<%= EmailList.ClientID %>");
        var textBox = document.getElementById("<%= EmailListe.ClientID %>");
        textBox.value = list.options[list.selectedIndex].text;
    }
</script>

<asp:DropDownList ID="EmailList" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<button onclick="setEmail(); return false;">Vælg Email</button>
<asp:TextBox runat="server" ID="EmailListe" TextMode="MultiLine" />

函数setEmail(){
var list=document.getElementById(“”);
var textBox=document.getElementById(“”);
textBox.value=list.options[list.selectedIndex].text;
}
Vælg电子邮件

根据,
ListControl.Text
ListControl.SelectedValue
完全相同。问题是控件在每次加载时都会重新绑定数据,因此一旦回发问题得到解决,这些属性中的任何一个都应该工作。根据,
ListControl.Text
ListControl.SelectedValue
完全相同。问题是控件在每次加载时都会重新绑定数据,因此,一旦回发问题得到解决,这些属性中的任何一个都会起作用。