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))、最初の段階で TextBox の値が空であるため、常に false を返すためif conditionです。TextBox宣言時…………

または、

SelectedIndexChangedof でこのようにするだけですDropDownList

 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 に答える