如何将DropDownList中的更改保存到sql数据库?

时间:2015-06-09 09:47:29

标签: c# html mysql asp.net database

我编辑了一个DataGrid列,所以该位置现在是一个DropDownList,它可以正常工作。它从数据库中填充DropDownList。

<asp:TemplateColumn HeaderText="Trailer Location">
    <itemtemplate>
        <asp:DropDownList ID="ddlTrailerLoc" runat="server" OnSelectedIndexChanged="ddlTrailerLoc_SelectedIndexChanged">
        </asp:DropDownList>
         <asp:HiddenField ID="hdlTrailerLoc" runat="server" Value='<%#Eval("TrailerLocation")%>' />
    </itemtemplate>
</asp:TemplateColumn>

但是当我更改DropDownList中的值时,我不知道如何保存对数据库所做的更改。

protected void PopulateDDLs(DropDownList ddlTrailerLoc)
{
    DataSet dsTrailerLocation = DataUtils.GetAllGenSmall(Company.Current.CompanyID, "Description", "", 1, false, "Description", false, "TrailerLocationNOCODE", 0);
    if (dsTrailerLocation.Tables[0].Rows.Count > 0)
    {
        ddlTrailerLoc.DataSource = dsTrailerLocation;
        ddlTrailerLoc.DataValueField = "Description";
        ddlTrailerLoc.DataTextField = "Description";
         ddlTrailerLoc.DataBind();
        }
        else
        {
            ddlTrailerLoc.Items.Insert(0, new ListItem("No Locations Entered", "0"));
        }
    }

protected void dgList_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager && e.Item.ItemType != ListItemType.Footer)
        {
            DropDownList ddlTrailerLocation = e.Item.FindControl("ddlTrailerLoc") as DropDownList;
            if (ddlTrailerLocation != null)
            {
                PopulateDDLs(ddlTrailerLocation);
                //set the value in dropdown
                HiddenField hdlTrailerLoc = e.Item.FindControl("hdlTrailerLoc") as HiddenField;
                if (hdlTrailerLoc != null)
                {
                    ddlTrailerLocation.SelectedValue = hdlTrailerLoc.Value;
                }
            }
        }
    }

我尝试创建这个ddlTrailerLoc_SelectedIndexChanged方法,但事件没有运行。

protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList list = (DropDownList)sender;
    TableCell cell = list.Parent as TableCell;
    DataGridItem item = cell.Parent as DataGridItem;

    int selectedIndex = item.ItemIndex;
    string selectedItem = item.Cells[0].Text;
    // now save your work here and rebind the grid.
    Trailer.UpdateTrailer(int.Parse(TrailerID), Company.Current.CompanyID,
                                                txtTrailerReg.Text,
                                                ddlTrailerLocation.Text);
    ddlTrailerLocation.DataValueField = "Description";
    ddlTrailerLocation.DataTextField = "Description";
    ddlTrailerLocation.DataBind();
}

1 个答案:

答案 0 :(得分:0)

请添加AutoPostBack =&#34; True&#34;在下拉列表中。它会起作用

<asp:DropDownList ID="ddlTrailerLoc" runat="server" OnSelectedIndexChanged="ddlTrailerLoc_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>

请编写如下代码

protected void ddlTrailerLoc_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlTrailerLoc=sender as DropDownList;
    if(ddlTrailerLoc!=null)
    {
         int trailerId=int.Parse(ddlTrailerLoc.SelectedValue.ToString());
         //Save selected value in Database 

    // now save your work here and rebind the grid.
    Trailer.UpdateTrailer(trailerId, Company.Current.CompanyID,
                                                txtTrailerReg.Text,
                                                ddlTrailerLoc.Text);
    ddlTrailerLocation.DataValueField = "Description";
    ddlTrailerLocation.DataTextField = "Description";
    ddlTrailerLocation.DataBind();
   }
}