2012-11-04 41 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()); 
}); 
相關問題