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>
4

2 回答 2

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());
});
于 2012-11-04T09:46:53.220 回答
0

你的问题是因为当你检查时 (!string.IsNullOrEmpty(TextBox1.Text)),它总是返回 false 因为在第一阶段, TextBox 的值是空的,因此永远不会执行if condition......如果你需要通过上述条件,你可以放一些值TextBox申报时.......

或者,

只需这样SelectedIndexChangedDropDownList

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

这是与您的代码一样的工作示例:

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;
        }
    }
于 2012-11-04T04:31:35.030 回答