views:

63

answers:

1

Hi, I really have a big problem(stuck) with two dropdownlists inside a detailsview. I have 4 detailsview : DdlRt,DdlRt2,ddlMobv1,ddlMobv2. Basically what happens here is that DdlRt2 gets populated when DdlRt is selected and ddlMobv2 gets populated when a specific value from ddlMobv1 is selected.

In code posted above the detailsview has a "Update" command and the problem is when the update command is executed the data is successfully posted to the db BUT! THE VALUES from DdlRt2 and ddlMobv2 dissapear! And I can't insert any more Items. The only Item that stays in these two dropdownlists is the default "Select Department" item which is't set in codebehind but is set in .aspx.....I can't figure what I'm doing wrong here It must be something with databinding & events.

So my question is "Why can't I insert any more items to these two dropdownlists after the "Update" command on the DetailsView is set or how can I preserve the values that are already in"?

THANKS FOR YOUR HELP!

<asp:DetailsView   ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="odsRegister"
DefaultMode="Edit" Height="50px" Width="125px" >
<Fields>
    <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
    <asp:BoundField DataField="EnotaID" HeaderText="EnotaID" SortExpression="EnotaID" />
    <asp:BoundField DataField="strOpis" HeaderText="strOpis" SortExpression="strOpis" />
    <asp:BoundField DataField="strPodrobenOpis" HeaderText="strPodrobenOpis" SortExpression="strPodrobenOpis" />
    <%--******************************************************************************--%>
    <%--RAZDELITEV TVEGANJ--%>
    <%--******************************************************************************--%>
    <asp:TemplateField HeaderText="RazdelitevTveganja1" SortExpression="RazdelitevTveganja1">
        <EditItemTemplate>
            <asp:DropDownList DataSourceID="odsRazdelitev1" ID="DdlRt" DataTextField="strOpis"
                DataValueField="Id" runat="server" OnSelectedIndexChanged="ddlRT_SelectedIndexChanged"
                AutoPostBack="true" 
                ondatabound="DdlRt_DataBound"  >
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="RazdelitevTveganja2" SortExpression="RazdelitevTveganja2">
        <ItemTemplate>
            <asp:DropDownList ondatabound="DdlRt_DataBound" AppendDataBoundItems="true" ID="DdlRt2"  DataTextField="strOpis" DataValueField="Id" runat="server">
                <asp:ListItem Text="Select Department" Value="-1" Selected="True" />
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField><%--SelectedValue='<%# Bind("RazdelitevTveganjID") %>'--%>
    <%--******************************************************************************--%>
    <%--VERJETNOST & POSLEDICA 0--%>
    <%--******************************************************************************--%>
     <asp:TemplateField HeaderText="VerjetnostID0" SortExpression="VerjetnostID0">
        <EditItemTemplate>
            <asp:DropDownList SelectedValue='<%# Bind("VerjetnostID0") %>' DataSourceID="odsVerjetnost0" ID="ddlVer0" DataTextField="strVerjetnostOpis" DataValueField="id" runat="server" Height="16px">
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="PosledicaID0" SortExpression="PosledicaID0">
        <EditItemTemplate>
            <asp:DropDownList SelectedValue='<%# Bind("PosledicaID0") %>' DataSourceID="odsPosledica0" ID="ddlPosl0" DataTextField="strPosledicaOpis" DataValueField="id" runat="server" Height="16px">
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
    <%--******************************************************************************--%>
    <asp:CheckBoxField DataField="bitPrenos" HeaderText="bitPrenos" SortExpression="bitPrenos" />
    <%--******************************************************************************--%>
    <%--METODE OBVLADOVANJA--%>
    <%--******************************************************************************--%>
    <asp:TemplateField HeaderText="MetodaObvladovanjaID1" SortExpression="MetodaObvladovanjaID1">
        <EditItemTemplate>
            <asp:DropDownList DataSourceID="odsMetodeObvladovanja1" ID="ddlMobv1" DataTextField="strOpis"
                DataValueField="id" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlRT_SelectedIndexChanged"
                Height="16px" ondatabound="DdlRt_DataBound">
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
      <asp:TemplateField HeaderText="MetodaObvladovanjaID1" SortExpression="MetodaObvladovanjaID1">
        <EditItemTemplate>
            <asp:DropDownList ondatabound="DdlRt_DataBound"  AppendDataBoundItems="true"  ID="ddlMobv2" DataTextField="strOpis" DataValueField="id" runat="server" Height="16px">
                 <asp:ListItem Text="Select Department" Value="-1" Selected="True" />
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField><%--SelectedValue='<%# Bind("MetodaObvladovanjaID") %>'--%>
    <%--******************************************************************************--%>
    <asp:BoundField DataField="strKontrolniMehanizmi" HeaderText="strKontrolniMehanizmi" ConvertEmptyStringToNull="false" SortExpression="strKontrolniMehanizmi" />
    <asp:BoundField DataField="strOdgovornaOseba0"  ConvertEmptyStringToNull="false" HeaderText="strOdgovornaOseba0" SortExpression="strOdgovornaOseba0" />
    <asp:BoundField DataField="strToleranca"  ConvertEmptyStringToNull="false" HeaderText="strToleranca" SortExpression="strToleranca" />
    <%--******************************************************************************--%>
    <%--VERJETNOST & POSLEDICA 1--%>
    <%--******************************************************************************--%>
     <asp:TemplateField HeaderText="VerjetnostID1" SortExpression="VerjetnostID1">
        <EditItemTemplate>
            <asp:DropDownList SelectedValue='<%# Bind("VerjetnostID1") %>'  DataSourceID="odsVerjetnost0" ID="ddlVer1" DataTextField="strVerjetnostOpis" DataValueField="id" runat="server" Height="16px">
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
     <asp:TemplateField HeaderText="PosledicaID1" SortExpression="PosledicaID1">
        <EditItemTemplate>
            <asp:DropDownList SelectedValue='<%# Bind("PosledicaID1") %>' DataSourceID="odsPosledica0" ID="ddlPosl1" DataTextField="strPosledicaOpis" DataValueField="id" runat="server" Height="16px"> 
            </asp:DropDownList>
        </EditItemTemplate>
    </asp:TemplateField>
     <%--******************************************************************************--%>
    <asp:BoundField DataField="strNadaljnjiUkrepi"  ConvertEmptyStringToNull="false" HeaderText="strNadaljnjiUkrepi" SortExpression="strNadaljnjiUkrepi" />
    <asp:BoundField DataField="strOdgovornaOseba1"  ConvertEmptyStringToNull="false" HeaderText="strOdgovornaOseba1" SortExpression="strOdgovornaOseba1" />
    <asp:BoundField DataField="strAktivnostiZaIzboljsave"  ConvertEmptyStringToNull="false" HeaderText="strAktivnostiZaIzboljsave"
        SortExpression="strAktivnostiZaIzboljsave"  />
    <asp:BoundField DataField="datDatumZadnjegaPregleda" HeaderText="datDatumZadnjegaPregleda"
        SortExpression="datDatumZadnjegaPregleda" />
    <asp:BoundField DataField="StatusID" HeaderText="StatusID" SortExpression="StatusID" />
    <asp:BoundField DataField="strOpomba"  ConvertEmptyStringToNull="false" HeaderText="strOpomba" SortExpression="strOpomba" />
    <asp:CommandField ShowEditButton="True" ShowCancelButton="false" />
