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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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# 文本框中有来自DDl的文本_C#_Asp.net - Fatal编程技术网

C# 文本框中有来自DDl的文本

C# 文本框中有来自DDl的文本,c#,asp.net,C#,Asp.net,我有一个从数据库收集数据的下拉列表和一个textbox,当我从下拉列表中选择一个值时,它包含下拉列表中的文本。我试着让它工作,但是当我从下拉列表中选择一个值时,文本框中没有显示数据,也没有出现错误。有人能帮我吗 aspx代码: <table id="Tbl" runat="server" width="70%" border="1" cellspacing="1" cellpadding="1"> <tr> <td>

我有一个从数据库收集数据的下拉列表和一个
textbox
,当我从下拉列表中选择一个值时,它包含下拉列表中的文本。我试着让它工作,但是当我从下拉列表中选择一个值时,
文本框中没有显示数据,也没有出现错误。有人能帮我吗

aspx代码:

<table id="Tbl" runat="server" width="70%" border="1" cellspacing="1" cellpadding="1">
        <tr>
            <td>
                Select
            </td>
            <td>
                <asp:SqlDataSource ID="SDSOption" runat="server" ConnectionString="Data Source=ELARABY-1EACFA3\SQLEXPRESS;Initial Catalog=ElarabyGroup;Integrated Security=True"
                    ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Id], [option] FROM [Option]">
                </asp:SqlDataSource>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SDSOption" DataTextField="option"
                            DataValueField="Id" ondatabound="DropDownList1_DataBound" 
                            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                        </asp:DropDownList>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
            <td>
                <b style="font-style: italic">Select Option</b> <b style="font-style: italic">
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TxtOption"
                        Display="Dynamic" ErrorMessage="*" SetFocusOnError="True"><b style="font-style: italic">*</b></asp:RequiredFieldValidator>
                </b>
            </td>
        </tr>
        <tr>
            <td class="style2">
                <asp:Label ID="LblOption" runat="server" Text="Option"></asp:Label>
            </td>
            <td class="style1">
                <asp:TextBox ID="TxtOption" runat="server"></asp:TextBox>
            </td>
            <td>
                <b style="font-style: italic">Note:Add Here Product Option</b> <b style="font-style: italic">
                    <br />
                    <asp:RequiredFieldValidator ID="RVOption" runat="server" ControlToValidate="TxtOption"
                        Display="Dynamic" ErrorMessage="*" SetFocusOnError="True"><b style="font-style: italic">*</b></asp:RequiredFieldValidator>
                </b>
            </td>
        </tr>
        <tr style="font-style: italic">
            <td class="style2">
                &nbsp;
            </td>
            <td>
                <asp:Button ID="BtnSubmit" runat="server" Height="20px" Text="Submit" Width="50px"
                    OnClick="BtnSubmit_Click" />
                &nbsp;
            </td>
            <td align="left">
                &nbsp; <b><i><span><a href="EditOption.aspx">
                    <asp:Image ID="Image1" runat="server" ImageUrl="~/Backend/Image/www_4photos_net_1137678404.jpg"
                        Width="60px" /></a> Display Other </span></i></b>
            </td>
        </tr>
    </table>

DropDownList包装在UpdatePanel中,但文本框不包装

这意味着在非同步回发期间(在SelectedIndexChanged事件上触发),回发仅具有UpdatePanel内控件的可视性(因为这是提交给服务器的全部内容)

因为文本框位于UpdatePanel之外,所以在异步回发期间它不可见

最简单的解决方案是将文本框放在UpdatePanel中

另一种解决方案是使用基本JavaScript(与异步回发不同,基本JavaScript可以访问整个DOM)使用
ScriptManager.RegisterStartupScript
从SelectedIndexChanged事件设置控件的值

例如

我刚刚获得了“无名英雄”金质徽章(超过10个答案被接受,没有投票,至少占总数的25%)。我既骄傲又失望
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TxtOption.Text += DropDownList1.SelectedItem.Value;
    }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(UpdatePanel1, 
    this.GetType(), 
    "NameOfScript", 
    string.Format("document.getElementById('{0}').value = '{1}';",
    txtOption.ClientId,
    DropDownList1.SelectedValue));
}