2013-01-03 115 views
1

我有一個其中包含gridview的頁面。
我的gridview顯示產品信息和管理員可以Edit gridviews columns
我的兩個columns顯示brand namecategory name每個產品;
我使用的ItemTemplate標籤標貼在我的網格視圖顯示這兩列的值,我用兩個dropdownlists(branddrop,categorydrop)
EditItemTemplate標籤編輯這兩列的值,當管理員在branddrop的categorydrop選擇項目應顯示品牌名稱,這些名稱與品牌名稱中的選定品牌名稱相關。
在EditItemTemplate中使用父子關係的Dropdownlist Gridview不起作用

在我的數據庫表的品牌

品牌名稱是:

三星,諾基亞,索尼愛立信,蘋果,LG,HTC ....

和我的類別名稱類別表有:

的Galaxy Nexus,銀河標籤2 7. 0,銀河S3,阿沙,的Lumia,的iPhone,iPad的Xperia弧,新的Xperia,X8的Xperia,餅乾3G,曲奇精簡版,Km555e,Optimus的L9,Optimus的精英,Optimus g,wt18i,w8,500,n 8 ...


當我點擊Edit button時,branddrop中的第一項等於可品牌化。

ItemTemplate中的文本,它工作正常我的問題是在類別下降的第一項不等於可分類。

文本在ItemTemplate中,並且類別下拉菜單不會將相關類別名稱顯示爲品牌名稱。
對於每款產品都顯示iphone和ipad,因此我必須在branddrop中選擇其他項目,然後再次選擇與該產品相關的相關品牌名稱,然後categorydrop可以顯示相關類別名稱的列表。

我試過用brandDrop_SelectedIndexChangedGridView1_RowEditing但它不起作用。

我不知道如何解決它。

這是我的第一個代碼:

<asp:TemplateField HeaderText="brand name"> 
      <ItemTemplate> 
       <asp:Label ID="brandname" runat="server" Text='<%#Eval("brand_name") %>'></asp:Label> 
       <asp:Label ID="idfrombrand" runat="server" Text='<%#Eval("idfrombrands") %>' Visible="false"></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:Label ID="br" runat="server" Text='<%# Eval("idfrombrands") %>' Visible="false"></asp:Label><%--OnSelectedIndexChanged="brandDrop_SelectedIndexChanged" --%> 

       <asp:DropDownList ID="brandDrop" runat="server" DataTextField="brand_name" DataValueField="id" DataSourceID="SqlDataSource4" AutoPostBack="true" OnSelectedIndexChanged="brandDrop_SelectedIndexChanged" > 
       </asp:DropDownList> 
       <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [id],[brand_name] from [brands]" ></asp:SqlDataSource> 
      </EditItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="category name"> 
      <ItemTemplate> 
       <asp:Label ID="catname" runat="server" Text='<%# Eval("category_name") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:Label ID="OldCatName" runat="server" Text='<%# Eval("idfromCategories") %>' Visible="false"></asp:Label> 
       <asp:DropDownList ID="categoryDrop" runat="server" AutoPostBack="true" DataTextField="category_name" DataValueField="id" DataSourceID="SqlDataSource3"> <%-- --%> 
       </asp:DropDownList> 
       <asp:Label ID="cat" runat="server" Text='<%# Eval("idfromCategories") %>' Visible="false" ></asp:Label> 
       <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:mobile_storeConnectionString2 %>" SelectCommand="select [category_name],[id],[idfrombrands] from [categories] where [email protected] " > 
       <SelectParameters> 
        <asp:ControlParameter ControlID="brandDrop" 
        Name="idfrombrands" PropertyName="SelectedValue" Type="Int32" /> 
       </SelectParameters> 
       </asp:SqlDataSource> 

      </EditItemTemplate> 
     </asp:TemplateField> 

