?
Solved

Disable 2nd Pulldown

Posted on 2006-04-26
21
Medium Priority
?
753 Views
Last Modified: 2008-03-03
I think you'll need some knowledge of ASP.NET to help me with this one.

How can I gray out my second pulldown if the user changes the original value of the first pulldown? I'm trying to use a JavaScript (added in my header); maybe if I had this part right getElementById('supplierDrop2').  it would work?

<script language="javascript">
function FreqChanged()
{
Form1.getElementById('supplierDrop2').Enabled=false
}
</script>


I have added (what I believe is) the correct code to this line:

    <asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server" OnChange="javascript:FreqChanged();"></asp:DropDownList>

************************************************************

My original code:


          <script language="vb" runat="server">
          Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
               'Put user code to initialize the page here
               If (Not Page.IsPostBack) Then
                    listfoods()
                    FQl()
               End If
          End Sub
             
          Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
           listfoods()
           FQl()
                     
    End Sub

         Private Sub listfoods()

              Dim objconn As New SqlConnection("Mystring")
               objconn.Open()
               try
               Dim MemID
          MemID = (Request.Cookies(“sy”)(“MemID"))        
               Dim objcmd As New SqlCommand("ChFQ" , objconn)
               objCmd.CommandType = CommandType.StoredProcedure
              Dim retValue As Integer
              objcmd.Parameters.Add("@MemID", SqlDbType.Int).Value = MemID
               Dim retParam As New SqlParameter("@RETURN_VALUE", SqlDbType.Int)
retParam.Direction = ParameterDirection.ReturnValue
objcmd.Parameters.Add(retParam)

objcmd.ExecuteScalar()

retValue = Cint(Cint(objcmd.Parameters("@RETURN_VALUE").Value))
If retValue = 300 then
'no daily foods
else

Dim dr As SqlDataReader
               dr = objcmd.ExecuteReader()
               Dim currentNick as string = ""
               Dim currentNoNick as string = ""
Dim dt as DataTable = new DataTable()
dt.Columns.Add("HowOft")
dt.Columns.Add("FreqID")
dt.Columns.Add("DayName")
dt.Columns.Add("DWID")
dt.Columns.Add("FoodID")
dt.Columns.Add("FoodDesc")
dt.Columns.Add("HMID")
dt.Columns.Add("HMNickName")
dt.Columns.Add("NickDisplay")
dt.Columns.Add("NoNickDisplay")

Do While (dr.Read())
    Dim row as DataRow = dt.NewRow()
 row("HowOft")=dr("HowOft")
 row("FreqID")=dr("FreqID")
 row("DayName")=dr("DayName")
   row("DWID")=dr("DWID")
     row("FoodID")=dr("FoodID")
    row("FoodDesc")=dr("FoodDesc")
         row("HMID")=dr("HMID")
     if   (currentNoNick =("")) and (dr("HMNickName") = ("Please Select"))
    row("NoNickDisplay") = "Anyone's Foods"
        currentNoNick="Anyone"
        elseif (currentNoNick="Anyone")
        row("NoNickDisplay")=""
        end if
        if (currentNick <> dr("HMNickName")) and (dr("HMNickName") <> ("Please Select")) ' if this is the first time
        row("NickDisplay") = dr("HMNickName")+"'s Foods"
        currentNick=dr("HMNickName")
    Else
        row("NickDisplay")=""
    End If
      dt.Rows.Add(row)
Loop
       dataList1.DataSource = dt
       dataList1.DataBind()
       dr.Close
end if
               finally
               objConn.Close()
               objConn.Dispose()
               end try
             
               End Sub
               
     
Private Sub datalist_item_bind(ByVal sender As Object, ByVal e As DataListItemEventArgs)
              If ((e.Item.ItemType = ListItemType.Item) or ( e.Item.ItemType = ListItemType.AlternatingItem)) Then
                    Dim droplist As DropDownList
                   
                    droplist = CType(e.Item.FindControl("supplierDrop"), DropDownList)
                    Dim objconn As New SqlConnection("Mystring")
                    objconn.Open()
                    try
                    Dim objcmd As New SqlCommand("FQEd", objconn)
                    objCmd.CommandType = CommandType.StoredProcedure
                    Dim dr As SqlDataReader
                    dr = objcmd.ExecuteReader()              
                    droplist.DataSource = dr
                    droplist.DataTextField = "HowOft"
                    droplist.DataValueField= "FreqID"
                    droplist.DataBind()
                   
          if not droplist.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "FreqID").ToString()) is nothing then
     droplist.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "FreqID").ToString()).Selected = True
