下拉列表没有给出所选值

本文关键字:下拉列表 | 更新日期: 2023-09-27 18:03:20

我有这个aspx源代码

<tr>
            <td>MatchName</td>
            <td>
                <asp:DropDownList ID="ddlMatchName" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>Winning Team</td>
            <td>
                <asp:DropDownList ID="ddlWinningTeam" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>Score</td>
            <td>
                <asp:TextBox ID="txtScore" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnSave" runat="server" Text="Save"  OnClick="btnSave_Click" />
            </td>
            <td>
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
            </td>
        </tr>

c# Code

 protected void Page_Load(object sender, EventArgs e)
    {
        string queryMatchDetail = "select MatchId,MatchName from ADMMasterMatch order by MatchDate";
        ddlMatchName.DataSource = clsNewSqlFunctions.GetSelectedData(queryMatchDetail);
        ddlMatchName.DataTextField = "MatchName";
        ddlMatchName.DataValueField = "MatchId";
        ddlMatchName.DataBind();
        string queryTeamDetail = "SELECT [TeamId],[TeamName] FROM [AdmCurrentEventMasterTeam] order by TeamName";
        ddlWinningTeam.DataSource = clsNewSqlFunctions.GetSelectedData(queryTeamDetail);
        ddlWinningTeam.DataTextField = "TeamName";
        //ddlWinningTeam.DataValueField = "TeamId";
        ddlWinningTeam.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        var s = ddlMatchName.SelectedIndex;
        string query = "UPDATE [ADMMasterMatch] SET Winner = '" + ddlWinningTeam.SelectedItem.Text + "' , Score = '" + txtScore.Text + "' Where MatchId ='" + ddlMatchName.SelectedItem.Text +"'";
        clsNewSqlFunctions.ExecuteQuery(query);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ADMCurrentEventMatchResult.aspx");
    }

这里clsNewSqlFunctions.ExecuteQuery()和clsNewSqlFunctions.GetSelectedData()是执行查询和选择数据的方法,以数据集的形式返回数据

Save_Click事件上,我正在从下拉列表的选定值更新数据库上的数据,但它没有显示我选定的值
它总是在索引0处显示值。在 SelectedText,设置SelectedItem
请告诉我我的代码有什么问题

下拉列表没有给出所选值

请在!ispostback中绑定您的下拉菜单

 protected void Page_Load(object sender, EventArgs e)
    {
      if(!ispostback)
      {
        string queryMatchDetail = "select MatchId,MatchName from ADMMasterMatch order by MatchDate";
        ddlMatchName.DataSource = clsNewSqlFunctions.GetSelectedData(queryMatchDetail);
        ddlMatchName.DataTextField = "MatchName";
        ddlMatchName.DataValueField = "MatchId";
        ddlMatchName.DataBind();
        string queryTeamDetail = "SELECT [TeamId],[TeamName] FROM [AdmCurrentEventMasterTeam] order by TeamName";
        ddlWinningTeam.DataSource = clsNewSqlFunctions.GetSelectedData(queryTeamDetail);
        ddlWinningTeam.DataTextField = "TeamName";
        //ddlWinningTeam.DataValueField = "TeamId";
        ddlWinningTeam.DataBind();
      }
    }

当你点击一个按钮,页面再次回发,这将导致绑定下拉框,所以你会得到选择的值作为下拉框的第一个索引