Domanda

I am trying to make the dropdownlist I have already created inside of the edit page of a formview start on the value previously selected from the sql database for the current user.

So far I have the code to populate the drop down list working correctly:

protected void ddlSelect_Init(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
    SqlCommand myCommand = new SqlCommand("SELECT Prefix, Number, ClassSection, Location, StartTime, EndTime, ClassDay, Prefix + Number  + ', Section: ' + CAST(ClassSection AS VarChar) +  ', Location: ' + Location + ', Start Time: ' + StartTime + ', End Time: ' + EndTime + ', Days: ' + ClassDay + ', Credits: ' + CAST(ClassCredit AS VarChar) AS PN, Capacity, GPAReqAbove1, GPAReqBelow1, CreditReqAbove30, CreditReqBelow30, ClassCredit, IsTransfer, SLN FROM Classes");
    myCommand.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(myCommand);
    DataTable dt = new DataTable();
    da.Fill(dt);

    DropDownList ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
    ddlSelect.DataSource = dt;
    ddlSelect.DataTextField = "PN";
    ddlSelect.DataValueField = "SLN";
    ddlSelect.DataBind();

    con.Close();
}

Where SLN is the unique value for each item in the dropdownlist and PN is the background information for each item in the dropdownlist. I want the item that is highlighted to be the PN that corresponds to what that specific user already has stored in the database. The problem is that when I try to have that value selected I am using:

 protected void FVStudentClass_ModeChanging(object sender, FormViewCommandEventArgs e)
    {
        if (FVStudentClass.CurrentMode != FormViewMode.Edit)
            return;
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ReinstatementCS"].ConnectionString);
        SqlCommand myCommand = new SqlCommand("SELECT Prefix + Number  + ', Section: ' + CAST(ClassSection AS VarChar) +  ', Location: ' + Location + ', Start Time: ' + StartTime + ', End Time: ' + EndTime + ', Days: ' + ClassDay + ', Credits: ' + CAST(ClassCredit AS VarChar) AS PN, SLN FROM Classes JOIN StudentClass on SLN = SCClass WHERE SCWSUID = " + Request.QueryString["ALWSUID"]);
        myCommand.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(myCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList ddlSelect = new DropDownList();
        ddlSelect = FVStudentClass.FindControl("ddlSelect") as DropDownList;
        if (ddlSelect != null)
        {
            ddlSelect.DataSource = dt;
            ddlSelect.Items.FindByText(dt.Rows[0]["PN"].ToString()).Selected = true;
        }
        con.Close();
    }

but I'm still stuck because the dropdownlist does not start out with the saved value being selected. Do you know how to fix this? Am I using the wrong command (Should I use something besides ModeChanging)? Thanks!

È stato utile?

Soluzione

Try implementing the logic to select the DropDownList item on the FormView DataBound event, the ModeChanging event happens before the mode is actually changed.

Altri suggerimenti

You should be able to just set ddlSelect.SelectedValue to the value you want the dropdown to select.

ddlSelect.SelectedValue = dt.Rows[0]["PN"].ToString();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top