這是我的代碼背後GridView1_RowDataBound和GridView1_RowUpdating:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    int brand=0; 
    int category=0; 
    //Drop Brand-------------------------------------- 

    DropDownList list1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("brandDrop"); 
    Label lbl = (Label)GridView1.Rows[e.RowIndex].FindControl("br"); 

    if (list1.SelectedItem.Value != null) 
    { 
     brand = Convert.ToInt32(list1.SelectedItem.Value);//NewIdFromBrand 

    } 
    else 
    { 
     brand = Convert.ToInt32(lbl.Text); 

    } 
    //Drop Category---------------------------------------- 

    DropDownList list2 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("categoryDrop"); 
    Label lbl2 = (Label)GridView1.Rows[e.RowIndex].FindControl("OldCatName"); 

    //int NewIdFromBrand2 = -1; 
    if (list2.SelectedItem.Value != null) 
    { 
     category = Convert.ToInt32(list2.SelectedItem.Value);//NewIdFromBrand2 

    } 
    else 
    { 

     category = Convert.ToInt32(lbl2.Text); 
    } 

    //Photo------------------------------------------- 

    string photoname = System.Guid.NewGuid().ToString(); 

    GridViewRow row = GridView1.Rows[e.RowIndex]; 
    FileUpload fileUpload = row.FindControl("FileUploadimg") as FileUpload; 
    Label lbl3 = (Label)GridView1.Rows[e.RowIndex].FindControl("oldImage"); 
    if (fileUpload != null && fileUpload.HasFile) 
    { 
     fileUpload.SaveAs(Server.MapPath("~/P_Image") + photoname + fileUpload.FileName); 

     SqlDataSource1.UpdateParameters["path"].DefaultValue = "~/P_Image" + photoname + fileUpload.FileName; 
    } 
    else 
    { 
    SqlDataSource1.UpdateParameters["path"].DefaultValue = lbl3.Text;//oldImage.Text; 
    } 
    int prid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); 
    SqlConnection cn = new SqlConnection(); 
    cn.ConnectionString = "server = . ; database = mobile_store ; Trusted_Connection=true"; 
    DataTable tb = new DataTable(); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = cn; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "UpdateProduct"; 
    cmd.Parameters.AddWithValue("@brandid", brand); 
    cmd.Parameters.AddWithValue("@catid", category); 
    cmd.Parameters.AddWithValue("@pid", prid); 
    try 
    { 
     cn.Open(); 
     cmd.ExecuteNonQuery(); 
     SqlDataSource1.DataBind(); 

    } 
    catch (Exception ex2) 
    { 


    } 
    finally { cn.Close(); } 
    //GridView1.EditIndex = -1; 
    //GridView1.DataBind(); 

} 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //check if is in edit mode 
     if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
     { 
      DataRowView dRowView1 = (DataRowView)e.Row.DataItem; 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
       { 
        DropDownList ddlStatus = (DropDownList)e.Row.FindControl("brandDrop"); 
        ddlStatus.SelectedValue = dRowView1["brandId"].ToString(); 
        DropDownList ddlStatus2 = (DropDownList)e.Row.FindControl("categoryDrop"); 
        ddlStatus2.SelectedValue = dRowView1["categoryID"].ToString(); 
        //Label1.Text = ddlStatus.SelectedValue; 

       } 
      } 

     } 

    } 
} 
+0

可能重複http://stackoverflow.com/questions/8056408/asp-net-gridview-refresh-with-cascading-dropdown-controls –

回答

1

非常感謝你信守Masaraure.you幫了我很多。 我刪除了grideview_rowediting和braddrop_selectedIndexChange事件,並在我的GridView1_RowDataBound事件中添加了兩行。

SqlDataSource sq = (SqlDataSource)e.Row.FindControl("SqlDataSource3"); 
sq.SelectParameters["idfrombrands"].DefaultValue = dRowView1["brandId"].ToString(); 

現在我的情況是這樣的:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //check if is in edit mode 
     if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
     { 
      DataRowView dRowView1 = (DataRowView)e.Row.DataItem; 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       if ((e.Row.RowState & DataControlRowState.Edit) > 0) 
       { 
        DropDownList ddlStatus = (DropDownList)e.Row.FindControl("brandDrop"); 
        ddlStatus.SelectedValue = dRowView1["brandId"].ToString(); 
        DropDownList ddlStatus2 = (DropDownList)e.Row.FindControl("categoryDrop"); 
        ddlStatus2.SelectedValue = dRowView1["categoryID"].ToString(); 

        SqlDataSource sq = (SqlDataSource)e.Row.FindControl("SqlDataSource3"); 
        sq.SelectParameters["idfrombrands"].DefaultValue = dRowView1["brandId"].ToString(); 
       } 
      } 

     } 

    } 
} 
0

我需要看到你的代碼中使用您的選擇索引更改事件。

下面的級聯下拉技術將帶走您在嘗試連接父級及其子級時所遭受的所有痛苦......我在我的所有項目中都使用Ajax控件工具包中的這一奇妙功能。您可以使用任何數據檢索方法,只要它返回一個數組。我在本例中使用linq進行數據檢索。 假設我有表名爲品牌和模型和類:品牌和型號和集合:品牌和型號

在'YourWebServicePath。ASMX」(Web服務文件)

<WebMethod()> _ 
    Public Function GetBrands(knownCategoryValues As String, category As String) As CascadingDropDownNameValue() 

     Dim result = From b As Bands In Brand.Brands Select New CascadingDropDownNameValue(b.BrandDesc, b.BrandID.ToString()) 
     Return result.ToArray() 

    End Function 


<WebMethod()> _ 
Public Function GetModels(knownCategoryValues As String, category As String) As CascadingDropDownNameValue() 
    Dim brandID As Guid 
    Dim brandValues As StringDictionary = AjaxControlToolkit.CascadingDropDown._ 
    ParseKnownCategoryValuesString(knownCategoryValues) 
    brandID = New Guid(brandValues("Brand")) 
    Dim result = From m As Models In Model.GetModels() Where m.brandID = brandID  Select New CascadingDropDownNameValue(m.ModelDesc, 
_ m.ModelID.ToString()) 
    Return result.ToArray() 
    End Function 

