locked
Moderators please delete this thread RRS feed

  • Question

  • User426001450 posted

    Please delete this thread. This thread has been viewed more than 600 times and has not been solved. As soon as it is deleted I will rewrite it in a different way and hopefully it will be solved. Thanks a lot for your help in deleting this thread.

    Friday, January 3, 2020 5:21 AM

All replies

  • User-1716253493 posted

    Try adding another if condition

    Protected Sub SelectionForVotingDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim ddl As DropDownList = CType(sender, DropDownList)
    If ddl.SelectedIndex > 0 Then

    The ddl will change another ddl, but don;t change the selection when you compare to two ddls with selectedindex = 0

    Friday, January 3, 2020 7:25 AM
  • User288213138 posted

    Hi vstorpedo,

    Since ' <asp:ListItem Selected="True" Value="Select">-- Select place --</asp:ListItem>; is selected, it is obvious that 'Select place' is display first, but after user select a different value like 'First place', the ddl goes back automatically to ' -- Select place -- ' again.

    I tested your code, it works fine in my side. Is your code complete?

    Best regards,

    Sam

    Monday, January 6, 2020 2:35 AM
  • User426001450 posted

    This is a mess. I'm having a hard time trying to understand why it was working fine months ago. I tested the DDL like a hundred times before and it was working. 

    Now while reading different posts on the Web I found that I have to set EnableViewState="false" on the DDL and on the ListView otherwise the code behind for the DDL, the OnSelectedIndexChanged won't be called. Very weird. I did that and is working, but still need to make some adjustment in the code. You could find the information on this link http://www.dirigodev.com/blog/web-development-execution/aspdropdownlist-does-not-fire-onselectedindexchanged-event-in-an-asprepeater/

    I'm going to continue testing and improving the code and I will update this post. Thanks for your help oned_gk, you are a true All-Star.

    Monday, January 6, 2020 6:21 AM
  • User426001450 posted

    Hello Samwu

    Thanks for helping. Did you make any change at all to the code or you just copy-paste and it was ready to go. Please let me know. May you please post the complete code that you tested? The code for the page and the code behind. Thanks again for your help.

    Monday, January 6, 2020 6:44 AM
  • User288213138 posted

    Hi vstorpedo,

    Did you make any change at all to the code or you just copy-paste and it was ready to go. Please let me know. May you please post the complete code that you tested?

    I just use the code you posted without any changes.

    The control EnableViewState property set a value indicating whether the server control persists its view state, and the view state of any child controls it contains, to the requesting client. If you disable ViewState, there is no longer a value on PostBack. But this seems to have nothing to do with the OnSelectedIndexChanged event.

    More information about ASP.NET View State you can refer to this link:https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/ms972976(v=msdn.10)?redirectedfrom=MSDN

    Best regards,

    Sam

    Monday, January 6, 2020 7:24 AM
  • User426001450 posted

    Code is giving error

    Monday, January 6, 2020 5:48 PM
  • User288213138 posted

    Hi vstorpedo,

    Compiler Error Message: BC30451: 'otherddl' is not declared. It may be inaccessible due to its protection level.

    The link in your first post has the statement otherddl. 

    Dim ddl As DropDownList = CType(sender, DropDownList)

    The DataSourceID of 'PerformersRosterApprovedForShowBallotListView' must be the ID of a control of type IDataSource.  A control with ID 'PerformersRosterApprovedForShowBallotSqlDataSource' could not be found.

    This is just binding my custom data source, does it have anything to do with reproducing your problem?

    My test code:

    <asp:ListView ID="PerformersRosterApprovedForShowBallotListView"
                    runat="server" ItemPlaceholderID="TableContent1">
                    <layouttemplate>
                        <div class="TableScroll1" style="overflow-x: auto; width: 90%">
                            <table class="TableClass" cellspacing="0" cellpadding="4" border="1">
                                <thead style="background-color: #00693E; color: white;">
                                    <tr>
                                        <th>Official show voting ballot</th>
                                    </tr>
                                </thead>
                                <tbody runat="server" id="TableContent1"></tbody>
    
                            </table>
                        </div>
                        <br />
                    </layouttemplate>
                    <itemtemplate>
                        <tr>
                            <th>
                                <br />
                                Performer # <%# Container.DataItemIndex + 1 %><br />
                                <br />
                                Your selection : 
                 <asp:DropDownList ID="SelectionForVotingDropDownList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="SelectionForVotingDropDownList_SelectedIndexChanged">
                     <asp:ListItem Selected="True" Value="Select">-- Select place --</asp:ListItem>
                     <asp:ListItem Value="First place"> First place </asp:ListItem>
                     <asp:ListItem Value="Second place"> Second place </asp:ListItem>
                     <asp:ListItem Value="Third place"> Third place </asp:ListItem>
                 </asp:DropDownList>
    
                                <br />
                                <br />
                            </th>
                        </tr>
                    </itemtemplate>
                </asp:ListView>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                Dim dt As DataTable = New DataTable()
                dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Age")})
                dt.Rows.Add(1, "name1", "age1")
                dt.Rows.Add(2, "name2", "age2")
                dt.Rows.Add(3, "name3", "age3")
                PerformersRosterApprovedForShowBallotListView.DataSource = dt
                PerformersRosterApprovedForShowBallotListView.DataBind()
            End If
        End Sub
    
        Protected Sub SelectionForVotingDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim ddl As DropDownList = CType(sender, DropDownList)
            Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
            Dim idx As Integer = item.DataItemIndex
    
            For i = 0 To PerformersRosterApprovedForShowBallotListView.Items.Count - 1
    
                If Not i = idx Then
                    Dim otherddl As DropDownList = CType(PerformersRosterApprovedForShowBallotListView.Items(i).FindControl("SelectionForVotingDropDownList"), DropDownList)
    
                    If ddl.SelectedValue = otherddl.SelectedValue Then
                        otherddl.SelectedIndex = 0
                    End If
                End If
            Next
        End Sub

    Best regards,

    Sam

    Tuesday, January 7, 2020 1:54 AM
  • User426001450 posted

    Code is not working yet

    Tuesday, January 7, 2020 7:03 PM
  • User288213138 posted

    Hi vstorpedo,

    1) You don’t have EnableViewState="false" in your code. I do have to add it to the ListView and to the DDL or code behind won’t work. Why? I don’t kwon. I’m trying to find out.

    2) The code that you posted is running fine on my site in a new page that I created for that code. But I do have the same code on another page and is not working. Why? I don’t know.

    3) When I add EnableViewState="false", to my page, the code is working, but DDL change values when I move and start changing selection from the top down. If I start making selection from the last DDL and move up to the top one, DDL do not change values. But they work both ways in your code. Why? I don’t know either. Have to find out.

    I can't reproduce your question, can you post your complete code that can reproduce your question(If you have a master page, please post it too)?

    I think you need to understand the role of EnableViewState in the control. When EnableViewState = “false”, the control cannot be used to pass values on the page, because if it is a postback page, the content entered on the control after EnableViewState = “false” will not be returned to the page after submitting to the server.

    Best regards,

    Sam

    Wednesday, January 8, 2020 8:43 AM
  • User426001450 posted

    I created a new page on the local site and I isolated all the coding that work with the DDL control in order to make it simple and easy to find the bug if any. The page display fine with not errors but DDL is not working. The only difference with your code is that I call my records from a database, you did it in a different way. 

    Thursday, January 9, 2020 1:17 AM
  • User288213138 posted

    Hi vstorpedo,

    vstorpedo

    Dim ddl As DropDownList = CType(sender, DropDownList)
            Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
            Dim idx As Integer = item.DataItemIndex
    
            For i = 0 To PerformersRosterApprovedForShowBallotListView.Items.Count - 1
    
                If Not i = idx Then
                    Dim otherddl As DropDownList = CType(PerformersRosterApprovedForShowBallotListView.Items(i).FindControl("SelectionForVotingDropDownList"), DropDownList)
    
                    If ddl.SelectedValue = otherddl.SelectedValue Then
                        otherddl.SelectedIndex = 0
                    End If
                End If
            Next

    I tested your code, if the value selected in the dropdownlist is the same as before, the previous value will become the default value "-Select place-".

    But I commented out your code hehind and it works.

    This is my tested code:

    <asp:ScriptManager ID="MultiTaskScriptManager" runat="server" />
                <asp:UpdatePanel ID="PerformerOfficialShowRosterBallotUpdatePanel" runat="server">
                    <ContentTemplate>
                        <asp:ListView ID="PerformersRosterApprovedForShowBallotListView"
                            runat="server" DataSourceID="PerformersRosterApprovedForShowBallotSqlDataSource"
                            ItemPlaceholderID="TableContent1">
    
                            <LayoutTemplate>
                                <div class="TableScroll1" style="overflow-x: auto; width: 90%">
                                    <table class="TableClass" cellspacing="0" cellpadding="4" border="1">
                                        <thead style="background-color: #00693E; color: white;">
                                            <tr>
                                                <th>Voting ballot</th>
                                            </tr>
                                        </thead>
                                        <tbody runat="server" id="TableContent1"></tbody>
                                    </table>
                                </div>
                                <br />
                            </LayoutTemplate>
    
                            <ItemTemplate>
                                <tr>
                                    <th>
                                        <br />
                                        Candidate # <%# Container.DataItemIndex + 1 %><br />
                                        <br />
    
                                        Your selection : 
                 <asp:DropDownList ID="SelectionForVotingDropDownList" runat="server" AutoPostBack="true"
                     OnSelectedIndexChanged="SelectionForVotingDropDownList_SelectedIndexChanged">
                     <asp:ListItem Selected="True" Value="Select">-- Select place --</asp:ListItem>
                     <asp:ListItem Value="First place"> First place </asp:ListItem>
                     <asp:ListItem Value="Second place"> Second place </asp:ListItem>
                     <asp:ListItem Value="Third place"> Third place </asp:ListItem>
                 </asp:DropDownList>
    
                                        <br />
                                        <br />
                                    </th>
                                </tr>
                            </ItemTemplate>
                        </asp:ListView>
    
                        <asp:SqlDataSource ID="PerformersRosterApprovedForShowBallotSqlDataSource" runat="server"
                            ConnectionString="<%$ ConnectionStrings:ThreadDemoConnectionString %>"
                            SelectCommand="SELECT [AuftragID], [AuftragKunde] FROM [Test1]"></asp:SqlDataSource>
    
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                    </ContentTemplate>
                </asp:UpdatePanel>
    
    
    Protected Sub SelectionForVotingDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

    The result:

    Best regards,

    Sam

    Friday, January 10, 2020 6:55 AM
  • User426001450 posted

    Hello samwu

    Thanks a lot for your help. Unfortunately the code is not working for me and it is not working for you either. If you take a close look at your online demo, you could see that after you selected ‘first’, ‘second’ and ‘third’ place for candidate 1, 2 and 3 respectively; you then selected ‘first’ again for candidate 4. This selection for candidate 4 is ok, but as soon as you do that, the selection on DDL for ‘candidate 1’ should change immediately to the default value "-Select place-" and it doesn’t. So, something is wrong and the code behind is not triggering. This code should not allow you to have two different candidates selected for first place at once; that is the only purpose of the code behind here. But it is allowing you to do that, so, once again, it’s not working. I hope that I was clear on this one. Here is your online demo, take a look.

    Friday, January 10, 2020 7:10 PM
  • User288213138 posted

    Hi vstorpedo,

    Select ‘first’ for candidate 1, then select ‘first’ also for candidate 2. If after you do that, candidate 1 change to default value, the code behind is working this way. After this, do the same but in the opposite way. Select ‘first’ for candidate 2, then select ‘first’ for candidate 1, if candidate 2 change to default value, then the code behind is working both ways and everything should be fine

    If you use your code behind, it can work like you said.

    Best regards,

    Sam

    Monday, January 13, 2020 1:49 AM
  • User-1716253493 posted

    Seem like the problem because you use ddl selectedindexchanged event to change another ddl with same selectedindexchanged event.

    So, you need to limit event affect, such as only selectedindex >0 and/or only ddl in next row.

    Changing selecteduindex from code behind maybe will change other ddls to item0, don't change other ddls when it's come from code (looping).

    Monday, January 13, 2020 3:34 AM
  • User426001450 posted

    Hello oned_gk

    oned_gk

    Seem like the problem because you use ddl selectedindexchanged event to change another ddl with same selectedindexchanged event.

    I understand your point here. Is like a domino effect, the loop activate others DDL and the sequence keep going and other DDL that should not even be involve may get into the loop. I understand, but there is not other way to do it without a loop, because the code should check all DDL after and before the one that is changed by the user. I tried to avoid all DDL where the selected index is '0' or 'Select', but it is not working either.   

    Tuesday, January 14, 2020 1:37 AM
  • User-1716253493 posted
        Protected Sub SelectionForVotingDropDownList_SelectedIndexChanged(sender As Object, e As EventArgs)
            Dim ddl As DropDownList = CType(sender, DropDownList)
            Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
            Dim idx As Integer = item.DataItemIndex
            If ddl.SelectedValue.ToString <> "Select" Then
                'not "select"
    
                For i = idx To (PerformersRosterApprovedForShowBallotListView.Items.Count - 1)
                    'next row only
                    Dim otherddl As DropDownList = CType(PerformersRosterApprovedForShowBallotListView.Items(i).FindControl("SelectionForVotingDropDownList"), DropDownList)
                    If otherddl.SelectedValue.ToString <> "Select" And i <> idx And ddl.SelectedValue = otherddl.SelectedValue Then
                        otherddl.SelectedIndex = 0
                    End If
                Next
            End If
        End Sub

    Try this

    AFAIK, you can also disable already selected item in the next ddl

    Tuesday, January 14, 2020 3:34 AM
  • User426001450 posted

    Hello 

    It is incredible the hard time that this is giving us. It's not working ! It works only one way. If a user makes a selection and there is another DDL after it with the same selection, that DDL will change to 'select'. But not the other way around. If a user makes a selection and there is another DDL before it with the same selection, that DDL will not change to 'select'. In doing so, user will not be able to select his preference on that particular DDL. So, code is not working as should be yet.

    Tuesday, January 14, 2020 5:00 AM
  • User288213138 posted

    Hi vstorpedo,

    Can you take a screenshot to show the problem you encountered?

    Best regards,

    Sam

    Tuesday, January 14, 2020 9:28 AM
  • User426001450 posted

    Hello samwu

    Sorry, I'm having problem uploading screenshot.Microsoft site do not allow me.

    Wednesday, January 15, 2020 6:02 AM
  • User288213138 posted

    Hi vstorpedo,

    Sorry, I'm having problem uploading screenshot.Microsoft site do not allow me.

    But, my previous post explain everything very well. Here is the explanation again:

    It works only one way. If a user makes a selection and there is another DDL after it with the same selection, that DDL will change to 'select'. But not the other way around. If a user makes a selection and there is another DDL before it with the same selection, that DDL will not change to 'select'. In doing so, user will not be able to select his preference on that particular DDL.

    About how to post image you can refer to this link: https://forums.asp.net/t/2163087.aspx

    And I am  confused by your question and code now. Can you show me your latest code?

    Best regards,

    Sam

    Thursday, January 16, 2020 6:46 AM
  • User426001450 posted

    In reference to the code, all code posted by me here since the beginning work fine on my page, but once again they only work one way as I explained before.

    Thanks a lot

    Thursday, January 16, 2020 5:39 PM
  • User288213138 posted

    Hi vstorpedo,

    , if a user makes a selection and there is another DDL before it with the same selection, that DDL will not change to 'select'

     I couldn’t understand this sentence requirement clearly.

    Can you describe it in detail?

    Best regards,

    Sam

    Friday, January 17, 2020 8:37 AM
  • User426001450 posted

    Hello samwu


    vstorpedo

    , if a user makes a selection and there is another DDL before it with the same selection, that DDL will not change to 'select'

     I couldn’t understand this sentence requirement clearly.

    Can you describe it in detail?

    Ok, let me explain. Let’s say that there is a ListView that display three items/records and each item/record is holding a DDL. Let’s call them the DDL: A, B and C, respectively for items 1, 2, 3. let’s say that user make a selection on DDL ‘B’ that is hold by item 2. So, taking this DDL as a reference, DDL ‘A’ is the one that I called the before DDL because it comes first than DDL ‘B’ in the ListView display and the DDL ‘C’ is the one that I called the one after because it comes after DDL ‘B’ in the ListView display. This is by looking at the display from top to bottom. I hope that this is clear now. Please let me know.

    Friday, January 17, 2020 5:52 PM
  • User288213138 posted

    Hi vstorpedo,

    let’s say that user make a selection on DDL ‘B’ that is hold by item 2. So, taking this DDL as a reference, DDL ‘A’ is the one that I called the before DDL because it comes first than DDL ‘B’ in the ListView display and the DDL ‘C’ is the one that I called the one after because it comes after DDL ‘B’ in the ListView display. This is by looking at the display from top to bottom.

    Do you mean that when the value selected by DDL 'C' is the same as DDL 'B', DDL 'B' will not become the default value?

    But this is my tested result, DDL selected values cannot be the same.

    Best regards,

    Sam

    Saturday, January 18, 2020 2:09 AM
  • User426001450 posted

    Hello samwu

    Do you mean that when the value selected by DDL 'C' is the same as DDL 'B', DDL 'B' will not become the default value?

    Yes, that what I mean and your code doesn’t do that either. Just to make it simple you could do this. Add only two records to the database, when the page is display, there are going to be only two ddl on display or one per record. Both of them should be displaying the default value ‘Select’. Now do this, on the second one, select ‘First’, after that select ‘First’ also on the first ddl. As soon as you do that, the second one change to default ‘Select’. That is fine and that is the way that should be. Now, do the opposite. Select ‘First’ again on the second one and you will see that the first one does not change to default, and it should. That is the problem. It only work one way. Please check it out. Thanks    

    Saturday, January 18, 2020 4:11 AM
  • User288213138 posted

    Hi vstoppedo,

    Select ‘First’ again on the second one and you will see that the first one does not change to default

    It also change to default. below is result:

    Best regards,

    Sam

    Saturday, January 18, 2020 8:31 AM
  • User426001450 posted

    Hello samwu

    OK, according to your display it is working both ways in your system. Good. The question now is why it doesn't work on my page when we have the same code. I just don't get it.

    Thanks a lot for your help

    Saturday, January 18, 2020 7:53 PM
  • User288213138 posted

    Hi vstorpedo,

    May you please increase the number of records to 6, do the same testing with ddl 1 and 2, and with 3 and 4, to find if it works disregarding the number of records

    This is tested result:

    May you please tell me if the following code is the code that you are still running or if you have made some changes. Thanks

    Are you sure that the following is still your code behind for your last display? Please be sure. 

    I tested the code you provided, the only thing that changed was the data source, but obviously your problem has nothing to do with the data source.

    I suggest you recreate a project to check if it works.

    Best regards,

    Sam

    Sunday, January 19, 2020 9:04 AM
  • User426001450 posted

    Hello to all

    Ok guys, according to samwu and his previous post the code that I asked him to review and to test is working. However it is not working as it should on my local server. Since this is the case I need a third person to test the code and tell me what you found. The complete code is on display just two posts behind this one on this very same thread. The only thing that you have to change in the code may be the adaptation for your database or the datasource code.

    As soon as you do that, this is what I’m asking you to do. Add only two records to the database. Run the code, there are going to be two ddl when the page display, one per record. Both of them should be displaying the default value ‘Select’. Now do this, on the second one, select ‘First’, after that select ‘First’ again on the first ddl. As soon as you do that, the second one should change to default ‘Select’. That is the way that should be. Now, do the opposite. Select ‘First’ again on the second one and hopefully the first one should change to default. Please let me know if it works in your system both way, back and forth. The problem that I’m having is that it is not changing in my system. It works but only the first way.

    Second, you may add six records to your database and do the same testing with ddl three and four, five and six. Thanks a lot for your help.

    Tuesday, January 21, 2020 2:46 AM
  • User288213138 posted

    Hi vstorpedo,

    Do you still encounter such problems when creating a new project?

    Best regards,

    Sam

    Wednesday, January 22, 2020 7:44 AM
  • User426001450 posted

    Yes, everything is the same.

    Thanks

    Wednesday, January 22, 2020 1:17 PM