2015-06-09 61 views
0

我編輯了一個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(); 
} 
+0

那麼你嘗試過什麼?做一點[研究DropDownLists](https://msdn.microsoft.com/en-us/library/1wd7hsyy(v = vs.85).aspx),如果有什麼不工作,我們可以提供幫助。 –

+0

我試過爲ddlTrailerLoc_SelectedIndexChanged創建一個函數,但那不起作用 – user2026041

+0

_What_沒有工作?該事件從未解僱?這些值不正確?無法連接到數據庫?插入不起作用? –

回答

0

請在下拉菜單中添加AutoPostBack =「True」。它將工作

<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(); 
    } 
} 
+0

我得到錯誤值不能爲空。參數名稱:字符串 上線Trailer.UpdateTrailer(int.Parse(TrailerID), – user2026041

+0

我已經更新了我的答案。請試試這個 –

+0

輸入字符串的int格式不正確錯誤trailerId = int.Parse(ddlTrailerLoc.SelectedValue的ToString()); – user2026041

相關問題