下拉列表显示基于单选按钮

时间:2014-08-23 08:43:00

标签: asp.net

我是一名不成熟的程序员。我刚刚开始编写一个月左右的编码

我正在尝试编写一个代码,该代码根据已更改的单选按钮在下拉列表中显示数据库表的特定值。

我尝试通过使用会话来保存表值的id并将其发送到下拉列表,但没有任何反应

<asp:RadioButton ID="rdbHome" runat="server" AutoPostBack="True" 
                    GroupName="rdb" oncheckedchanged="rdbHome_CheckedChanged" />

<asp:DropDownList ID="DropDownList1" runat="server" 
                    DataSourceID="SqlDataSource1" DataTextField="p" DataValueField="playerID">
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:BasketballConnectionString %>" 
                    SelectCommand="PlayerName" SelectCommandType="StoredProcedure">
                    <SelectParameters>
                        <asp:SessionParameter Name="team1ID" SessionField="team1" Type="Int32" />
                    </SelectParameters>
                </asp:SqlDataSource>

protected void rdbHome_CheckedChanged(object sender, EventArgs e)
    {
        string conStr = WebConfigurationManager.ConnectionStrings["BasketballConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(conStr);

        SqlCommand cmd = new SqlCommand("team1ID", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@mID", int.Parse(Request.QueryString["mID"]));

        SqlCommand cmd2 = new SqlCommand("matchTeam2", con);
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.Parameters.AddWithValue("@mID", int.Parse(Request.QueryString["mID"]));




        try
        {
            con.Open();
            SqlDataReader reader;
            reader = cmd2.ExecuteReader();
            //reader2 = cmd2.ExecuteReader();
            reader.Read();
            Session["team1"] = reader["team1ID"].ToString();
            cmd.Parameters.AddWithValue("@team1ID", Session["team1"]);
        }
        catch(Exception ex)
        {
            lblMsg.Text = "خطا" + ex.Message;
        }

    }

    ALTER PROCEDURE [dbo].[team1ID](@mID INT)
AS
BEGIN
SELECT teamTbl.teamID AS team1ID from 
teamTbl join matchTbl ON matchTbl.team1ID=teamTbl.teamID
WHERE matchID=@mID
END

ALTER PROCEDURE [dbo].[matchTeam2](@mID int)
AS
BEGIN
SELECT t1.teamName as team1Name,t2.teamName as team2name,matchTbl.playeName,matchTbl.playerScore,
matchTbl.team1Score,matchTbl.team2Score,team1ID,team2ID
FROM matchTbl JOIN teamTbl as t1 on matchTbl.team1ID=t1.teamID
JOIN teamTbl as t2 on matchTbl.team2ID=t2.teamID
WHERE matchTbl.matchID=@mID


 END

和sqlDataSource的代码

ALTER PROCEDURE [dbo].[PlayerName](@team1ID INT)
AS
BEGIN
SELECT playersTbl.playerName, playersTbl.playerID
FROM teamTbl JOIN playersTbl ON teamTbl.teamID=playersTbl.teamID
WHERE teamTbl.teamID=@team1ID
END

2 个答案:

答案 0 :(得分:0)

在这种情况下,您似乎根本没有填充下拉列表。在'rdbHome_CheckedChanged'方法中,你应该填充'DropDownList1'下拉控件。

如下所示:

DropDownList1.Items.Add(new ListItem("5", "5%"));

答案 1 :(得分:0)

感谢您的帮助,代码工作正常,其中只有一些愚蠢的错误,上面的代码被编辑并且没有问题