简体   繁体   中英

How to save option selected in Dropdownlist after selection

I am displaying columns in a GridView and one of the columns is a dropdownlist. I want to be able to save the option selected in the dropdownlist as soon as something is selected. I have done this with one of the columns that has a textbox so I was hoping to do something similar with the DropDownList. The code for the textbox and dropdownlist:

protected void gvPieceDetails_ItemDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        JobPieceSerialNo SerNo = e.Row.DataItem as JobPieceSerialNo;
        if (SerNo != null) {
            TextBox txtComment = e.Row.FindControl("txtComment") as TextBox;
            txtComment.Text = SerNo.Comment;
            txtComment.Attributes.Add("onblur", "UpdateSerialComment(" + SerNo.ID.ToString() + ", this.value);");

            DropDownList ddlReasons = (e.Row.FindControl("ddlReasons") as DropDownList);
            DataSet dsReasons = DataUtils.GetUnapprovedReasons(Company.Current.CompanyID, "", true, "DBRIEF");
            ddlReasons.DataSource = dsReasons;
            ddlReasons.DataTextField = "Description";
            ddlReasons.DataValueField = "Description";
            ddlReasons.DataBind();

            ddlReasons.Items.Insert(0, new ListItem("Reason"));

        }
    }

How to I create an update function for a dropdownlist?

protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sel = ddlReasons.SelectedValue.ToString();
    }

    public static void UpdateSerialReason(int SerNoID, string Reasons)
    {
        JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
        SerNo.Reason = sel; //can't find sel value 
        SerNo.Update();
    }

Dropdownlist:

 <asp:DropDownList ID="ddlReasons" runat="server" OnSelectedIndexChanged="DDLReasons_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>

I created an OnSelectedIndexChanged function to get the selected value. But how do I then save that value? Is there a way to pass it into the UpdateSerialReason function?

Just move the string sel declaration outside the scope of DDLReasons_SelectedIndexChanged and get the Text of the SelectedItem since it's included in your data source.

private string sel;

protected void DDLReasons_SelectedIndexChanged(object sender, EventArgs e)
{
    sel = ddlReasons.SelectedItem.Text;
}

public static void UpdateSerialReason(int SerNoID, string Reasons)
{
    JobPieceSerialNo SerNo = new JobPieceSerialNo(SerNoID);
    SerNo.Reason = sel; // Should now be available
    SerNo.Update();
}

The way you had it previously it was only available in the local scope, ie, inside the method in which it was being declared and used.

You can get selected value when you call your function:

UpdateSerialReason(/*Some SerNoID*/ 123456, ddlReasons.SelectedValue)

You will lose your value after postback is done if you save value to variable as Equalsk suggested. If you need to use your value on the other page you can save it in session.
If you are working within one asp.net page you can do as I suggested above. Then you can skip the postback on your DropDownList and call UpdateSerialReason when you need :)

And you might want to add property ViewStateMode="Enabled" and EnableViewState="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM