我編輯了一個DataGrid列,所以該位置現在是一個DropDownList,它工作正常。它從數據庫填充DropDownList。如何將更改從DropDownList保存到sql數據庫?
<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();
}
那麼你嘗試過什麼?做一點[研究DropDownLists](https://msdn.microsoft.com/en-us/library/1wd7hsyy(v = vs.85).aspx),如果有什麼不工作,我們可以提供幫助。 –
我試過爲ddlTrailerLoc_SelectedIndexChanged創建一個函數,但那不起作用 – user2026041
_What_沒有工作?該事件從未解僱?這些值不正確?無法連接到數據庫?插入不起作用? –