Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 下拉选择编辑项更改_C#_Asp.net_Html Select - Fatal编程技术网

C# 下拉选择编辑项更改

C# 下拉选择编辑项更改,c#,asp.net,html-select,C#,Asp.net,Html Select,我有两个下拉列表,第二个下拉列表中的项目完全取决于从第一个下拉列表中选择的项目。因此,我希望第二个下拉菜单在第一个下拉菜单的项目选择上进行更改。我的第一个下拉列表如下 <td> <asp:DropDownList ID="DropClient" runat="server" style="margin-left: 0px" AutoPostBack="true" Width="200px" OnTextChanged="DropClient_Te

我有两个下拉列表,第二个下拉列表中的项目完全取决于从第一个下拉列表中选择的项目。因此,我希望第二个下拉菜单在第一个下拉菜单的项目选择上进行更改。我的第一个下拉列表如下

<td>
                <asp:DropDownList ID="DropClient" runat="server" style="margin-left: 0px" AutoPostBack="true" Width="200px" OnTextChanged="DropClient_TextChanged">
                </asp:DropDownList> </td>
<td>
还有我的电子邮件()


上面的操作与预期的一样,但在我在第二个下拉列表中选择一个项目后,它不会按所选内容提交所选项目。默认情况下,它会提交列表中的第一项。另外,当我单击submit按钮时,它会快速刷新并提交列表中的第一项。我很困惑,任何帮助都将不胜感激

为什么不使用
onSeletedIndexChanged
事件,在
onSeletedIndexChanged
事件上调用相同的函数,可能会对您有所帮助

OnSelectedIndexChanged="cboRegion_SelectedIndexChanged"
将绑定第一个下拉列表的代码放在
中!iPostback
检查

if (!Page.IsPostBack)
{
    //Bind your first dropdown here
}

您可以使用
更新面板

这是示例代码

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem Value="0">Choose...</asp:ListItem>
<asp:ListItem Value="1">test1</asp:ListItem>
<asp:ListItem Value="2">Test2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel> 

选择。。。
测试1
测试2

每次回发都会触发页面加载,因此为了防止每次加载代码并重置DropDownList,请执行以下操作:

 protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
    //Put your code in here. This will only load the first time the page loads.
    bindDrEmail();
    }
}

伙计们,谢谢你们的时间。我终于解决了这个问题。首先,它之前没有触发事件的原因是,我将第二个下拉列表“AutoPostback”设置为true,而没有将我要触发的第一个下拉列表设置为true。当我反转此过程时,它将按如下方式启动

 protected void DropClient_TextChanged(object sender, EventArgs e)
        {

                bindDrEmail();

        }
<asp:DropDownList ID="DropDownClient" runat="server" style="margin-left: 0px"  AutoPostBack="true"
                    Width="200px" onselectedindexchanged="DropDownClient_SelectedIndexChanged">
                </asp:DropDownList>
请注意,drContact.DataValueField设置为“clientID”。clientID和drContact.DataTextField来自不同的表。我需要的列是“name”,因此必须将drContact.DataValueField更改为name为列的表的主键。因此

 drContact.Enabled = true;
        drContact.DataSource = reader;
        drContact.DataTextField = "name";
        drContact.DataValueField = "contactID";
        drContact.DataBind();

以上对我来说很好。谢谢您的时间。

在页面加载事件中显示您的代码,您可以将项目绑定到第一个下拉列表中。缺少IsPostBack检查可能是OnSelectedIndexChanged不适合您的原因。同意Prash。听起来你好像没有检查!isPostBack确保在(!page.PostBack())中包含页面加载代码!Page.PostBack()?从没听说过。但是我尝试了(!(IsPostBack))但它没有触发任何东西。Nuru,你必须使用更新面板:)你可以使用更新面板。但这并不是说你必须使用UpdatePanel.Yah,DropdownList在一个面板内,而面板在一个更新面板内。:)当我放入(!Page.IsPostBack)时,不会触发OnTextChanged/OnselectedIndexChanged。您在
(!Page.IsPostBack)
中实际放入的是什么?能否显示代码。'protected void Page_Load(object sender,EventArgs e){if(!(Page.IsPostBack)){bindPhone();bindDropDownClient();}}'
 <asp:DropDownList ID="drContact" runat="server" style="margin-left: 0px" Width="200px"> </asp:DropDownList>
 string sql = "SELECT a.clientID,a.cname,c.contactID,c.name FROM [CLIENT] a INNER JOIN [BRANCH] b ON a.clientID=b.clientID JOIN [CONTACT] c ON b.bid=c.bid WHERE a.cname =@clientname";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@clientname", DropDownClient.SelectedItem.Text);

            SqlDataReader reader = cmd.ExecuteReader();

            drContact.Enabled = true;
            drContact.DataSource = reader;
            drContact.DataTextField = "name";
            drContact.DataValueField = "clientID";
            drContact.DataBind();
 drContact.Enabled = true;
        drContact.DataSource = reader;
        drContact.DataTextField = "name";
        drContact.DataValueField = "contactID";
        drContact.DataBind();