1

我有一个页面gridview
gridview的显示产品信息和管理员可以Edit gridviews columns
我的两个columns展示brand namecategory name每个产品;
我在网格视图中使用 ItemTemplate 标记中的标签来显示这两列值,并在我的标记中使用两个dropdownlists(branddrop,categorydrop) 来编辑这两列值,当管理员在branddrop 中选择一个项目时,categorydrop 应该显示类别名称,它们是与branddrop中选择的品牌名称相关。
EditItemTemplate

我在数据库中的品牌表中的品牌名称是:

三星、诺基亚、索尼爱立信、苹果、LG、HTC……

我在类别表中的类别名称是:

Galaxy Nexus,Galaxy Tab 2 7. 0,Galaxy S3,Asha,Lumia,iPhone,iPad,Xperia Arc,Xperia Neo,Xperia X8,Cookie 3g,Cookie lite,Km555e,擎天柱 l9,擎天柱精英,擎天柱 g,wt18i,w8 ,500,n8...


当我单击 时Edit button,branddrop 中的第一个项目等于 Brandlable。

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 idfrombrands=@idfrombrands " >
                <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;

                }
            }

        }

    }
}
4

4 回答 4

1

非常感谢Abide Masaraure。你帮了我很多。我删除了 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();
                }
            }

        }

    }
}
于 2013-01-05T15:37:12.537 回答
0

To use the above code i gave you in a row editing event you have to modify it like so.Then those objects won't be null.

 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");

    //
}

Something is definitely happening in your page cycle and is keeping on biting you.I suggest zip an mdf of your database and the offending aspx page and let me tackle it from here if you don't get it to work.Never give up.

enter image description here

于 2013-01-05T13:24:08.303 回答
0

尤里卡!!!。你欠我一杯咖啡。我复制了你的问题,并在两个小时后意识到你需要真正挂钩到你的索引选择的更改事件。诀窍是使用命名容器属性来定位你的下拉菜单,他们认为非常隐藏,但是 sender 参数使我们能够在 dropband 下拉触发时暴露它们。一切都像魅力一样运作。

在您选择的更改事件中使用此代码片段。

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

如果您碰巧继续遇到问题,这次我可以向您发送演示的 zip 文件。祝您编码愉快!!!。 调试

于 2013-01-03T21:29:43.080 回答
0

我需要查看您在选择的索引更改事件中使用的代码。

以下级联下拉技术将消除您在尝试连接父级及其子级时所遇到的所有痛苦......我在所有项目的 Ajax 控制工具包中都使用了这个奇妙的功能。您可以使用任何您可以使用的数据检索方法想要只要它返回一个数组。我在这个例子中使用 linq 进行数据检索。假设我有名为 Brand and Model 和 classes 的表:Brand and Model and collections: Brands and Models

在“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>

这就是您所需要的。无需连接事件。瞧!让工具包发挥作用。

于 2013-01-03T13:33:04.253 回答