下拉列表未给出选定的值

时间:2014-05-14 07:49:58

标签: c# asp.net webforms

我有这个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#代码

 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()是执行查询并选择以数据集形式返回数据的数据的方法

on Save_Click事件我正在从下拉列表的选定值更新数据库中的数据,但它没有显示我选择的值
它总是在 SelectedText,SelectedItem
的索引0处显示值 请帮我解决我的代码有什么问题

1 个答案:

答案 0 :(得分:2)

请在!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();
      }

    }

当您再次点击按钮页面回发并且将导致再次绑定下拉列表时。您将获得所选值作为下拉列表的第一个索引..