【问题标题】:ASP RadioButtonList auto change item after click单击后 ASP RadioButtonList 自动更改项目
【发布时间】:2017-05-06 23:39:47
【问题描述】:

我绑定了我的 RadioButtonList,但是当我在单击 RadiobuttonList 后选择项目并使用按钮时,将选中的项目更改为第一行表单绑定

当我使用 autopostback='true" 和 RadioButtonList 作为选择发件人时也会发生这种情况

if (!IsPostBack)
{
    using (SqlConnection con = new SqlConnection(@"server=localhost; database=PS_User; trusted_connection=true;"))
    {
        con.Open();

        SqlCommand cmd = new SqlCommand(@"Select * from S1 WHERE PART =@1 AND PART IS NOT NULL Order by point", con);
        cmd.Parameters.AddWithValue("@0", 0);
        cmd.Parameters.AddWithValue("@1", 1);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        RadioButtonList1.DataTextField = "Name";
        RadioButtonList1.DataValueField = "Point";
        RadioButtonList1.DataSource = ds.Tables[0];
        RadioButtonList1.DataBind();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (RadioButtonList1.SelectedIndex > -1)
    {
        Label1.Text = "You chose: " +
            RadioButtonList1.SelectedItem.Text;
    }
}

Bug

【问题讨论】:

  • 我已经测试了按钮点击和OnSelectedIndexChanged 自动回发设置为真。它两次都按预期工作。你在使用UpdatePanel 或类似的东西吗?因为问题不在上面的sn-p中。

标签: c# asp.net


【解决方案1】:

是的,我用过 https://www.youtube.com/watch?v=K2DL8jHEzgE&feature=youtu.be

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="False" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
            </asp:RadioButtonList>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" ClientIDMode="Static" />
            <br />
            <br />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
        </ContentTemplate>
    </asp:UpdatePanel>

【讨论】:

    【解决方案2】:

    我解决了这个问题

        RadioButtonList1.DataValueField = "Point";
    

    应该是

        RadioButtonList1.DataValueField = "Name";
    

    我有另一个问题,我无法创建新主题,所以我在这里问: 在没有 SQL 注入的情况下,是否可以在 ASP 中的字符串查询中声明 usnig? 我做了一个查询并且工作正常,但我必须在查询中添加声明

    string queryString = @"Declare @Slot Tinyint,@empty smallint
    

    因为当我尝试这个不再工作时:

     cmd2.Parameters.Add("@Slot", SqlDbType.TinyInt); cmd2.Parameters.Add("@empty", SqlDbType.SmallInt);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多