2012-11-04 98 views
0

我试图在下拉菜单中将所选值分配给文本框,当我按下按钮时。我无法看到TextBox中的选定值。有人能让我知道我错过了什么吗?如何将下拉列表中的所选值分配给文本框

我的代码如下:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    {      

    } 

    protected void Button1_Click1(object sender, EventArgs e) 
    { 

     string strConn = "Initial Catalog=NavigateV6;Data Source=SVUSRYE-SQL3D1;User ID=dopis_user;Password=dopis_password;Persist Security Info=True;MultipleActiveResultSets=True"; 
     SqlConnection mycn = new SqlConnection(strConn); 
     DataSet ds = new DataSet(); 
     SqlDataAdapter myda = new SqlDataAdapter("Select accountid FROM trn_account ", mycn); 
     myda.Fill(ds); 
     DDL.DataSource = ds; 
     DDL.DataTextField = "AccountID"; 
     DDL.DataValueField = "AccountID"; 
     DDL.DataBind(); 

    } 



    protected void DDL_SelectedIndexChanged(Object sender, EventArgs e) 
    { 
     var selectedValue = ((DropDownList)sender).SelectedValue; 
     if (!string.IsNullOrEmpty(TextBox1.Text)) 
      TextBox1.Text = selectedValue; 

    } 
} 

ASPX:

<asp:DropDownList ID="DDL" runat="server" AutoPostBack="True" OnSelectedIndexChanged = " DDL_SelectedIndexChanged" > 

    </asp:DropDownList> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <br /> 
    <br /> 
    <br /> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click1" 
     Text="sumbit" /> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <br /> 
</asp:Content> 
+0

我试过了,但是在选择了下拉值之后它说网页无法显示。 – user1567194

+0

你有没有考虑尝试我的建议? – boruchsiper

回答

0

你的问题,是因为当您检查(!string.IsNullOrEmpty(TextBox1.Text)),它总是因为在第一阶段返回false,文本框的值是空的因此从不执行内部代码if condition .....如果您需要经过上述条件,您可以在申报时在TextBox中输入一些值.......

或者,

根本就这样在DropDownList

var selectedValue = ((DropDownList)sender).SelectedValue; 
TextBox1.Text = selectedValue; 

这里SelectedIndexChanged正在例如像你的代码:

ASPX:

<body> 
    <form runat="server" id="form1"> 
     <asp:DropDownList ID="DDL" runat="server" AutoPostBack="True" 
     OnSelectedIndexChanged = "DDL_SelectedIndexChanged" > 
     <asp:ListItem Value="One">One</asp:ListItem> 
     <asp:ListItem Value="Two">Two</asp:ListItem> 
    </asp:DropDownList> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <br /> 
    <br /> 
    <br /> 
    <asp:Button ID="Button1" runat="server" 
     Text="sumbit" onclick="Button1_Click" /> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <br /> 
    </form> 
</body> 

代码背后:

public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 

     } 

     protected void DDL_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      var selectedValue = ((DropDownList)sender).SelectedValue; 
      TextBox1.Text = selectedValue; 
     } 
    } 
+0

我试过了,但在改变下拉菜单中的值之后说它的页面无法显示 – user1567194

+0

您是否在带有runat = server属性的form标签中包含了上面的html控件? –

+0

是的,我确实..Here是标签的 user1567194

1

jQuery的可以做,在没有回发一个单元:

$('#<%= ButtonID.ClientID %>').click(function() { 
    $('#<%= TextBoxID.ClientID %>').val($('#<%= DDL_ID.ClientID %>').val()); 
    return false; 
}); 

事实上,你甚至不需要点击一个按钮,就可以当用户选择一个新的值来实现ddl:

$('#<%= DDL_ID.ClientID %>').change(function() { 
    $('#<%= TextBoxID.ClientID %>').val($(this).val()); 
}); 
相关问题