end if
              dr.Close()
                     
                    finally
                    objConn.Close()
                   objConn.Dispose()
                    end try    
      End If          
             
  If ((e.Item.ItemType = ListItemType.Item) or ( e.Item.ItemType = ListItemType.AlternatingItem)) Then
  Dim droplist2 As DropDownList
                 
                    Dim objconn As New SqlConnection("Mystring")
                    objconn.Open()
                      try
                    droplist2 = CType(e.Item.FindControl("supplierDrop2"), DropDownList)
                                     
                    Dim objcmd2 As New SqlCommand("TP", objconn)
                    objCmd2.CommandType = CommandType.StoredProcedure
                    Dim dr3 As SqlDataReader
                    dr3 = objcmd2.ExecuteReader()              
                    droplist2.DataSource = dr3
                    droplist2.DataTextField = "DayName"
                    droplist2.DataValueField= "DWID"
                    droplist2.DataBind()
                   
          if not droplist2.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "DWID").ToString()) is nothing then
     droplist2.Items.FindByValue(DataBinder.Eval(e.Item.DataItem, "DWID").ToString()).Selected = True
end if
             dr3.Close()
             
             
                    finally
                    objConn.Close()
                   objConn.Dispose()
                    end try

               End If
          End Sub
         
                     
    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim objconn As New SqlConnection("Mystring")
        'asked from open to close
        objconn.Open()
        Dim objcmd As New SqlCommand("UpCh", objconn)
        objCmd.CommandType = CommandType.StoredProcedure
        objcmd.Parameters.Add("@FoodID", SqlDbType.Int)
        objcmd.Parameters.Add("@FreqID", SqlDbType.Int)
        objcmd.Parameters.Add("@DWID", SqlDbType.Int)

        For Each dlItem As DataListItem In DataList1.Items
            Dim txtProdName As TextBox = CType(dlItem.FindControl("txtprodname"), TextBox)
            Dim droplist As DropDownList = CType(dlItem.FindControl("supplierDrop"), DropDownList)
            Dim droplist2 As DropDownList = CType(dlItem.FindControl("supplierDrop2"), DropDownList)
            Dim spanFoodID As HtmlGenericControl = CType(dlItem.FindControl("spanProdID"), HtmlGenericControl)
            objcmd.Parameters("@FoodID").Value = spanFoodID.InnerHtml
            objcmd.Parameters("@FreqID").Value = CInt(droplist.SelectedValue)
            objcmd.Parameters("@DWID").Value = CInt(droplist2.SelectedValue)

            objcmd.ExecuteNonQuery()            
        Next
        objconn.Close()
    End Sub
         
          </script>
     </HEAD>
     <body>
     
          <br>
          <div style="TEXT-ALIGN: center">
               <table class="main">
                    <tbody>
                         <tr align="center">
                              <td colspan="5">
                                   
                              </td>
                         </tr>
                         <tr align="center">
                              <td colspan="2">
                                   <form id="Form1" method="post" runat="server">
                                        <asp:DataList id="DataList1" DataKeyField="FoodID" runat="server" OnItemDataBound="datalist_item_bind">
                                             <ItemTemplate>
                                                  <tr align="center">
                                                       <td align="left">
                                                            <span style="display:none" id="Span1" runat="server">
                                                                 <%# DataBinder.Eval(Container.DataItem, "HMID") %>
                                                            </span>
                                                            <div class="formtextsm"><%# DataBinder.Eval(Container.DataItem, "NoNickDisplay") %>
                                                                 <%# DataBinder.Eval(Container.DataItem, "NickDisplay") %>
                                                                 &nbsp;</div>
                                                       </td>
                                                       <td width="5">&nbsp;</td>
                                                       <td align="left">
                                                            <span style="display:none" id="spanProdID" runat="server">
                                                                 <%# DataBinder.Eval(Container.DataItem, "FoodID") %>
                                                            </span>
                                                            <div class="formtextsmbld"><%# DataBinder.Eval(Container.DataItem, "FoodDesc") %>&nbsp;</div>
                                                       </td>
                                                       <td width="5">&nbsp;</td>
                                                       <td>
                                                            <asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server"></asp:DropDownList>
                                                            <asp:DropDownList ID="supplierDrop2" DataTextField="DayName" DataValueField="DWID" Runat="server"></asp:DropDownList>
                                                       </td>
                                                  </tr>
                    </tbody>
                    </ItemTemplate> </asp:DataList>
                    <br>
                    <asp:Button id="cmdSubmit" runat="server" Text="Submit!" OnClick="cmdSubmit_Click"></asp:Button></FORM></TD></TR>
               </table>
          </div>
     </body>
</HTML>
0
Comment
Question by:net_susan
  • 13
  • 8
21 Comments
 
LVL 9

Expert Comment

by:smaccari
ID: 16544497
First, modify your JS function:

<script language="javascript">
function FreqChanged()
{
document.getElementById('supplierDrop2').disabled=true;
}
</script>

And maybe you should use this on your asp.net control:

<asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server" OnClientChange="FreqChanged();"></asp:DropDownList>

(mybe add a clientid attribute too, not sure though)
0
 
