填充下拉列表中的文本框在asp.net中选择

时间:2014-02-11 05:22:17

标签: asp.net

我是asp.net的新手。我有一个下拉列表,后面填充了代码。当我从下拉列表中选择任何项目时,它应该根据表格中的数据填充两个文本框和另一个下拉列表。

<asp:DropDownList runat="server" ID="dropDownExisting"></DropDownList><br />
<asp:TextBox ID="txtPromotion" runat="server" Width="77px" ></asp:TextBox><br />
<asp:TextBox ID="txtSubject" runat="server" Width="288px"></asp:TextBox><br />
<asp:DropDownList ID="dropDownType" runat="server">
    <asp:ListItem>Monthly Newsleter</asp:ListItem>
    <asp:ListItem>Webbinar Newsleter</asp:ListItem>
    <asp:ListItem>Annoucement</asp:ListItem>
</asp:DropDownList>

我希望它能在客户端完成。我在哪里提供数据库连接?我是否需要像更新面板一样使用ajax控件?或者javascript会运行良好吗?我想在客户端。

3 个答案:

答案 0 :(得分:1)

只需在DropDown SelectedIndexChanged事件

中执行此操作即可

SelectedIndexChanged

修改

<asp:UpdatePanel ID="upDdlGoal" runat="server" UpdateMode="always">
    <ContentTemplate>
      <asp:DropDownList ID="drop1" runat="server" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="drop1_SelectedIndexChanged">                    
</asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostbackTrigger ControlID="drop1" EventName="SelectedIndexChanged" />  
    </Triggers>
</asp:UpdatePanel>

代码背后:

<强> VB:

Protected Sub drop1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAddDepPlans.SelectedIndexChanged
    //Connectivity coding
End Sub

<强> C#:

protected void drop1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    //Connectivity coding
}

答案 1 :(得分:0)

客户端使用JavaScript进行编码

<强>的.aspx

  <asp:DropDownList runat="server"  ID="dropDownExisting" ></asp:DropDownList><br />
    <asp:TextBox ID="txtPromotion" runat="server" Width="77px" ></asp:TextBox><br />
    <asp:TextBox ID="txtSubject" runat="server" Width="288px"></asp:TextBox><br />
    <asp:DropDownList ID="dropDownType" runat="server">
        <asp:ListItem>Monthly Newsleter</asp:ListItem>
        <asp:ListItem>Webbinar Newsleter</asp:ListItem>
        <asp:ListItem>Annoucement</asp:ListItem>
    </asp:DropDownList>

<强>的.cs

  protected void Page_Load(object sender, EventArgs e)
        {

            dropDownType.Attributes.Add("onChange", "return OnSelectedIndexChange();");
        }

<强> JavaScript的:

   <script type="text/javascript">
          function OnSelectedIndexChange() {
              var sel = document.getElementById("dropDownExisting");
              var text = sel.options.value;
              var out = document.getElementById("txtPromotion");
              out.value += text + "\n";


          }

    </script>

答案 2 :(得分:0)