</Fields></asp:DetailsView>

(I left the code for the ObjectDataSources) CODEBEHIND:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind("DdlRt", "DdlRt2");
            Bind("ddlMobv1", "ddlMobv2");
        }
    }



    protected void ddlRT_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (((DropDownList)sender).ID == "DdlRt") Bind("DdlRt", "DdlRt2");
        else if (((DropDownList)sender).ID == "ddlMobv1") Bind("ddlMobv1", "ddlMobv2");
    }



    private void Bind(string parentName, string childName)
    {
        var ddl = DetailsView1.FindControl(parentName) as DropDownList;
        var ddl2 = DetailsView1.FindControl(childName) as DropDownList;

        ListItem li = ddl2.Items[0];
        ddl2.Items.Clear();
        ddl2.Items.Insert(0, li);
        ddl2.DataSource = RazdelitevLogic.FetchByParent(int.Parse(ddl.SelectedValue));
        ddl2.DataBind();
    }




    protected void DdlRt_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        Register register = DetailsView1.DataItem as Register;

        if (register != null)
        {
            if (ddl.ID == "DdlRt")
            {
                Razdelitev item = RazdelitevLogic.Fetch(register.RazdelitevTveganjID);
                ddl.SelectedValue = item.intParent.ToString();

            }
            else if (ddl.ID == "DdlRt2")
            {
                Razdelitev item = RazdelitevLogic.Fetch(register.RazdelitevTveganjID);
                ddl.SelectedValue = item.Id.ToString();
            }

            else if (ddl.ID == "ddlMobv1")
            {
                Razdelitev item = RazdelitevLogic.Fetch(register.MetodaObvladovanjaID);
                ddl.SelectedValue = item.intParent.ToString();
            }
            else if (ddl.ID == "ddlMobv2")
            {
                Razdelitev item = RazdelitevLogic.Fetch(register.MetodaObvladovanjaID);
                ddl.SelectedValue = item.Id.ToString();
            }


        }


    }


    protected void odsRegister_Updating(object sender, ObjectDataSourceMethodEventArgs e)
    {
        var ddl = DetailsView1.FindControl("DdlRt2") as DropDownList;
        var ddlMobv2 = DetailsView1.FindControl("ddlMobv2") as DropDownList;

        foreach (Register item in e.InputParameters.Values)
        {
            item.RazdelitevTveganjID = int.Parse(ddl.SelectedValue);
            item.MetodaObvladovanjaID = int.Parse(ddlMobv2.SelectedValue);
        }

    }
A: 

That's an awful lot of code to read... :|

I'm not too familiar with the DetailsView control. How does one determine whether the EditItemTemplate or the ItemTemplate is rendered? With the other repeater controls, you must REBIND the data after setting the EditIndex property. Are you doing that?

Bryan