標記

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %> 

    <EditItemTemplate> 

       <asp:DropDownList ID="dropBrand" runat="server" AutoPostBack="true" > 
       </asp:DropDownList> 


        <cc1:CascadingDropDown ID="BrandCascadingDropDown" 
              runat="server" 
              Category="Brand" 
              TargetControlID="dropBrand" 
              PromptText="-Select Brand-" 
              LoadingText="Loading Brands.." 
              ServicePath="YourWebServicePath.asmx" 

              ServiceMethod="GetBrands"> 
        </cc1:CascadingDropDown> 

</EditItemTemplate> 

<EditItemTemplate> 
       <asp:DropDownList ID="dropModel" runat="server" AutoPostBack="true" > 
       </asp:DropDownList> 


      <cc1:CascadingDropDown ID="ModelCascadingDropDown" 
              runat="server" 
              Category="Model" 
              TargetControlID="dropModel" 
              ParentControlID="dropBrand" 
              PromptText="-Select Model-" 
              LoadingText="Loading Models.." 

              ServicePath="YourWebServicePath.asmx" 
              ServiceMethod="GetModels" > 
        </cc1:CascadingDropDown> 

並且那你need.No需要接線events.And瞧的!讓工具包發揮魔力。

+0

感謝您的答覆。我沒有在我選擇的索引更改事件中放入任何東西,並且categorydrop工作正常,當我從branddrop.it中選擇一個項目時,單擊編輯按鈕時效果不佳。因爲我使用sqldatasource,我不需要使用selected索引更改事件,當我從branddrop中選擇一個項目時,categorydrop會顯示相關的類別名稱,但是當我點擊編輯按鈕時,因爲我仍然不選擇它EM從品牌放棄它不能很好地工作。我不知道如何解決它 –

0

尤里卡!。你欠我coffee.I一杯複製你的問題,而實際上你需要勾選擇索引兩小時後實現改變event.The訣竅是使用命名容器屬性來定位你的下拉菜單,他們認爲是非常隱蔽的,但發件人的說法使我們能夠在dropband dropdown火災時暴露他們。而presto一切都像一個魅力。

在所選更改事件這一小段代碼片段。

protected void dropBand_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 

DropDownList dropBand = (DropDownList)sender; 

SqlDataSource dsc = (SqlDataSource)dropBand.NamingContainer.FindControl("SqlDataSource3"); 
DropDownList categoryDrop = (DropDownList)dropBand.NamingContainer.FindControl("categoryDrop"); 
dsc.SelectParameters("BrandID").DefaultValue = dropBand.SelectedValue; 
categoryDrop.DataBind(); 
} 

,如果你碰巧有繼續編碼issue.Happy我這次給你演示的zip文件!!! Debug

+0

謝謝你這麼多花時間來解決我的問題,我用它,但它是一樣的before.I需要把你的代碼像DSC .SelectParameters [「idfrombrands」]。DefaultValue = dropBand.SelectedValue;在事件發生時,當我點擊編輯按鈕,發生之前選擇索引更改事件。你知道我有產品名稱,產品價格和其他每行可能是管理點擊編輯按鈕,並只編輯產品價格,當他點擊更新按鈕類別名稱錯誤的值設置爲我的產品信息 –

+0

我的意思是把我的產品類別名稱爲「iPhone」,所以這種情況下迫使管理員每次他想要編輯的產品info.my問題是時候再次選擇該品牌名稱點擊編輯時,我不知道哪個事件觸發。 –

+0

我用gridviw_rowediting 的SqlDataSource平方=(SqlDataSource的)GridView1.Rows [e.NewEditIndex] .NamingContainer此代碼。的FindControl( 「SqlDataSource3」); DropDownList category Drop =(DropDownList)GridView1.Rows [e.NewEditIndex] .NamingContainer.FindControl(「categoryDrop」); DropDownList branddrop =(DropDownList)GridView1.Rows [e.NewEditIndex] .NamingContainer.FindControl(「brandDrop」); sq.SelectParameters [「idfrombrands」]。DefaultValue = branddrop.SelectedValue; categorydrop.DataBind();但發生null錯誤,branddrop,categorydrop,sq爲null –

0

要使用上面的代碼,我給了你一個行編輯事件,你必須修改它像so.Then那些對象不會爲空。

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GridView testgrid = (GridView)(sender); 
    testgrid.EditIndex = e.NewEditIndex; 
    testgrid.DataBind(); 
    DropDownList dropBand = (DropDownList)testgrid.Rows[e.NewEditIndex].FindControl("ddlProducts"); 

    // 
} 

有東西在你的頁面一定週期發生並保持在咬你。我建議壓縮您的數據庫的MDF和違規aspx頁面,讓我在這裏解決它,如果你沒有得到它工作。不要放棄。

enter image description here

相關問題