LVL 1

Author Comment

by:net_susan
ID: 16544605
'document.getElementById(...)' is null or not an object
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16544804
Can you check in the generated page source code (right click->view source in browser) the generated id for your supplierDrop2 dropdown?
If it's not supplierDrop2, try to add the clientid attribute as i suggested:

<asp:DropDownList ID="supplierDrop2" ClientID="supplierDrop2" DataTextField="DayName" DataValueField="DWID" Runat="server"></asp:DropDownList>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:net_susan
ID: 16544949
<select name="DataList1:_ctl11:supplierDrop2" id="DataList1__ctl11_supplierDrop2" ClientID="supplierDrop2">

Like this nothing happens
OnClientChange="FreqChanged();"

Like this
OnChange="javascript:FreqChanged();"  it tries to fire, but it still says:

'document.getElementById(...)' is null or not an object


0
 
LVL 9

Expert Comment

by:smaccari
ID: 16545111
This should work then:

<script language="javascript">
function FreqChanged()
{
document.getElementById('DataList1__ctl11_supplierDrop2').disabled=true;
}
</script>

By the way, it's strange that you cannot generate a specific id with clientid, i thought you could do this is asp.net controls.
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545408
That works...

Problem is each pulldown is given its own ID:
DataList1__ctl0_supplierDrop2
DataList1__ctl1_supplierDrop2

I'm starting to think there is no way to do this.
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545445
It always coordinates with the first pulldown, though

"DataList1:_ctl0:supplierDrop"
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545486
Is something like this possible?

if document.getElementById('DataList1__ctl0_supplierDrop2').SelectedValue != 4 then
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=true;

else document.getElementById('DataList1__ctl1_supplierDrop2').SelectedValue != 4 then
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=true;
else
'do nothing
end if
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545515
Oh, that's not what I meant. Sorry.

Is something like this possible?

if document.getElementById('DataList1__ctl0_supplierDrop').SelectedValue != 4 then
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=true;

else document.getElementById('DataList1__ctl1_supplierDrop').SelectedValue != 4 then
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=true;
else
'do nothing
end if
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16545520
Like this then:

if (document.getElementById('DataList1__ctl0_supplierDrop2').value != 4)
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=true;
else if (document.getElementById('DataList1__ctl1_supplierDrop2').SelectedValue != 4)
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=true;

But there is something i missed i think. Maybe you could explain what you want to achieve exactly, and maybe tell more about the page context?
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545571
I was posting at the same time you were, I think my last post may help.
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545661
Ta da!

Now I guess I'll just do this a bunch of times:

{
if (document.getElementById('DataList1__ctl0_supplierDrop').value != 4)
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=true;
else if (document.getElementById('DataList1__ctl0_supplierDrop').value = 4)
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=false;

if (document.getElementById('DataList1__ctl1_supplierDrop').value != 4)
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=true;
else if (document.getElementById('DataList1__ctl1_supplierDrop').value = 4)
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=false;
}
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16545675
Ok good JS syntax for your code:

if (document.getElementById('DataList1__ctl0_supplierDrop').value != 4)
document.getElementById('DataList1__ctl0_supplierDrop2').disabled=true;
else if (document.getElementById('DataList1__ctl1_supplierDrop').SelectedValue != 4)
document.getElementById('DataList1__ctl1_supplierDrop2').disabled=true;



0
 
LVL 1

Author Comment

by:net_susan
ID: 16545706
Is there a way to say "if exists" so I can repeat this script a bunch of times without it coming up with an error?

Like... if there is a ctl50, otherwise, ignore this part

(document.getElementById('DataList1__ctl50_supplierDrop').
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16545712
Else, try to do this in your ASP.NET DropDown supplierDrop:

<asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server" OnChange="if (this.value != 4) document.getElementById(this.id +'2').disabled=true"></asp:DropDownList>
0
 
LVL 9

Accepted Solution

by:
smaccari earned 2000 total points
ID: 16545724
Maybe you'll have to reenable the second field if value not 4:

<asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server" OnChange="if (this.value != 4) document.getElementById(this.id +'2').disabled=true; else document.getElementById(this.id +'2').disabled=false;"></asp:DropDownList>
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545761
This rocks!

<asp:DropDownList ID="supplierDrop" DataTextField="HowOft" DataValueField="FreqID" Runat="server" OnChange="if (this.value != 4) document.getElementById(this.id +'2').disabled=true; else document.getElementById(this.id +'2').disabled=false;"></asp:DropDownList>
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16545782
So no more need of the function FreqChanged ;)
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545784
Keep an eye out for a very similar question, please. I'll post a link to it here (might be tomorrow).
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545798
Yes, I had just about lost hope. This is not my first attempt at finding an answer.  :)
0
 
LVL 1

Author Comment

by:net_susan
ID: 16545913
Nevermind, I figured it out for my other page. Thank you so much!!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question